-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
105 lines (85 loc) · 1.75 KB
/
packet.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package socketio
import (
"bytes"
"encoding/json"
"strconv"
)
const Version = 5
const (
PacketTypeConnect PacketType = "0"
PacketTypeDisconnect PacketType = "1"
PacketTypeEvent PacketType = "2"
PacketTypeAck PacketType = "3"
PacketTypeConnectError PacketType = "4"
PacketTypeBinaryEvent PacketType = "5"
PacketTypeBinaryAck PacketType = "6"
)
type PacketType string
type Packet struct {
Type PacketType
Namespace string
AckID *int
Data json.RawMessage
// TODO: add binary
}
type ErrorData struct {
Error string `json:"error"`
}
func (p Packet) MarshalBinary() (data []byte, err error) {
var b bytes.Buffer
b.WriteString(string(p.Type))
if p.Namespace != "/" {
b.WriteString(p.Namespace)
b.WriteByte(',')
}
if p.AckID != nil {
b.WriteString(strconv.FormatInt(int64(*p.AckID), 10))
}
if len(p.Data) != 0 {
b.Write(p.Data)
}
return b.Bytes(), nil
}
func UnmarshalPacket(data []byte) (p Packet, err error) {
err = p.UnmarshalBinary(data)
return p, err
}
func (p *Packet) UnmarshalBinary(data []byte) error {
p.Type = PacketType(data[0])
data = data[1:]
p.Namespace = "/"
if len(data) == 0 {
return nil
}
if len(data) != 0 && data[0] == '/' {
idx := bytes.IndexByte(data, ',')
p.Namespace = string(data[:idx])
data = data[idx+1:]
}
if ack, idx := readInt(data); ack != nil {
p.AckID = ack
data = data[idx:]
}
if len(data) != 0 {
p.Data = data
}
return nil
}
func readInt(data []byte) (*int, int) {
if len(data) == 0 {
return nil, 0
}
var ptr int
for data[ptr] >= '0' && data[ptr] <= '9' {
ptr++
}
if ptr == 0 {
return nil, 0
}
val, _ := strconv.Atoi(string(data[:ptr]))
return &val, ptr
}
func Marshal(val any) []byte {
b, _ := json.Marshal(val)
return b
}