/
storage_message.go
636 lines (540 loc) · 16.3 KB
/
storage_message.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
/**
* 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 "bytes"
import "encoding/binary"
import log "github.com/golang/glog"
//存储服务器消息
const MSG_SAVE_AND_ENQUEUE = 200
const MSG_DEQUEUE = 201
const MSG_LOAD_OFFLINE = 202
const MSG_LOAD_GROUP_OFFLINE = 203
const MSG_RESULT = 204
const MSG_LOAD_LATEST = 205
const MSG_SAVE_AND_ENQUEUE_GROUP = 206
const MSG_DEQUEUE_GROUP = 207
const MSG_LOAD_HISTORY = 208
//初始化last received id
const MSG_INIT_QUEUE = 209
const MSG_INIT_GROUP_QUEUE = 210
const MSG_GET_OFFLINE_COUNT = 211
const MSG_GET_GROUP_OFFLINE_COUNT = 212
//主从同步消息
const MSG_SYNC_BEGIN = 220
const MSG_SYNC_MESSAGE = 221
const MSG_SYNC_MESSAGE_BATCH = 222
//内部文件存储使用
const MSG_GROUP_IM_LIST = 252
const MSG_GROUP_ACK_IN = 253
const MSG_OFFLINE = 254
const MSG_ACK_IN = 255
func init() {
message_creators[MSG_SAVE_AND_ENQUEUE] = func()IMessage{return new(SAEMessage)}
message_creators[MSG_DEQUEUE] = func()IMessage{return new(DQMessage)}
message_creators[MSG_LOAD_OFFLINE] = func()IMessage{return new(LoadOffline)}
message_creators[MSG_LOAD_GROUP_OFFLINE] = func()IMessage{return new(LoadGroupOffline)}
message_creators[MSG_RESULT] = func()IMessage{return new(MessageResult)}
message_creators[MSG_LOAD_LATEST] = func()IMessage{return new(LoadLatest)}
message_creators[MSG_LOAD_HISTORY] = func()IMessage{return new(LoadHistory)}
message_creators[MSG_SAVE_AND_ENQUEUE_GROUP] = func()IMessage{return new(SAEMessage)}
message_creators[MSG_DEQUEUE_GROUP] = func()IMessage{return new(DQGroupMessage)}
message_creators[MSG_INIT_QUEUE] = func()IMessage{return new(InitQueue)}
message_creators[MSG_INIT_GROUP_QUEUE] = func()IMessage{return new(InitGroupQueue)}
message_creators[MSG_GET_OFFLINE_COUNT] = func()IMessage{return new(LoadOffline)}
message_creators[MSG_GET_GROUP_OFFLINE_COUNT] = func()IMessage{return new(LoadGroupOffline)}
message_creators[MSG_GROUP_IM_LIST] = func()IMessage{return new(GroupOfflineMessage)}
message_creators[MSG_GROUP_ACK_IN] = func()IMessage{return new(GroupOfflineMessage)}
message_creators[MSG_OFFLINE] = func()IMessage{return new(OfflineMessage)}
message_creators[MSG_ACK_IN] = func()IMessage{return new(MessageACKIn)}
message_creators[MSG_SYNC_BEGIN] = func()IMessage{return new(SyncCursor)}
message_creators[MSG_SYNC_MESSAGE] = func()IMessage{return new(EMessage)}
message_creators[MSG_SYNC_MESSAGE_BATCH] = func()IMessage{return new(MessageBatch)}
message_descriptions[MSG_SAVE_AND_ENQUEUE] = "MSG_SAVE_AND_ENQUEUE"
message_descriptions[MSG_DEQUEUE] = "MSG_DEQUEUE"
message_descriptions[MSG_LOAD_OFFLINE] = "MSG_LOAD_OFFLINE"
message_descriptions[MSG_RESULT] = "MSG_RESULT"
message_descriptions[MSG_LOAD_LATEST] = "MSG_LOAD_LATEST"
message_descriptions[MSG_LOAD_HISTORY] = "MSG_LOAD_HISTORY"
message_descriptions[MSG_INIT_QUEUE] = "MSG_INIT_QUEUE"
message_descriptions[MSG_INIT_GROUP_QUEUE] = "MSG_INIT_GROUP_QUEUE"
message_descriptions[MSG_SAVE_AND_ENQUEUE_GROUP] = "MSG_SAVE_AND_ENQUEUE_GROUP"
message_descriptions[MSG_DEQUEUE_GROUP] = "MSG_DEQUEUE_GROUP"
message_descriptions[MSG_SYNC_BEGIN] = "MSG_SYNC_BEGIN"
message_descriptions[MSG_SYNC_MESSAGE] = "MSG_SYNC_MESSAGE"
message_descriptions[MSG_SYNC_MESSAGE_BATCH] = "MSG_SYNC_MESSAGE_BATCH"
}
type SyncCursor struct {
msgid int64
}
func (cursor *SyncCursor) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, cursor.msgid)
return buffer.Bytes()
}
func (cursor *SyncCursor) FromData(buff []byte) bool {
if len(buff) < 8 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &cursor.msgid)
return true
}
type EMessage struct {
msgid int64
device_id int64
msg *Message
}
func (emsg *EMessage) ToData() []byte {
if emsg.msg == nil {
return nil
}
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, emsg.msgid)
binary.Write(buffer, binary.BigEndian, emsg.device_id)
mbuffer := new(bytes.Buffer)
WriteMessage(mbuffer, emsg.msg)
msg_buf := mbuffer.Bytes()
var l int16 = int16(len(msg_buf))
binary.Write(buffer, binary.BigEndian, l)
buffer.Write(msg_buf)
buf := buffer.Bytes()
return buf
}
func (emsg *EMessage) FromData(buff []byte) bool {
if len(buff) < 18 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &emsg.msgid)
binary.Read(buffer, binary.BigEndian, &emsg.device_id)
var l int16
binary.Read(buffer, binary.BigEndian, &l)
if int(l) > buffer.Len() {
return false
}
msg_buf := make([]byte, l)
buffer.Read(msg_buf)
mbuffer := bytes.NewBuffer(msg_buf)
//recusive
msg := ReceiveMessage(mbuffer)
if msg == nil {
return false
}
emsg.msg = msg
return true
}
type MessageBatch struct {
first_id int64
last_id int64
msgs []*Message
}
func (batch *MessageBatch) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, batch.first_id)
binary.Write(buffer, binary.BigEndian, batch.last_id)
count := int32(len(batch.msgs))
binary.Write(buffer, binary.BigEndian, count)
for _, m := range batch.msgs {
SendMessage(buffer, m)
}
buf := buffer.Bytes()
return buf
}
func (batch *MessageBatch) FromData(buff []byte) bool {
if len(buff) < 18 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &batch.first_id)
binary.Read(buffer, binary.BigEndian, &batch.last_id)
var count int32
binary.Read(buffer, binary.BigEndian, &count)
batch.msgs = make([]*Message, 0, count)
for i := 0; i < int(count); i++ {
msg := ReceiveMessage(buffer)
if msg == nil {
return false
}
batch.msgs = append(batch.msgs, msg)
}
return true
}
type OfflineMessage struct {
appid int64
receiver int64
msgid int64
device_id int64
prev_msgid int64
}
func (off *OfflineMessage) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, off.appid)
binary.Write(buffer, binary.BigEndian, off.receiver)
binary.Write(buffer, binary.BigEndian, off.msgid)
binary.Write(buffer, binary.BigEndian, off.device_id)
binary.Write(buffer, binary.BigEndian, off.prev_msgid)
buf := buffer.Bytes()
return buf
}
func (off *OfflineMessage) FromData(buff []byte) bool {
if len(buff) < 32 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &off.appid)
binary.Read(buffer, binary.BigEndian, &off.receiver)
binary.Read(buffer, binary.BigEndian, &off.msgid)
if len(buff) == 40 {
binary.Read(buffer, binary.BigEndian, &off.device_id)
}
binary.Read(buffer, binary.BigEndian, &off.prev_msgid)
return true
}
type MessageACKIn struct {
appid int64
receiver int64
msgid int64
device_id int64
}
func (off *MessageACKIn) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, off.appid)
binary.Write(buffer, binary.BigEndian, off.receiver)
binary.Write(buffer, binary.BigEndian, off.msgid)
binary.Write(buffer, binary.BigEndian, off.device_id)
buf := buffer.Bytes()
return buf
}
func (off *MessageACKIn) FromData(buff []byte) bool {
if len(buff) < 32 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &off.appid)
binary.Read(buffer, binary.BigEndian, &off.receiver)
binary.Read(buffer, binary.BigEndian, &off.msgid)
binary.Read(buffer, binary.BigEndian, &off.device_id)
return true
}
type DQMessage struct {
appid int64
receiver int64
msgid int64
device_id int64
}
func (dq *DQMessage) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, dq.appid)
binary.Write(buffer, binary.BigEndian, dq.receiver)
binary.Write(buffer, binary.BigEndian, dq.msgid)
binary.Write(buffer, binary.BigEndian, dq.device_id)
buf := buffer.Bytes()
return buf
}
func (dq *DQMessage) FromData(buff []byte) bool {
if len(buff) < 32 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &dq.appid)
binary.Read(buffer, binary.BigEndian, &dq.receiver)
binary.Read(buffer, binary.BigEndian, &dq.msgid)
binary.Read(buffer, binary.BigEndian, &dq.device_id)
return true
}
type DQGroupMessage struct {
appid int64
receiver int64
msgid int64
gid int64
device_id int64
}
func (dq *DQGroupMessage) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, dq.appid)
binary.Write(buffer, binary.BigEndian, dq.receiver)
binary.Write(buffer, binary.BigEndian, dq.msgid)
binary.Write(buffer, binary.BigEndian, dq.gid)
binary.Write(buffer, binary.BigEndian, dq.device_id)
buf := buffer.Bytes()
return buf
}
func (dq *DQGroupMessage) FromData(buff []byte) bool {
if len(buff) < 40 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &dq.appid)
binary.Read(buffer, binary.BigEndian, &dq.receiver)
binary.Read(buffer, binary.BigEndian, &dq.msgid)
binary.Read(buffer, binary.BigEndian, &dq.gid)
binary.Read(buffer, binary.BigEndian, &dq.device_id)
return true
}
type GroupOfflineMessage struct {
appid int64
receiver int64
msgid int64
gid int64
device_id int64
prev_msgid int64
}
func (off *GroupOfflineMessage) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, off.appid)
binary.Write(buffer, binary.BigEndian, off.receiver)
binary.Write(buffer, binary.BigEndian, off.msgid)
binary.Write(buffer, binary.BigEndian, off.gid)
binary.Write(buffer, binary.BigEndian, off.device_id)
binary.Write(buffer, binary.BigEndian, off.prev_msgid)
buf := buffer.Bytes()
return buf
}
func (off *GroupOfflineMessage) FromData(buff []byte) bool {
if len(buff) < 40 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &off.appid)
binary.Read(buffer, binary.BigEndian, &off.receiver)
binary.Read(buffer, binary.BigEndian, &off.msgid)
binary.Read(buffer, binary.BigEndian, &off.gid)
if len(buff) == 48 {
binary.Read(buffer, binary.BigEndian, &off.device_id)
}
binary.Read(buffer, binary.BigEndian, &off.prev_msgid)
return true
}
type SAEMessage struct {
msg *Message
appid int64
receiver int64
device_id int64
}
func (sae *SAEMessage) ToData() []byte {
if sae.msg == nil {
return nil
}
if sae.msg.cmd == MSG_SAVE_AND_ENQUEUE {
log.Warning("recusive sae message")
return nil
}
buffer := new(bytes.Buffer)
mbuffer := new(bytes.Buffer)
WriteMessage(mbuffer, sae.msg)
msg_buf := mbuffer.Bytes()
var l int16 = int16(len(msg_buf))
binary.Write(buffer, binary.BigEndian, l)
buffer.Write(msg_buf)
binary.Write(buffer, binary.BigEndian, sae.appid)
binary.Write(buffer, binary.BigEndian, sae.receiver)
binary.Write(buffer, binary.BigEndian, sae.device_id)
buf := buffer.Bytes()
return buf
}
func (sae *SAEMessage) FromData(buff []byte) bool {
if len(buff) < 4 {
return false
}
buffer := bytes.NewBuffer(buff)
var l int16
binary.Read(buffer, binary.BigEndian, &l)
if int(l) > buffer.Len() {
return false
}
msg_buf := make([]byte, l)
buffer.Read(msg_buf)
mbuffer := bytes.NewBuffer(msg_buf)
//recusive
msg := ReceiveMessage(mbuffer)
if msg == nil {
return false
}
sae.msg = msg
if buffer.Len() < 24 {
return false
}
binary.Read(buffer, binary.BigEndian, &sae.appid)
binary.Read(buffer, binary.BigEndian, &sae.receiver)
binary.Read(buffer, binary.BigEndian, &sae.device_id)
return true
}
type MessageResult struct {
status int32
content []byte
}
func (result *MessageResult) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, result.status)
buffer.Write(result.content)
buf := buffer.Bytes()
return buf
}
func (result *MessageResult) FromData(buff []byte) bool {
if len(buff) < 4 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &result.status)
result.content = buff[4:]
return true
}
type LoadLatest struct {
app_uid AppUserID
limit int32
}
func (lh *LoadLatest) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, lh.app_uid.appid)
binary.Write(buffer, binary.BigEndian, lh.app_uid.uid)
binary.Write(buffer, binary.BigEndian, lh.limit)
buf := buffer.Bytes()
return buf
}
func (lh *LoadLatest) FromData(buff []byte) bool {
if len(buff) < 20 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &lh.app_uid.appid)
binary.Read(buffer, binary.BigEndian, &lh.app_uid.uid)
binary.Read(buffer, binary.BigEndian, &lh.limit)
return true
}
type LoadHistory struct {
appid int64
uid int64
msgid int64
}
func (lh *LoadHistory) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, lh.appid)
binary.Write(buffer, binary.BigEndian, lh.uid)
binary.Write(buffer, binary.BigEndian, lh.msgid)
buf := buffer.Bytes()
return buf
}
func (lh *LoadHistory) FromData(buff []byte) bool {
if len(buff) < 24 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &lh.appid)
binary.Read(buffer, binary.BigEndian, &lh.uid)
binary.Read(buffer, binary.BigEndian, &lh.msgid)
return true
}
type LoadOffline struct {
appid int64
uid int64
device_id int64
}
func (lo *LoadOffline) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, lo.appid)
binary.Write(buffer, binary.BigEndian, lo.uid)
binary.Write(buffer, binary.BigEndian, lo.device_id)
buf := buffer.Bytes()
return buf
}
func (lo *LoadOffline) FromData(buff []byte) bool {
if len(buff) < 24 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &lo.appid)
binary.Read(buffer, binary.BigEndian, &lo.uid)
binary.Read(buffer, binary.BigEndian, &lo.device_id)
return true
}
type LoadGroupOffline struct {
appid int64
gid int64
uid int64
device_id int64
}
func (lo *LoadGroupOffline) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, lo.appid)
binary.Write(buffer, binary.BigEndian, lo.gid)
binary.Write(buffer, binary.BigEndian, lo.uid)
binary.Write(buffer, binary.BigEndian, lo.device_id)
buf := buffer.Bytes()
return buf
}
func (lo *LoadGroupOffline) FromData(buff []byte) bool {
if len(buff) < 32 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &lo.appid)
binary.Read(buffer, binary.BigEndian, &lo.gid)
binary.Read(buffer, binary.BigEndian, &lo.uid)
binary.Read(buffer, binary.BigEndian, &lo.device_id)
return true
}
type InitQueue struct {
appid int64
uid int64
device_id int64
}
func (lo *InitQueue) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, lo.appid)
binary.Write(buffer, binary.BigEndian, lo.uid)
binary.Write(buffer, binary.BigEndian, lo.device_id)
buf := buffer.Bytes()
return buf
}
func (lo *InitQueue) FromData(buff []byte) bool {
if len(buff) < 24 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &lo.appid)
binary.Read(buffer, binary.BigEndian, &lo.uid)
binary.Read(buffer, binary.BigEndian, &lo.device_id)
return true
}
type InitGroupQueue struct {
appid int64
gid int64
uid int64
device_id int64
}
func (lo *InitGroupQueue) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, lo.appid)
binary.Write(buffer, binary.BigEndian, lo.gid)
binary.Write(buffer, binary.BigEndian, lo.uid)
binary.Write(buffer, binary.BigEndian, lo.device_id)
buf := buffer.Bytes()
return buf
}
func (lo *InitGroupQueue) FromData(buff []byte) bool {
if len(buff) < 32 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &lo.appid)
binary.Read(buffer, binary.BigEndian, &lo.gid)
binary.Read(buffer, binary.BigEndian, &lo.uid)
binary.Read(buffer, binary.BigEndian, &lo.device_id)
return true
}