-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
65 lines (56 loc) · 1.16 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package gosocket
import (
"encoding/binary"
"encoding/json"
"fmt"
"net"
)
const (
MSG_TYPE_ACK = iota // 响应消息类型, 用于服务器和客户端交互userid和connid
MSG_TYPE_CHAT
)
type Conn struct {
conn net.Conn
connID uint32
userData interface{}
localAddr string
remoteAddr string
}
type ConnEvent struct {
Type int
Conn Conn
Data []byte
}
type ChatMsg struct {
MsgType int
FromID int
ToID int
Data []byte
}
func (c *Conn) SendMsg(msg ChatMsg) (n int, err error) {
msgBody := SerialMsg(msg)
n, err = c.conn.Write(msgBody)
return
}
func (c *Conn) GetRemoteAddr() string {
return c.remoteAddr
}
func SerialMsg(msg ChatMsg) []byte {
msgJson, _ := json.Marshal(msg)
msgByte := []byte(msgJson)
var msgHead [4]byte
binary.BigEndian.PutUint32(msgHead[0:], uint32(len(msgByte)))
msgBody := append(msgHead[0:], []byte(msgJson)...)
return msgBody
}
func HandleMsg(b []byte) (ChatMsg, error) {
msgHeader := b[:4]
msgLength := binary.BigEndian.Uint32(msgHeader)
var msg ChatMsg
err := json.Unmarshal(b[4:msgLength+4], &msg)
if err != nil {
fmt.Printf("解码错误: %+v \n", err)
return msg, err
}
return msg, err
}