-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet.go
305 lines (275 loc) · 5.71 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* Golang socket.io
* Copyright (C) 2024 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package socket
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"github.com/LiterMC/socket.io/internal/utils"
)
var (
errTooMuchArgs = errors.New("Too much arguments were given")
errIncompletePacket = errors.New("Incomplete packet")
)
type UnexpectedPacketTypeError struct {
Type PacketType
}
var _ error = (*UnexpectedPacketTypeError)(nil)
func (e *UnexpectedPacketTypeError) Error() string {
return fmt.Sprintf("Unexpected packet type %d", e.Type)
}
type UnexpectedTokenError struct {
Token byte
}
var _ error = (*UnexpectedTokenError)(nil)
func (e *UnexpectedTokenError) Error() string {
return fmt.Sprintf("Unexpected token %q", e.Token)
}
type PacketType int8
const (
unknownType PacketType = -1
CONNECT PacketType = iota
DISCONNECT
EVENT
ACK
CONNECT_ERROR
BINARY_EVENT
BINARY_ACK
)
func (t PacketType) String() string {
switch t {
case CONNECT:
return "CONNECT"
case DISCONNECT:
return "DISCONNECT"
case EVENT:
return "EVENT"
case ACK:
return "ACK"
case CONNECT_ERROR:
return "CONNECT_ERROR"
case BINARY_EVENT:
return "BINARY_EVENT"
case BINARY_ACK:
return "BINARY_ACK"
}
return fmt.Sprintf("PacketType(%d)", (int8)(t))
}
func (t PacketType) ID() byte {
switch t {
case CONNECT:
return '0'
case DISCONNECT:
return '1'
case EVENT:
return '2'
case ACK:
return '3'
case CONNECT_ERROR:
return '4'
case BINARY_EVENT:
return '5'
case BINARY_ACK:
return '6'
}
panic(fmt.Errorf("Unknown socket.io packet type %d", t))
}
func pktTypeFromByte(id byte) PacketType {
switch id {
case '0':
return CONNECT
case '1':
return DISCONNECT
case '2':
return EVENT
case '3':
return ACK
case '4':
return CONNECT_ERROR
case '5':
return BINARY_EVENT
case '6':
return BINARY_ACK
}
return unknownType
}
type Packet struct {
typ PacketType
namespace string
id int // is the actual id + 1
data []byte
attachs [][]byte
}
func (p *Packet) Type() PacketType {
return p.typ
}
func (p *Packet) String() string {
return fmt.Sprintf("Packet(%s, %q, %d, <%d bytes>)", p.typ.String(), p.namespace, p.id, len(p.data))
}
func (p *Packet) Id() int {
return p.id - 1
}
func (p *Packet) SetId(id int) {
p.id = id + 1
}
func (p *Packet) SetData(args ...any) (err error) {
p.data = nil
p.attachs = p.attachs[:0]
if len(args) == 0 {
return
}
switch p.typ {
case EVENT, ACK, BINARY_EVENT, BINARY_ACK:
p.encodeAttachs(args)
if len(p.attachs) > 0 {
switch p.typ {
case EVENT:
p.typ = BINARY_EVENT
case ACK:
p.typ = BINARY_ACK
}
}
p.data, err = json.Marshal(args)
default:
if len(args) > 1 {
panic(errTooMuchArgs)
}
p.data, err = json.Marshal(args[0])
}
return
}
func (p *Packet) UnmarshalData(ptr any) (err error) {
if err = json.Unmarshal(p.data, ptr); err != nil {
return
}
p.decodeAttachs(ptr)
return
}
func (p *Packet) Attachments() [][]byte {
return p.attachs
}
func (p *Packet) WriteTo(w io.Writer) (n int64, err error) {
var n0 int
if err = utils.WriteByte(w, p.typ.ID()); err != nil {
return
}
n++
var buf [10]byte
if attLeng := len(p.attachs); attLeng > 0 {
n0, err = w.Write(append(strconv.AppendInt(buf[:0], (int64)(attLeng), 10), '-'))
n += (int64)(n0)
if err != nil {
return
}
}
if p.namespace != "" && p.namespace != "/" {
if n0, err = io.WriteString(w, p.namespace); err != nil {
return
}
n += (int64)(n0)
if err = utils.WriteByte(w, ','); err != nil {
return
}
n++
}
if p.id > 0 {
n0, err = w.Write(strconv.AppendInt(buf[:0], (int64)(p.id-1), 10))
n += (int64)(n0)
if err != nil {
return
}
}
if len(p.data) > 0 {
n0, err = w.Write(p.data)
n += (int64)(n0)
if err != nil {
return
}
}
return
}
func (p *Packet) UnmarshalBinary(data []byte) (err error) {
if len(data) == 0 {
return io.EOF
}
typ := pktTypeFromByte(data[0])
if typ == unknownType {
err = &UnexpectedPacketTypeError{typ}
return
}
p.typ = typ
p.namespace = ""
p.id = 0
p.data = p.data[:0]
p.attachs = p.attachs[:0]
if data = data[1:]; len(data) == 0 {
return
}
var (
num int
attachLen int
)
num, data = readNumber(data)
if num >= 0 { // if read a number
if ok := len(data) > 0; ok && data[0] == '-' { // is <# of binary attachments>-
attachLen = num
} else { // assume it is <acknowledgment id>
p.id = num + 1
if ok {
goto READ_DATA
}
return
}
}
if data[0] == '/' { // is <namespace>,
i := bytes.IndexByte(data, ',')
if i < 0 {
return io.EOF
}
p.namespace, data = (string)(data[:i]), data[i+1:]
}
READ_DATA:
p.data = append(p.data, data...)
if cap(p.attachs) >= attachLen {
p.attachs = p.attachs[:attachLen]
} else {
p.attachs = make([][]byte, attachLen)
}
return
}
func readNumber(data []byte) (num int, rest []byte) {
if len(data) == 0 || data[0] < '0' || '9' < data[0] {
return -1, data
}
for {
if len(data) == 0 {
return
}
b := data[0]
if b < '0' || '9' < b {
rest = data
return
}
data = data[1:]
num = num*10 + (int)(b-'0')
}
}