forked from GoBelieveIO/im_service
/
protocol.go
829 lines (693 loc) · 19.6 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/**
* 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 log "github.com/golang/glog"
import "fmt"
import "errors"
//deprecated
const MSG_HEARTBEAT = 1
const MSG_AUTH = 2
const MSG_AUTH_STATUS = 3
//persistent
const MSG_IM = 4
const MSG_ACK = 5
const MSG_RST = 6
//persistent
const MSG_GROUP_NOTIFICATION = 7
const MSG_GROUP_IM = 8
//deprecated
const MSG_PEER_ACK = 9
const MSG_INPUTING = 10
//deprecated
const MSG_SUBSCRIBE_ONLINE_STATE = 11
const MSG_ONLINE_STATE = 12
const MSG_PING = 13
const MSG_PONG = 14
const MSG_AUTH_TOKEN = 15
const MSG_LOGIN_POINT = 16
const MSG_RT = 17
const MSG_ENTER_ROOM = 18
const MSG_LEAVE_ROOM = 19
const MSG_ROOM_IM = 20
//persistent
const MSG_SYSTEM = 21
const MSG_UNREAD_COUNT = 22
//persistent
const MSG_CUSTOMER_SERVICE = 23
const MSG_VOIP_CONTROL = 64
//平台号
const PLATFORM_IOS = 1
const PLATFORM_ANDROID = 2
const PLATFORM_WEB = 3
const DEFAULT_VERSION = 1
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_ACK] = func()IMessage{return new(MessageACK)}
message_creators[MSG_GROUP_NOTIFICATION] = func()IMessage{return new(GroupNotification)}
message_creators[MSG_PEER_ACK] = func()IMessage{return new(MessagePeerACK)}
message_creators[MSG_INPUTING] = func()IMessage{return new(MessageInputing)}
message_creators[MSG_SUBSCRIBE_ONLINE_STATE] = func()IMessage{return new(MessageSubscribeState)}
message_creators[MSG_ONLINE_STATE] = func()IMessage{return new(MessageOnlineState)}
message_creators[MSG_AUTH_TOKEN] = func()IMessage{return new(AuthenticationToken)}
message_creators[MSG_LOGIN_POINT] = func()IMessage{return new(LoginPoint)}
message_creators[MSG_RT] = func()IMessage{return new(RTMessage)}
message_creators[MSG_ENTER_ROOM] = func()IMessage{return new(Room)}
message_creators[MSG_LEAVE_ROOM] = func()IMessage{return new(Room)}
message_creators[MSG_ROOM_IM] = func()IMessage{return &RoomMessage{new(RTMessage)}}
message_creators[MSG_SYSTEM] = func()IMessage{return new(SystemMessage)}
message_creators[MSG_UNREAD_COUNT] = func()IMessage{return new(MessageUnreadCount)}
message_creators[MSG_CUSTOMER_SERVICE] = func()IMessage{return new(CustomerServiceMessage)}
message_creators[MSG_VOIP_CONTROL] = func()IMessage{return new(VOIPControl)}
vmessage_creators[MSG_GROUP_IM] = func()IVersionMessage{return new(IMMessage)}
vmessage_creators[MSG_IM] = func()IVersionMessage{return new(IMMessage)}
vmessage_creators[MSG_AUTH_STATUS] = func()IVersionMessage{return new(AuthenticationStatus)}
message_descriptions[MSG_AUTH] = "MSG_AUTH"
message_descriptions[MSG_AUTH_STATUS] = "MSG_AUTH_STATUS"
message_descriptions[MSG_IM] = "MSG_IM"
message_descriptions[MSG_ACK] = "MSG_ACK"
message_descriptions[MSG_GROUP_NOTIFICATION] = "MSG_GROUP_NOTIFICATION"
message_descriptions[MSG_GROUP_IM] = "MSG_GROUP_IM"
message_descriptions[MSG_PEER_ACK] = "MSG_PEER_ACK"
message_descriptions[MSG_INPUTING] = "MSG_INPUTING"
message_descriptions[MSG_SUBSCRIBE_ONLINE_STATE] = "MSG_SUBSCRIBE_ONLINE_STATE"
message_descriptions[MSG_ONLINE_STATE] = "MSG_ONLINE_STATE"
message_descriptions[MSG_PING] = "MSG_PING"
message_descriptions[MSG_PONG] = "MSG_PONG"
message_descriptions[MSG_AUTH_TOKEN] = "MSG_AUTH_TOKEN"
message_descriptions[MSG_LOGIN_POINT] = "MSG_LOGIN_POINT"
message_descriptions[MSG_RT] = "MSG_RT"
message_descriptions[MSG_ENTER_ROOM] = "MSG_ENTER_ROOM"
message_descriptions[MSG_LEAVE_ROOM] = "MSG_LEAVE_ROOM"
message_descriptions[MSG_ROOM_IM] = "MSG_ROOM_IM"
message_descriptions[MSG_SYSTEM] = "MSG_SYSTEM"
message_descriptions[MSG_UNREAD_COUNT] = "MSG_UNREAD_COUNT"
message_descriptions[MSG_CUSTOMER_SERVICE] = "MSG_CUSTOMER_SERVICE"
message_descriptions[MSG_VOIP_CONTROL] = "MSG_VOIP_CONTROL"
}
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
}
type RTMessage struct {
sender int64
receiver int64
content string
}
func (message *RTMessage) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, message.sender)
binary.Write(buffer, binary.BigEndian, message.receiver)
buffer.Write([]byte(message.content))
buf := buffer.Bytes()
return buf
}
func (rt *RTMessage) FromData(buff []byte) bool {
if len(buff) < 16 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &rt.sender)
binary.Read(buffer, binary.BigEndian, &rt.receiver)
rt.content = string(buff[16:])
return true
}
type IMMessage struct {
sender int64
receiver int64
timestamp int32
msgid int32
content string
}
func (message *IMMessage) ToDataV0() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, message.sender)
binary.Write(buffer, binary.BigEndian, message.receiver)
binary.Write(buffer, binary.BigEndian, message.msgid)
buffer.Write([]byte(message.content))
buf := buffer.Bytes()
return buf
}
func (im *IMMessage) FromDataV0(buff []byte) bool {
if len(buff) < 20 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &im.sender)
binary.Read(buffer, binary.BigEndian, &im.receiver)
binary.Read(buffer, binary.BigEndian, &im.msgid)
im.content = string(buff[20:])
return true
}
func (message *IMMessage) ToDataV1() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, message.sender)
binary.Write(buffer, binary.BigEndian, message.receiver)
binary.Write(buffer, binary.BigEndian, message.timestamp)
binary.Write(buffer, binary.BigEndian, message.msgid)
buffer.Write([]byte(message.content))
buf := buffer.Bytes()
return buf
}
func (im *IMMessage) FromDataV1(buff []byte) bool {
if len(buff) < 24 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &im.sender)
binary.Read(buffer, binary.BigEndian, &im.receiver)
binary.Read(buffer, binary.BigEndian, &im.timestamp)
binary.Read(buffer, binary.BigEndian, &im.msgid)
im.content = string(buff[24:])
return true
}
func (im *IMMessage) ToData(version int) []byte {
if version == 0 {
return im.ToDataV0()
} else {
return im.ToDataV1()
}
}
func (im *IMMessage) FromData(version int, buff []byte) bool {
if version == 0 {
return im.FromDataV0(buff)
} else {
return im.FromDataV1(buff)
}
}
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() || int(l) < 0 {
return false
}
token := make([]byte, l)
buffer.Read(token)
binary.Read(buffer, binary.BigEndian, &l)
if int(l) > buffer.Len() || int(l) < 0 {
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 //兼容版本0
}
func (auth *AuthenticationStatus) ToData(version int) []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, auth.status)
if version == 0 {
binary.Write(buffer, binary.BigEndian, auth.ip)
}
buf := buffer.Bytes()
return buf
}
func (auth *AuthenticationStatus) FromData(version int, buff []byte) bool {
if len(buff) < 4 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &auth.status)
if version == 0 {
if len(buff) < 8 {
return false
}
binary.Read(buffer, binary.BigEndian, &auth.ip)
}
return true
}
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 MessageACK struct {
seq int32
}
func (ack *MessageACK) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, ack.seq)
buf := buffer.Bytes()
return buf
}
func (ack *MessageACK) FromData(buff []byte) bool {
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &ack.seq)
return true
}
type MessagePeerACK struct {
sender int64
receiver int64
msgid int32
}
func (ack *MessagePeerACK) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, ack.sender)
binary.Write(buffer, binary.BigEndian, ack.receiver)
binary.Write(buffer, binary.BigEndian, ack.msgid)
buf := buffer.Bytes()
return buf
}
func (ack *MessagePeerACK) FromData(buff []byte) bool {
if len(buff) < 20 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &ack.sender)
binary.Read(buffer, binary.BigEndian, &ack.receiver)
binary.Read(buffer, binary.BigEndian, &ack.msgid)
return true
}
type MessageInputing struct {
sender int64
receiver int64
}
func (inputing *MessageInputing) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, inputing.sender)
binary.Write(buffer, binary.BigEndian, inputing.receiver)
buf := buffer.Bytes()
return buf
}
func (inputing *MessageInputing) FromData(buff []byte) bool {
if len(buff) < 16 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &inputing.sender)
binary.Read(buffer, binary.BigEndian, &inputing.receiver)
return true
}
type MessageUnreadCount struct {
count int32
}
func (u *MessageUnreadCount) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, u.count)
buf := buffer.Bytes()
return buf
}
func (u *MessageUnreadCount) FromData(buff []byte) bool {
if len(buff) < 4 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &u.count)
return true
}
type SystemMessage struct {
notification string
}
func (sys *SystemMessage) ToData() []byte {
return []byte(sys.notification)
}
func (sys *SystemMessage) FromData(buff []byte) bool {
sys.notification = string(buff)
return true
}
type CustomerServiceMessage struct {
customer_id int64//普通用户id
sender int64
receiver int64
timestamp int32
content string
}
func (cs *CustomerServiceMessage) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, cs.customer_id)
binary.Write(buffer, binary.BigEndian, cs.sender)
binary.Write(buffer, binary.BigEndian, cs.receiver)
binary.Write(buffer, binary.BigEndian, cs.timestamp)
buffer.Write([]byte(cs.content))
buf := buffer.Bytes()
return buf
}
func (cs *CustomerServiceMessage) FromData(buff []byte) bool {
if len(buff) < 20 {
return false
}
buffer := bytes.NewBuffer(buff)
if len(buff) >= 28 {
//兼容旧数据
binary.Read(buffer, binary.BigEndian, &cs.customer_id)
}
binary.Read(buffer, binary.BigEndian, &cs.sender)
binary.Read(buffer, binary.BigEndian, &cs.receiver)
binary.Read(buffer, binary.BigEndian, &cs.timestamp)
if len(buff) >= 28 {
cs.content = string(buff[28:])
} else {
cs.content = string(buff[20:])
}
return true
}
type GroupNotification struct {
notification string
}
func (notification *GroupNotification) ToData() []byte {
return []byte(notification.notification)
}
func (notification *GroupNotification) FromData(buff []byte) bool {
notification.notification = string(buff)
return true
}
type Room int64
func (room *Room) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, int64(*room))
buf := buffer.Bytes()
return buf
}
func (room *Room) FromData(buff []byte) bool {
if len(buff) < 8 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, (*int64)(room))
return true
}
func (room *Room) RoomID() int64 {
return int64(*room)
}
type RoomMessage struct {
*RTMessage
}
type MessageOnlineState struct {
sender int64
online int32
}
func (state *MessageOnlineState) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, state.sender)
binary.Write(buffer, binary.BigEndian, state.online)
buf := buffer.Bytes()
return buf
}
func (state *MessageOnlineState) FromData(buff []byte) bool {
if len(buff) < 12 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &state.sender)
binary.Read(buffer, binary.BigEndian, &state.online)
return true
}
type MessageSubscribeState struct {
uids []int64
}
func (sub *MessageSubscribeState) ToData() []byte {
return nil
}
func (sub *MessageSubscribeState) FromData(buff []byte) bool {
buffer := bytes.NewBuffer(buff)
var count int32
binary.Read(buffer, binary.BigEndian, &count)
sub.uids = make([]int64, count)
for i := 0; i < int(count); i++ {
binary.Read(buffer, binary.BigEndian, &sub.uids[i])
}
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 AppUserID struct {
appid int64
uid int64
}
func (id *AppUserID) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, id.appid)
binary.Write(buffer, binary.BigEndian, id.uid)
buf := buffer.Bytes()
return buf
}
func (id *AppUserID) FromData(buff []byte) bool {
if len(buff) < 16 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &id.appid)
binary.Read(buffer, binary.BigEndian, &id.uid)
return true
}
type AppRoomID struct {
appid int64
room_id int64
}
func (id *AppRoomID) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, id.appid)
binary.Write(buffer, binary.BigEndian, id.room_id)
buf := buffer.Bytes()
return buf
}
func (id *AppRoomID) FromData(buff []byte) bool {
if len(buff) < 16 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &id.appid)
binary.Read(buffer, binary.BigEndian, &id.room_id)
return true
}
type AppGroupMemberID struct {
appid int64
gid int64
uid int64
}
func (id *AppGroupMemberID) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, id.appid)
binary.Write(buffer, binary.BigEndian, id.gid)
binary.Write(buffer, binary.BigEndian, id.uid)
buf := buffer.Bytes()
return buf
}
func (id *AppGroupMemberID) FromData(buff []byte) bool {
if len(buff) < 24 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &id.appid)
binary.Read(buffer, binary.BigEndian, &id.gid)
binary.Read(buffer, binary.BigEndian, &id.uid)
return true
}
func WriteHeader(len int32, seq int32, cmd byte, version byte, buffer io.Writer) {
binary.Write(buffer, binary.BigEndian, len)
binary.Write(buffer, binary.BigEndian, seq)
t := []byte{cmd, byte(version), 0, 0}
buffer.Write(t)
}
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)
}
func WriteMessage(w *bytes.Buffer, msg *Message) {
body := msg.ToData()
WriteHeader(int32(len(body)), int32(msg.seq), byte(msg.cmd), byte(msg.version), w)
w.Write(body)
}
func SendMessage(conn io.Writer, msg *Message) error {
buffer := new(bytes.Buffer)
WriteMessage(buffer, msg)
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 >= 32*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
}