forked from jimingmin/frame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_helper.cpp
371 lines (303 loc) · 8.56 KB
/
server_helper.cpp
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
/*
* server_helper.cpp
*
* Created on: Mar 13, 2015
* Author: jimm
*/
#include "server_helper.h"
#include "common/common_crypt.h"
#include "common/common_datetime.h"
#include "../include/typedef.h"
#include "../include/account_msg.h"
#include "../include/sync_msg.h"
#include "frame/cachekey_define.h"
#include "frame.h"
using namespace FRAME;
static char g_arrSSKey[16] = {'v', 'd', 'c', '$', 'a', 'u', 't', 'h', '@', '1', '7','9','.', 'c', 'o', 'm'};
bool CServerHelper::IsExist(Document &document, const char *szKey)
{
return document.HasMember(szKey);
}
bool CServerHelper::IsExist(Value &value, const char *szKey)
{
return value.HasMember(szKey);
}
bool CServerHelper::GetInt(Document &document, const char *szKey, int32_t &nValue)
{
if(!document.HasMember(szKey))
{
return false;
}
nValue = 0;
if(!document[szKey].IsInt())
{
if(!document[szKey].IsString())
{
return false;
}
nValue = atoi(document[szKey].GetString());
}
else
{
nValue = document[szKey].GetInt();
}
return true;
}
bool CServerHelper::GetUint(Document &document, const char *szKey, uint32_t &nValue)
{
if(!document.HasMember(szKey))
{
return false;
}
nValue = 0;
if(!document[szKey].IsUint())
{
if(!document[szKey].IsString())
{
return false;
}
nValue = atoi(document[szKey].GetString());
}
else
{
nValue = document[szKey].GetUint();
}
return true;
}
bool CServerHelper::GetString(Document &document, const char *szKey, string &nValue)
{
if(!document.HasMember(szKey))
{
return false;
}
nValue = "";
if(!document[szKey].IsString())
{
return false;
}
nValue = document[szKey].GetString();
return true;
}
bool CServerHelper::GetInt(Value &value, const char *szKey, int32_t &nValue)
{
if(!value.HasMember(szKey))
{
return false;
}
nValue = 0;
if(!value[szKey].IsInt())
{
if(!value[szKey].IsString())
{
return false;
}
nValue = atoi(value[szKey].GetString());
}
else
{
nValue = value[szKey].GetInt();
}
return true;
}
bool CServerHelper::GetString(Value &value, const char *szKey, string &nValue)
{
if(!value.HasMember(szKey))
{
return false;
}
nValue = "";
if(!value[szKey].IsString())
{
return false;
}
nValue = value[szKey].GetString();
return true;
}
bool CServerHelper::GetValue(Document &document, const char *szKey, Value &nValue)
{
if(!document.HasMember(szKey))
{
return false;
}
nValue = "";
if(!document[szKey].IsObject())
{
return false;
}
nValue = document[szKey];
return true;
}
bool CServerHelper::GetValue(Value &value, const char *szKey, Value &nValue)
{
if(!value.HasMember(szKey))
{
return false;
}
nValue = "";
if(!value[szKey].IsObject())
{
return false;
}
nValue = value[szKey];
return true;
}
void CServerHelper::Split(uint8_t *szInputBuf, int32_t nInputSize, uint8_t *szOutputBuf, int32_t &nOutputSize, int32_t nSplitSize)
{
for (nOutputSize = 0; nOutputSize < nInputSize;)
{
if((szInputBuf[nOutputSize] & 0xf0) == 0xe0) /**1110xxxx 10xxxxxx 10xxxxxx*/
{
if(nOutputSize + 3 > nSplitSize)
{
break;
}
nOutputSize += 3;
}
else if((szInputBuf[nOutputSize] & 0xe0) == 0xc0)/**110xxxxx 10xxxxxx*/
{
if(nOutputSize + 2 > nSplitSize)
{
break;
}
nOutputSize += 2;
}
else if (szInputBuf[nOutputSize] <= 0x7f)
{
if(nOutputSize + 1 > nSplitSize)
{
break;
}
nOutputSize += 1;
}
else
{
break;
}
}
memcpy(szOutputBuf, szInputBuf, nOutputSize);
}
void CServerHelper::FillControlHead(ControlHead &stCtlHead, uint16_t nTotalSize, ControlCode nCtlCode, uint32_t nUin, SessionID nSessionID,
uint32_t nClientAddress, uint16_t nClientPort, int32_t nGateID, uint8_t nPhoneType, uint32_t nGateRedisAddress,
uint16_t nGateRedisPort)
{
stCtlHead.m_nTotalSize = nTotalSize;
stCtlHead.m_nControlCode = nCtlCode;
stCtlHead.m_nUin = nUin;
stCtlHead.m_nSessionID = nSessionID;
stCtlHead.m_nClientAddress = nClientAddress;
stCtlHead.m_nClientPort = nClientPort;
stCtlHead.m_nGateID = nGateID;
stCtlHead.m_nTimeStamp = CTimeValue::CurrentTime().Milliseconds();
stCtlHead.m_nPhoneType = nPhoneType;
stCtlHead.m_nGateRedisAddress = nGateRedisAddress;
stCtlHead.m_nGateRedisPort = nGateRedisPort;
}
int32_t CServerHelper::MakeMsg(ICtlHead *pCtlHead, IMsgHead *pMsgHead, IMsgBody *pMsgBody, uint8_t *pOutBuf, int32_t nOutBufSize)
{
// ControlHead *pControlHead = (ControlHead *)pCtlHead;
MsgHeadCS *pMsgHeadCS = (MsgHeadCS *)pMsgHead;
uint32_t nOffset = 0;
int32_t nRet = pCtlHead->Encode(pOutBuf, nOutBufSize, nOffset);
if(nRet != 0)
{
return 0;
}
nRet = pMsgHead->Encode(pOutBuf, nOutBufSize, nOffset);
if(nRet != 0)
{
return 0;
}
nRet = pMsgBody->Encode(pOutBuf, nOutBufSize, nOffset);
if(nRet != 0)
{
return 0;
}
uint16_t nCtlHeadSize = pCtlHead->GetSize();
//msghead + msgbody的大小
uint16_t nMsgTotalSize = nOffset - nCtlHeadSize;
pMsgHeadCS->m_nTotalSize = nMsgTotalSize;
nOffset = nCtlHeadSize;
CCodeEngine::Encode(pOutBuf, nOutBufSize, nOffset, nMsgTotalSize);
nOffset = 0;
//ctlhead + msghead + msgbody的大小
uint16_t nCtlTotalSize = nMsgTotalSize + nCtlHeadSize;
CCodeEngine::Encode(pOutBuf, nOutBufSize, nOffset, nCtlTotalSize);
return nCtlTotalSize;
}
int32_t CServerHelper::SendSyncNoti(CRedisChannel *pPushClientChannel, ControlHead *pControlHead, uint32_t nUin)
{
MsgHeadCS stMsgHeadCS;
stMsgHeadCS.m_nMsgID = MSGID_STATUSSYNC_NOTI;
stMsgHeadCS.m_nSrcUin = nUin;
stMsgHeadCS.m_nDstUin = nUin;
CStatusSyncNoti stStatusSyncNoti;
uint8_t arrRespBuf[MAX_MSG_SIZE];
uint16_t nTotalSize = CServerHelper::MakeMsg(pControlHead, &stMsgHeadCS, &stStatusSyncNoti, arrRespBuf, sizeof(arrRespBuf));
pPushClientChannel->RPush(NULL, MakeRedisKey(ClientResp::keyname, pControlHead->m_nGateID), (char *)arrRespBuf, nTotalSize);
g_Frame.Dump(pControlHead, &stMsgHeadCS, &stStatusSyncNoti, "send");
return 0;
}
int32_t CServerHelper::PushToAPNS(CRedisChannel *pPushAPNSChannel, uint32_t nSrcUin, uint32_t nDstUin, uint16_t nMsgID, uint8_t *pBuf, int32_t nBufSize)
{
int32_t nContentLen = (nBufSize >= 50 ? 50 : nBufSize);
char szApnsParams[1024];
int32_t nParamsLen = sprintf(szApnsParams, "%u:%u:%d:%d:", nSrcUin, nDstUin, nMsgID, nContentLen);
memcpy(&szApnsParams[nParamsLen], pBuf, nContentLen);
return pPushAPNSChannel->RPush(NULL, MakeRedisKey(PushApns::keyname), szApnsParams, nParamsLen + nContentLen);
}
int32_t CServerHelper::SendMsgToClient(IIOSession *pIoSession, MsgHeadCS *pMsgHeadCS, uint8_t *pBuf, int32_t nBufSize)
{
uint8_t arrMsg[MAX_MSG_SIZE];
uint16_t nHeadSize = pMsgHeadCS->GetSize();
uint8_t *pBody = pBuf + nHeadSize;
int32_t nBodySize = CXTEA::Encrypt((char *)pBody, nBufSize - pMsgHeadCS->GetSize(), (char *)&arrMsg[nHeadSize],
sizeof(arrMsg) - nHeadSize, g_arrSSKey);
memcpy(arrMsg, pBuf, nHeadSize);
uint16_t nTotalSize = (uint16_t)(nBodySize + pMsgHeadCS->GetSize());
uint32_t nOffset = 0;
CCodeEngine::Encode(arrMsg, sizeof(arrMsg), nOffset, nTotalSize);
g_Frame.Dump(NULL, pMsgHeadCS, NULL, "send ");
return pIoSession->Write(arrMsg, nTotalSize);
}
int32_t CServerHelper::KickUser(ControlHead *pControlHead, MsgHeadCS *pMsgHeadCS, CRedisChannel *pRedisChannel, const char *strReason)
{
ControlHead stCtlHead;
stCtlHead = *pControlHead;
stCtlHead.m_nControlCode = enmControlCode_Close;
MsgHeadCS stMsgHeadCS;
stMsgHeadCS.m_nMsgID = MSGID_KICKUSER_NOTI;
stMsgHeadCS.m_nSrcUin = pMsgHeadCS->m_nSrcUin;
stMsgHeadCS.m_nDstUin = pMsgHeadCS->m_nDstUin;
CKickUserNoti stKickUserNoti;
stKickUserNoti.m_strReason = strReason;
uint8_t arrRespBuf[MAX_MSG_SIZE];
uint16_t nTotalSize = CServerHelper::MakeMsg(&stCtlHead, &stMsgHeadCS, &stKickUserNoti, arrRespBuf, sizeof(arrRespBuf));
pRedisChannel->RPush(NULL, MakeRedisKey(ClientResp::keyname, pControlHead->m_nGateID), (char *)arrRespBuf, nTotalSize);
g_Frame.Dump(&stCtlHead, &stMsgHeadCS, &stKickUserNoti, "send ");
return 0;
}
const char *CServerHelper::MakeRedisKey(const char *szKey)
{
return szKey;
}
const char *CServerHelper::MakeRedisKey(const char *szBaseKey, int32_t nParamKey)
{
static char szRedisKey[1024];
int32_t nRedisKeyLen = sprintf(szRedisKey, "%s%d", szBaseKey, nParamKey);
szRedisKey[nRedisKeyLen] = '\0';
return szRedisKey;
}
const char *CServerHelper::MakeRedisKey(const char *szBaseKey, uint32_t nParamKey)
{
static char szRedisKey[1024];
int32_t nRedisKeyLen = sprintf(szRedisKey, "%s%u", szBaseKey, nParamKey);
szRedisKey[nRedisKeyLen] = '\0';
return szRedisKey;
}
const char *CServerHelper::MakeRedisKey(const char *szBaseKey, const char *szParamKey)
{
static char szRedisKey[1024];
int32_t nRedisKeyLen = sprintf(szRedisKey, "%s%s", szBaseKey, szParamKey);
szRedisKey[nRedisKeyLen] = '\0';
return szRedisKey;
}