-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
134 lines (112 loc) · 2.8 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
package eiop
import "strconv"
var NoopPacket = Packet{Type: Noop}
type (
// PacketType indicates type of engine.io Packet
PacketType byte
Payload []Packet
Packet struct {
Type PacketType
Data any
}
)
const (
// Open is sent from the server when new transport is opened (recheck)
Open PacketType = iota
// Close requests the close of this transport but does not shut down the connection itself.
Close
// Ping is sent by the client. Server should answer with a pong packet containing the same data
Ping
// Pong is sent by the server to respond to ping packets.
Pong
// Message denotes actual message, client and server should call their callbacks with the data.
Message
// Upgrade is sent by the client requesting the server to flush its cache on the old transport and switch to the new transport.
Upgrade
// Noop denotes a noop packet. Used primarily to force a poll cycle when an incoming websocket connection is received.
Noop
// Error is for internal use
Error
)
const (
StrOpen = "open"
StrClose = "close"
StrPing = "ping"
StrPong = "pong"
StrMessage = "message"
StrUpgrade = "upgrade"
StrNoop = "noop"
StrError = "error"
)
var (
mapTypeToStr = map[PacketType]string{
Open: StrOpen,
Close: StrClose,
Ping: StrPing,
Pong: StrPong,
Message: StrMessage,
Upgrade: StrUpgrade,
Noop: StrNoop,
}
mapStrToType = map[string]PacketType{
StrOpen: Open,
StrClose: Close,
StrPing: Ping,
StrPong: Pong,
StrMessage: Message,
StrUpgrade: Upgrade,
StrNoop: Noop,
}
)
// String returns string representation of a PacketType
func (p PacketType) String() string {
if str, ok := mapTypeToStr[p]; ok {
return str
}
return StrError
}
func (p PacketType) Int() int {
return int(p)
}
func (p PacketType) Encode() string {
return strconv.Itoa(p.Int())
}
func (p PacketType) Bytes() []byte {
return []byte(p.Encode())
}
func TextPacket(t PacketType, data ...string) Packet {
return Packet{Type: t, Data: optstr(data...)}
}
func BinaryPacket(t PacketType, data []byte) Packet {
return Packet{Type: t, Data: data}
}
func MessagePacket(data any) Packet {
return Packet{Type: Message, Data: data}
}
func OpenPacket(data ...string) Packet {
return TextPacket(Open, data...)
}
func ClosePacket(reason ...string) Packet {
return TextPacket(Close, reason...)
}
func PingPacket(data ...string) Packet {
return TextPacket(Ping, data...)
}
func PongPacket(data ...string) Packet {
return TextPacket(Pong, data...)
}
func ErrorPacket(err error) Packet {
return Packet{Type: Error, Data: err}
}
func optstr(str ...string) any {
if len(str) == 0 || len(str[0]) == 0 {
return nil
}
return str[0]
}
func (p Payload) Encode() any {
return EncodePayload(p)
}
func (p Packet) Encode(supportsBinary bool) any {
return EncodePacket(p, supportsBinary)
}