forked from nielsAD/gowarcraft3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.go
252 lines (224 loc) · 7.58 KB
/
serialization.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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
package bncs
import (
"io"
"io/ioutil"
"github.com/nielsAD/gowarcraft3/protocol"
)
// SerializationBuffer is used by SerializePacketWithBuffer to bring amortized allocs to 0 for repeated calls
type SerializationBuffer = protocol.Buffer
// SerializePacketWithBuffer serializes p and writes it to w.
func SerializePacketWithBuffer(w io.Writer, b *SerializationBuffer, p Packet) (int, error) {
b.Truncate()
if err := p.Serialize(b); err != nil {
return 0, err
}
return w.Write(b.Bytes)
}
// SerializePacket serializes p and writes it to w.
func SerializePacket(w io.Writer, p Packet) (int, error) {
return SerializePacketWithBuffer(w, &SerializationBuffer{}, p)
}
// DeserializationBuffer is used by DeserializePacketWithBuffer to bring amortized allocs to 0 for repeated calls
type DeserializationBuffer struct {
Buffer [4096]byte
keepAlive KeepAlive
ping Ping
enterChatReq EnterChatReq
enterChatResp EnterChatResp
joinChannel JoinChannel
chatCommand ChatCommand
chatEvent ChatEvent
floodDetected FloodDetected
messageBox MessageBox
getAdvListResp GetAdvListResp
getAdvListReq GetAdvListReq
startAdvex3Resp StartAdvex3Resp
startAdvex3Req StartAdvex3Req
stopAdv StopAdv
notifyJoin NotifyJoin
netGamePort NetGamePort
authInfoResp AuthInfoResp
authInfoReq AuthInfoReq
authCheckResp AuthCheckResp
authCheckReq AuthCheckReq
authAccountCreateResp AuthAccountCreateResp
authAccountCreateReq AuthAccountCreateReq
authAccountLogonResp AuthAccountLogonResp
authAccountLogonReq AuthAccountLogonReq
authAccountLogonProofResp AuthAccountLogonProofResp
authAccountLogonProofReq AuthAccountLogonProofReq
setEmail SetEmail
clanInfo ClanInfo
unknownPacket UnknownPacket
}
// ReadPacketWithBuffer reads exactly one packet from r and returns its raw bytes.
func ReadPacketWithBuffer(r io.Reader, b *DeserializationBuffer) ([]byte, int, error) {
if n, err := io.ReadFull(r, b.Buffer[:4]); err != nil {
if err == io.ErrUnexpectedEOF {
err = ErrNoProtocolSig
}
return nil, n, err
}
if b.Buffer[0] != ProtocolSig {
return nil, 4, ErrNoProtocolSig
}
var size = int(uint16(b.Buffer[3])<<8 | uint16(b.Buffer[2]))
if size < 4 {
return nil, 4, ErrNoProtocolSig
} else if size > len(b.Buffer) {
if n, err := io.CopyN(ioutil.Discard, r, int64(size-4)); err != nil {
return nil, int(n + 4), err
}
return nil, size, ErrBufferTooSmall
}
if n, err := io.ReadFull(r, b.Buffer[4:size]); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, n + 4, err
}
return b.Buffer[:size], size, nil
}
// DeserializeClientPacketWithBuffer reads exactly one client packet from r and returns it in the proper (deserialized) packet type.
func DeserializeClientPacketWithBuffer(r io.Reader, b *DeserializationBuffer) (Packet, int, error) {
var bytes, n, err = ReadPacketWithBuffer(r, b)
if err != nil {
return nil, n, err
}
var pbuf = protocol.Buffer{Bytes: bytes}
var pkt Packet
// Explicitly call deserialize on type instead of interface for compiler optimizations
switch b.Buffer[1] {
case PidNull:
err = b.keepAlive.Deserialize(&pbuf)
pkt = &b.keepAlive
case PidStopAdv:
err = b.stopAdv.Deserialize(&pbuf)
pkt = &b.stopAdv
case PidGetAdvListEx:
err = b.getAdvListReq.Deserialize(&pbuf)
pkt = &b.getAdvListReq
case PidEnterChat:
err = b.enterChatReq.Deserialize(&pbuf)
pkt = &b.enterChatReq
case PidJoinChannel:
err = b.joinChannel.Deserialize(&pbuf)
pkt = &b.joinChannel
case PidChatCommand:
err = b.chatCommand.Deserialize(&pbuf)
pkt = &b.chatCommand
case PidStartAdvex3:
err = b.startAdvex3Req.Deserialize(&pbuf)
pkt = &b.startAdvex3Req
case PidNotifyJoin:
err = b.notifyJoin.Deserialize(&pbuf)
pkt = &b.notifyJoin
case PidPing:
err = b.ping.Deserialize(&pbuf)
pkt = &b.ping
case PidNetGamePort:
err = b.netGamePort.Deserialize(&pbuf)
pkt = &b.netGamePort
case PidAuthInfo:
err = b.authInfoReq.Deserialize(&pbuf)
pkt = &b.authInfoReq
case PidAuthCheck:
err = b.authCheckReq.Deserialize(&pbuf)
pkt = &b.authCheckReq
case PidAuthAccountCreate:
err = b.authAccountCreateReq.Deserialize(&pbuf)
pkt = &b.authAccountCreateReq
case PidAuthAccountLogon:
err = b.authAccountLogonReq.Deserialize(&pbuf)
pkt = &b.authAccountLogonReq
case PidAuthAccountLogonProof:
err = b.authAccountLogonProofReq.Deserialize(&pbuf)
pkt = &b.authAccountLogonProofReq
case PidSetEmail:
err = b.setEmail.Deserialize(&pbuf)
pkt = &b.setEmail
default:
err = b.unknownPacket.Deserialize(&pbuf)
pkt = &b.unknownPacket
}
if err != nil {
return nil, n, err
}
return pkt, n, nil
}
// DeserializeServerPacketWithBuffer reads exactly one server packet from r and returns it in the proper (deserialized) packet type.
func DeserializeServerPacketWithBuffer(r io.Reader, b *DeserializationBuffer) (Packet, int, error) {
var bytes, n, err = ReadPacketWithBuffer(r, b)
if err != nil {
return nil, n, err
}
var pbuf = protocol.Buffer{Bytes: bytes}
var pkt Packet
// Explicitly call deserialize on type instead of interface for compiler optimizations
switch b.Buffer[1] {
case PidNull:
err = b.keepAlive.Deserialize(&pbuf)
pkt = &b.keepAlive
case PidGetAdvListEx:
err = b.getAdvListResp.Deserialize(&pbuf)
pkt = &b.getAdvListResp
case PidEnterChat:
err = b.enterChatResp.Deserialize(&pbuf)
pkt = &b.enterChatResp
case PidChatEvent:
err = b.chatEvent.Deserialize(&pbuf)
pkt = &b.chatEvent
case PidFloodDetected:
err = b.floodDetected.Deserialize(&pbuf)
pkt = &b.floodDetected
case PidMessageBox:
err = b.messageBox.Deserialize(&pbuf)
pkt = &b.messageBox
case PidStartAdvex3:
err = b.startAdvex3Resp.Deserialize(&pbuf)
pkt = &b.startAdvex3Resp
case PidPing:
err = b.ping.Deserialize(&pbuf)
pkt = &b.ping
case PidAuthInfo:
err = b.authInfoResp.Deserialize(&pbuf)
pkt = &b.authInfoResp
case PidAuthCheck:
err = b.authCheckResp.Deserialize(&pbuf)
pkt = &b.authCheckResp
case PidAuthAccountCreate:
err = b.authAccountCreateResp.Deserialize(&pbuf)
pkt = &b.authAccountCreateResp
case PidAuthAccountLogon:
err = b.authAccountLogonResp.Deserialize(&pbuf)
pkt = &b.authAccountLogonResp
case PidAuthAccountLogonProof:
err = b.authAccountLogonProofResp.Deserialize(&pbuf)
pkt = &b.authAccountLogonProofResp
case PidClanInfo:
err = b.clanInfo.Deserialize(&pbuf)
pkt = &b.clanInfo
default:
err = b.unknownPacket.Deserialize(&pbuf)
pkt = &b.unknownPacket
}
if err != nil {
return nil, n, err
}
return pkt, n, nil
}
// ReadPacket reads exactly one packet from r and returns it in the proper (deserialized) packet type.
func ReadPacket(r io.Reader, b *DeserializationBuffer) ([]byte, int, error) {
return ReadPacketWithBuffer(r, &DeserializationBuffer{})
}
// DeserializeClientPacket reads exactly one packet from r and returns it in the proper (deserialized) packet type.
func DeserializeClientPacket(r io.Reader) (Packet, int, error) {
return DeserializeClientPacketWithBuffer(r, &DeserializationBuffer{})
}
// DeserializeServerPacket reads exactly one packet from r and returns it in the proper (deserialized) packet type.
func DeserializeServerPacket(r io.Reader) (Packet, int, error) {
return DeserializeServerPacketWithBuffer(r, &DeserializationBuffer{})
}