-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathprotocol.go
350 lines (290 loc) · 8.03 KB
/
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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import "io"
import "bytes"
import "encoding/binary"
import "fmt"
import "errors"
import log "github.com/golang/glog"
const MSG_HEARTBEAT = 1
const MSG_AUTH = 2
const MSG_AUTH_STATUS = 3
const MSG_PING = 13
const MSG_PONG = 14
const MSG_AUTH_TOKEN = 15
const MSG_LOGIN_POINT = 16
const MSG_VOIP_CONTROL = 64
var message_descriptions map[int]string = make(map[int]string)
type MessageCreator func()IMessage
var message_creators map[int]MessageCreator = make(map[int]MessageCreator)
type VersionMessageCreator func()IVersionMessage
var vmessage_creators map[int]VersionMessageCreator = make(map[int]VersionMessageCreator)
func init() {
message_creators[MSG_AUTH] = func()IMessage {return new(Authentication)}
message_creators[MSG_AUTH_STATUS] = func()IMessage {return new(AuthenticationStatus)}
message_creators[MSG_AUTH_TOKEN] = func()IMessage{return new(AuthenticationToken)}
message_creators[MSG_VOIP_CONTROL] = func()IMessage{return new(VOIPControl)}
message_creators[MSG_LOGIN_POINT] = func()IMessage{return new(LoginPoint)}
message_descriptions[MSG_AUTH] = "MSG_AUTH"
message_descriptions[MSG_AUTH_STATUS] = "MSG_AUTH_STATUS"
message_descriptions[MSG_VOIP_CONTROL] = "MSG_VOIP_CONTROL"
message_descriptions[MSG_PING] = "MSG_PING"
message_descriptions[MSG_PONG] = "MSG_PONG"
message_descriptions[MSG_AUTH_TOKEN] = "MSG_AUTH_TOKEN"
}
type Command int
func (cmd Command) String() string {
c := int(cmd)
if desc, ok := message_descriptions[c]; ok {
return desc
} else {
return fmt.Sprintf("%d", c)
}
}
type IMessage interface {
ToData() []byte
FromData(buff []byte) bool
}
type IVersionMessage interface {
ToData(version int) []byte
FromData(version int, buff []byte) bool
}
type Message struct {
cmd int
seq int
version int
body interface{}
}
func (message *Message) ToData() []byte {
if message.body != nil {
if m, ok := message.body.(IMessage); ok {
return m.ToData()
}
if m, ok := message.body.(IVersionMessage); ok {
return m.ToData(message.version)
}
return nil
} else {
return nil
}
}
func (message *Message) FromData(buff []byte) bool {
cmd := message.cmd
if creator, ok := message_creators[cmd]; ok {
c := creator()
r := c.FromData(buff)
message.body = c
return r
}
if creator, ok := vmessage_creators[cmd]; ok {
c := creator()
r := c.FromData(message.version, buff)
message.body = c
return r
}
return len(buff) == 0
}
func WriteHeader(len int32, seq int32, cmd byte, version byte, buffer *bytes.Buffer) {
binary.Write(buffer, binary.BigEndian, len)
binary.Write(buffer, binary.BigEndian, seq)
buffer.WriteByte(cmd)
buffer.WriteByte(byte(version))
buffer.WriteByte(byte(0))
buffer.WriteByte(byte(0))
}
func ReadHeader(buff []byte) (int, int, int, int) {
var length int32
var seq int32
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &length)
binary.Read(buffer, binary.BigEndian, &seq)
cmd, _ := buffer.ReadByte()
version, _ := buffer.ReadByte()
return int(length), int(seq), int(cmd), int(version)
}
type LoginPoint struct {
up_timestamp int32
platform_id int8
device_id string
}
func (point *LoginPoint) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, point.up_timestamp)
binary.Write(buffer, binary.BigEndian, point.platform_id)
buffer.Write([]byte(point.device_id))
buf := buffer.Bytes()
return buf
}
func (point *LoginPoint) FromData(buff []byte) bool {
if len(buff) <= 5 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &point.up_timestamp)
binary.Read(buffer, binary.BigEndian, &point.platform_id)
point.device_id = string(buff[5:])
return true
}
type VOIPControl struct {
sender int64
receiver int64
content []byte
}
func (ctl *VOIPControl) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, ctl.sender)
binary.Write(buffer, binary.BigEndian, ctl.receiver)
buffer.Write([]byte(ctl.content))
buf := buffer.Bytes()
return buf
}
func (ctl *VOIPControl) FromData(buff []byte) bool {
if len(buff) <= 16 {
return false
}
buffer := bytes.NewBuffer(buff[:16])
binary.Read(buffer, binary.BigEndian, &ctl.sender)
binary.Read(buffer, binary.BigEndian, &ctl.receiver)
ctl.content = buff[16:]
return true
}
type Authentication struct {
uid int64
}
func (auth *Authentication) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, auth.uid)
buf := buffer.Bytes()
return buf
}
func (auth *Authentication) FromData(buff []byte) bool {
if len(buff) < 8 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &auth.uid)
return true
}
type AuthenticationToken struct {
token string
platform_id int8
device_id string
}
func (auth *AuthenticationToken) ToData() []byte {
var l int8
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, auth.platform_id)
l = int8(len(auth.token))
binary.Write(buffer, binary.BigEndian, l)
buffer.Write([]byte(auth.token))
l = int8(len(auth.device_id))
binary.Write(buffer, binary.BigEndian, l)
buffer.Write([]byte(auth.device_id))
buf := buffer.Bytes()
return buf
}
func (auth *AuthenticationToken) FromData(buff []byte) bool {
var l int8
if (len(buff) <= 3) {
return false
}
auth.platform_id = int8(buff[0])
buffer := bytes.NewBuffer(buff[1:])
binary.Read(buffer, binary.BigEndian, &l)
if int(l) > buffer.Len() {
return false
}
token := make([]byte, l)
buffer.Read(token)
binary.Read(buffer, binary.BigEndian, &l)
if int(l) > buffer.Len() {
return false
}
device_id := make([]byte, l)
buffer.Read(device_id)
auth.token = string(token)
auth.device_id = string(device_id)
return true
}
type AuthenticationStatus struct {
status int32
ip int32 //主机公网ip
}
func (auth *AuthenticationStatus) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, auth.status)
binary.Write(buffer, binary.BigEndian, auth.ip)
buf := buffer.Bytes()
return buf
}
func (auth *AuthenticationStatus) FromData(buff []byte) bool {
if len(buff) < 8 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &auth.status)
binary.Read(buffer, binary.BigEndian, &auth.ip)
return true
}
func SendMessage(conn io.Writer, msg *Message) error {
body := msg.ToData()
buffer := new(bytes.Buffer)
WriteHeader(int32(len(body)), int32(msg.seq), byte(msg.cmd), byte(msg.version), buffer)
buffer.Write(body)
buf := buffer.Bytes()
n, err := conn.Write(buf)
if err != nil {
log.Info("sock write error:", err)
return err
}
if n != len(buf) {
log.Infof("write less:%d %d", n, len(buf))
return errors.New("write less")
}
return nil
}
func ReceiveMessage(conn io.Reader) *Message {
buff := make([]byte, 12)
_, err := io.ReadFull(conn, buff)
if err != nil {
log.Info("sock read error:", err)
return nil
}
length, seq, cmd, version := ReadHeader(buff)
if length < 0 || length > 64*1024 {
log.Info("invalid len:", length)
return nil
}
buff = make([]byte, length)
_, err = io.ReadFull(conn, buff)
if err != nil {
log.Info("sock read error:", err)
return nil
}
message := new(Message)
message.cmd = cmd
message.seq = seq
message.version = version
if !message.FromData(buff) {
log.Warning("parse error")
return nil
}
return message
}