-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol.go
49 lines (40 loc) · 889 Bytes
/
protocol.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
package mattress
import (
"github.com/giskook/gotcp"
"log"
)
var (
Illegal uint16 = 0
HalfPack uint16 = 255
ReportStatus uint16 = 1
)
type MattressProtocol struct {
}
func (this *MattressProtocol) ReadPacket(c *gotcp.Conn) (gotcp.Packet, error) {
smconn := c.GetExtraData().(*Conn)
buffer := smconn.GetBuffer()
conn := c.GetRawConn()
for {
data := make([]byte, 2048)
readLengh, err := conn.Read(data)
log.Printf("%X\n", data[0:readLengh])
if err != nil {
return nil, err
}
if readLengh == 0 {
return nil, gotcp.ErrConnClosing
} else {
buffer.Write(data[0:readLengh])
cmdid, pkglen := CheckProtocol(buffer)
// log.Printf("recv box cmd %d \n", cmdid)
pkgbyte := make([]byte, pkglen)
buffer.Read(pkgbyte)
switch cmdid {
case ReportStatus:
return ParseReportStatus(pkgbyte), nil
case HalfPack:
case Illegal:
}
}
}
}