forked from GoBelieveIO/im_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_message.go
263 lines (209 loc) · 6.4 KB
/
route_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
/**
* 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"
func init() {
message_creators[MSG_SUBSCRIBE] = func()IMessage{return new(SubscribeMessage)}
message_creators[MSG_UNSUBSCRIBE] = func()IMessage{return new(RouteUserID)}
message_creators[MSG_PUBLISH] = func()IMessage{return new(RouteMessage)}
message_creators[MSG_PUSH] = func()IMessage{return new(BatchPushMessage)}
message_creators[MSG_PUBLISH_GROUP] = func()IMessage{return new(RouteMessage)}
message_creators[MSG_SUBSCRIBE_ROOM] = func()IMessage{return new(RouteRoomID)}
message_creators[MSG_UNSUBSCRIBE_ROOM] = func()IMessage{return new(RouteRoomID)}
message_creators[MSG_PUBLISH_ROOM] = func()IMessage{return new(RouteMessage)}
message_descriptions[MSG_SUBSCRIBE] = "MSG_SUBSCRIBE"
message_descriptions[MSG_UNSUBSCRIBE] = "MSG_UNSUBSCRIBE"
message_descriptions[MSG_PUBLISH] = "MSG_PUBLISH"
message_descriptions[MSG_PUSH] = "MSG_PUSH"
message_descriptions[MSG_PUBLISH_GROUP] = "MSG_PUBLISH_GROUP"
message_descriptions[MSG_SUBSCRIBE_ROOM] = "MSG_SUBSCRIBE_ROOM"
message_descriptions[MSG_UNSUBSCRIBE_ROOM] = "MSG_UNSUBSCRIBE_ROOM"
message_descriptions[MSG_PUBLISH_ROOM] = "MSG_PUBLISH_ROOM"
}
//批量消息的推送
type BatchPushMessage struct {
appid int64
receivers []int64
msg *Message
}
func (amsg *BatchPushMessage) ToData() []byte {
if amsg.msg == nil {
return nil
}
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, amsg.appid)
var count uint16
count = uint16(len(amsg.receivers))
binary.Write(buffer, binary.BigEndian, count)
for _, receiver := range amsg.receivers {
binary.Write(buffer, binary.BigEndian, receiver)
}
mbuffer := new(bytes.Buffer)
WriteMessage(mbuffer, amsg.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 (amsg *BatchPushMessage) FromData(buff []byte) bool {
if len(buff) < 12 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &amsg.appid)
var count uint16
binary.Read(buffer, binary.BigEndian, &count)
if len(buff) < 8+2+int(count)*8+2 {
return false
}
receivers := make([]int64, 0, count)
for i := 0; i < int(count); i++ {
var receiver int64
binary.Read(buffer, binary.BigEndian, &receiver)
receivers = append(receivers, receiver)
}
amsg.receivers = receivers
var l int16
binary.Read(buffer, binary.BigEndian, &l)
if int(l) > buffer.Len() || l < 0 {
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
}
amsg.msg = msg
return true
}
type RouteMessage struct {
appid int64
receiver int64
msgid int64
prev_msgid int64
device_id int64
timestamp int64 //纳秒,测试消息从im->imr->im的时间
msg []byte
}
func (amsg *RouteMessage) ToData() []byte {
if amsg.msg == nil {
return nil
}
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, amsg.appid)
binary.Write(buffer, binary.BigEndian, amsg.receiver)
binary.Write(buffer, binary.BigEndian, amsg.msgid)
binary.Write(buffer, binary.BigEndian, amsg.device_id)
binary.Write(buffer, binary.BigEndian, amsg.timestamp)
msg_buf := amsg.msg
var l int16 = int16(len(msg_buf))
binary.Write(buffer, binary.BigEndian, l)
buffer.Write(msg_buf)
buf := buffer.Bytes()
return buf
}
func (amsg *RouteMessage) FromData(buff []byte) bool {
if len(buff) < 42 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &amsg.appid)
binary.Read(buffer, binary.BigEndian, &amsg.receiver)
binary.Read(buffer, binary.BigEndian, &amsg.msgid)
binary.Read(buffer, binary.BigEndian, &amsg.device_id)
binary.Read(buffer, binary.BigEndian, &amsg.timestamp)
var l int16
binary.Read(buffer, binary.BigEndian, &l)
if int(l) > buffer.Len() || l < 0 {
return false
}
msg_buf := make([]byte, l)
buffer.Read(msg_buf)
amsg.msg = msg_buf
return true
}
type SubscribeMessage struct {
appid int64
uid int64
online int8 //1 or 0
}
func (sub *SubscribeMessage) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, sub.appid)
binary.Write(buffer, binary.BigEndian, sub.uid)
binary.Write(buffer, binary.BigEndian, sub.online)
buf := buffer.Bytes()
return buf
}
func (sub *SubscribeMessage) FromData(buff []byte) bool {
if len(buff) < 17 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &sub.appid)
binary.Read(buffer, binary.BigEndian, &sub.uid)
binary.Read(buffer, binary.BigEndian, &sub.online)
return true
}
type RouteUserID struct {
appid int64
uid int64
}
func (id *RouteUserID) 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 *RouteUserID) 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 RouteRoomID struct {
appid int64
room_id int64
}
func (id *RouteRoomID) 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 *RouteRoomID) 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
}