-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_mainthread.cpp
executable file
·593 lines (498 loc) · 14.1 KB
/
frame_mainthread.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
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
/*
* frame_mainthread.cpp
*
* Created on: 2011-11-22
* Author: jimm
*/
#include "common/common_api.h"
#include "common/common_datetime.h"
#include "common/common_message.h"
#include "frame_errordef.h"
#include "frame_mainthread.h"
#include "frame_eventid.h"
#include "frame_netqueue.h"
#include "frame_commandqueue.h"
#include "frame_logengine.h"
#include "frame_timer_mgt.h"
#include "frame_session_mgt.h"
#include "frame_msgevent_mgt.h"
#include "frame_connection_mgt.h"
#include "frame_thread_mgt.h"
#include "frame_eventid.h"
#include "frame_crypt.h"
#include "lightframe.h"
FRAME_NAMESPACE_BEGIN
CFrameMainThread::CFrameMainThread()
{
}
CFrameMainThread::~CFrameMainThread()
{
}
//初始化主业务线程
int32_t CFrameMainThread::Initialize()
{
return S_OK;
}
//恢复主业务线程
int32_t CFrameMainThread::Resume()
{
return Initialize();
}
//注销主业务线程
int32_t CFrameMainThread::Uninitialize()
{
return S_OK;
}
//线程入口函数
void CFrameMainThread::Execute()
{
bool bHasData = false;
int32_t nThreadID = (int32_t)gettid();
g_FrameThreadMgt.RegistThread(nThreadID, this);
Delay(enmThreadExecutePeriod * 10);
WRITE_MAIN_LOG(enmLogLevel_Debug, "MainThread Start!\n");
bool bIdle = true;
while ((!GetTerminated()) || (!bIdle))
{
bIdle = true;
//处理定时器事件
bHasData = ProcessTimerEvent();
if (bHasData)
{
bIdle = false;
}
//处理SS通信数据
if (bIdle)
{
bHasData = ProcessSSLogicData();
if (bHasData)
{
bIdle = false;
}
}
//处理CS通信数据
if (bIdle)
{
bHasData = ProcessCSLogicData();
if (bHasData)
{
bIdle = false;
}
}
if (bIdle)
{
bHasData = ProcessTestMessage();
if (bHasData)
{
bIdle = false;
}
}
//若没有任务需要处理
if (bIdle)
{
//写入统计数据
//GET_NETTHREADSTAT_INSTANCE().WriteCount();
Delay(enmThreadExecutePeriod);
}
}
WRITE_MAIN_LOG(enmLogLevel_Debug, "MainThread Stop!\n");
}
bool CFrameMainThread::ProcessTestMessage()
{
NetPacket *pNetPacket = NULL;
int32_t ret = g_FrameCommandQueue.Pop(pNetPacket);
if ((0 > ret) || (pNetPacket == NULL))
{
return false;
}
return ProcessSSMessage(&pNetPacket->m_pNetPacket[0], pNetPacket->m_nNetPacketLen);
}
//处理SS通信数据
bool CFrameMainThread::ProcessSSLogicData()
{
//从接收队列中取出消息
NetPacket *pNetPacket = NULL;
int32_t ret = g_FrameNetQueue.PopRecvSSQueue(pNetPacket);
if ((0 > ret) || (pNetPacket == NULL))
{
return false;
}
bool bHasData = ProcessSSMessage(&pNetPacket->m_pNetPacket[0], pNetPacket->m_nNetPacketLen);
FREE((uint8_t *)pNetPacket);//g_FrameMemMgt.RecycleBlock((uint8_t *)pNetPacket);
return bHasData;
}
//处理CS通信数据
bool CFrameMainThread::ProcessCSLogicData()
{
NetPacket *pNetPacket = NULL;
//从接收队列中取出消息
int32_t ret = g_FrameNetQueue.PopRecvCSQueue(pNetPacket);
if ((0 > ret) || (pNetPacket == NULL))
{
return false;
}
bool bHasData = ProcessCSMessage(&pNetPacket->m_pNetPacket[0], pNetPacket->m_nNetPacketLen);
FREE((uint8_t *)pNetPacket);//g_FrameMemMgt.RecycleBlock((uint8_t *)pNetPacket);
return bHasData;
}
//处理定时器事件
bool CFrameMainThread::ProcessTimerEvent()
{
CFrameTimer *pTimer = NULL;
TimerIndex timerIndex = enmInvalidTimerIndex;
//获取定时器列表中的结束时间最早的定时器
int32_t ret = g_FrameTimerMgt.GetFirstTimer(pTimer, timerIndex);
if (0 > ret)
{
return false;
}
//定时器结束时间小于当前时间
if (pTimer->GetEndTime() > CTimeValue::CurrentTime().Microseconds())
{
return false;
}
//保留TimerSeq 用于保护
uint32_t nTimerSeq = pTimer->GetTimerSeq();
//回调定时器接口
// if(enmFrameTimerType_Session == pTimer->GetTimerType())
// {
// //如果是会话的Timer
// FrameTimerProc pProc = pTimer->GetFrameEventProc();
// if(NULL != pProc)
// {
// pProc(pTimer);
// }
// }
// else
{
//外部Timer直接回调接口
ITimerEvent *pHandle = pTimer->GetEventHandler();
if( NULL != pHandle)
{
TimerProc proc = pTimer->GetEventProc();
if(proc != NULL)
{
(pHandle->*proc)(pTimer);
}
else
{
pHandle->OnTimerEvent(pTimer);
}
}
}
//定时器事件已触发
g_FrameTimerMgt.TimerFired(timerIndex,nTimerSeq);
return true;
}
bool CFrameMainThread::ProcessSSMessage(uint8_t *pBuf, uint32_t nLength)
{
uint32_t nOffset = 0;
bool bHasData = false;
MessageHeadSS stMessageHead;
int32_t ret = stMessageHead.MessageDecode(pBuf, nLength, nOffset);
if (0 > ret)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "Pop center net message, but decode msghead error!\n");
return bHasData;
}
if (nLength < nOffset)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "Pop center net message, but decode msgbody error!\n");
return bHasData;
}
//注册包
if(stMessageHead.nTransType == enmTransType_Regist)
{
OnSystemEvent(SYS_EVENT_REGIST_SERVER, &stMessageHead);
}
else if(stMessageHead.nTransType == enmTransType_Disconnect)
{
OnSystemEvent(SYS_EVENT_DISCONNECT_SERVER, &stMessageHead);
}
else
{
//消息处理
OnMessageEvent(&stMessageHead, nOffset, pBuf + nOffset, nLength - nOffset);
}
return true;
}
bool CFrameMainThread::ProcessCSMessage(uint8_t *pBuf, uint32_t nLength)
{
uint32_t nOffset = 0;
bool bHasData = false;
ConnInfo *pConnInfo = (ConnInfo *)pBuf;
nOffset += sizeof(ConnInfo);
//链接断开
if(pConnInfo->nErrorCode > 0)
{
OnSystemEvent(pConnInfo->nErrorCode, pConnInfo);
//回收资源
g_FrameConnectionMgt.DestroyConnection(pConnInfo->nTunnelIndex);
}
else
{
MessageHeadCS stMessageHead;
int32_t ret = stMessageHead.MessageDecode(pBuf, nLength, nOffset);
if (0 > ret)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "Pop center net message, but decode msghead error!\n");
return bHasData;
}
if (nLength < nOffset)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "Pop center net message, but decode msgbody error!\n");
return bHasData;
}
//获取链接信息
CFrameConnection *pConnection = g_FrameConnectionMgt.GetConnection(pConnInfo->nTunnelIndex);
if(pConnection == NULL)
{
pConnection = g_FrameConnectionMgt.CreateConnection(pConnInfo->nTunnelIndex, pConnInfo);
if(pConnection == NULL)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "create conntion info failed!{nRoleID=%d,nFd=%d}\n", stMessageHead.nRoleID,pConnInfo->nTunnelIndex);
return bHasData;
}
}
uint32_t nBodySize = nLength - nOffset;
//来自web端的消息不需要解密
if(nBodySize > 0 && pConnInfo->nServerID != enmEntityType_Webagent)
{
ret = CFrameCrypt::Decrypt(&stMessageHead, pBuf + nOffset, nBodySize, pConnection);
}
if(ret >= 0)
{
//消息处理
OnMessageEvent(&stMessageHead, nOffset, pBuf + nOffset, nBodySize, sizeof(ConnInfo), &pConnection->m_stConnInfo);
}
}
return bHasData;
}
//系统事件
int32_t CFrameMainThread::OnSystemEvent(uint16_t nEventID, void *pParam)
{
SystemEventInfo *pEventInfo = g_FrameMsgEventMgt.GetSystemEventProc(nEventID);
if(pEventInfo == NULL)
{
return S_OK;
}
if(pEventInfo->pHandle != NULL)
{
return pEventInfo->pHandle->OnSystemEvent(nEventID, pParam);
}
WRITE_MAIN_LOG(enmLogLevel_Warning, "null pointer:sysevent hander is null!{eventid=0x%08x}\n", nEventID);
return S_OK;
}
int32_t CFrameMainThread::OnMessageEvent(MessageHeadCS* pMsgHead, const uint16_t nMsgHeadOffset,
const uint8_t* buf, const uint32_t size, const int32_t nOptionLen, const void *pOption)
{
uint32_t MsgID = pMsgHead->nMessageID;
int32_t nRet = E_UNKNOWN;
uint32_t nBodySize = size;
//检查是否走defaultevent
int32_t nDefEventCount = 0;
DefaultEventInfo *arrPDefaultEventInfo[MAX_DEFEVENT_COUNT] = {NULL};
nRet = g_FrameMsgEventMgt.GetDefEvent(MsgID, arrPDefaultEventInfo, nDefEventCount);
if (nRet < 0)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "it's not found default event hander error!{msgid=0x%08x}\n", MsgID);
return nRet;
}
bool bIsDefEvent = false;
for(int32_t i = 0; i < nDefEventCount; i++)
{
if(arrPDefaultEventInfo[i]->pHandleCS != NULL)
{
if(!bIsDefEvent)
{
bIsDefEvent = true;
}
arrPDefaultEventInfo[i]->pHandleCS->OnMessageEvent(pMsgHead,buf,nBodySize,nOptionLen,pOption);
}
else
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "null pointer:default event hander is null!{msgid=0x%08x}\n", MsgID);
continue;
}
}
if(bIsDefEvent)
{
return S_OK;
}
MsgEventInfo * arrPMsgEventInfo[MAX_MSGEVENT_COUNT] = {NULL};
int32_t nEventCount = 0;
nRet = g_FrameMsgEventMgt.GetMessageEvent(MsgID, arrPMsgEventInfo, nEventCount);
if ((nRet < 0) || (nEventCount <= 0))
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "it's not found msg hander!{msgid=0x%08x}\n", MsgID);
return nRet;
}
for (int32_t i = 0; i < nEventCount; i++)
{
//获取IMsgBody
IMsgBody *pMsgBody = g_MessageMapDecl.GetMessageBody(MsgID);
if(NULL == pMsgBody)
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "it's not found msg body!{msgid=0x%08x}\n", MsgID);
continue;
}
uint32_t offset = 0;
nRet = pMsgBody->MessageDecode(buf, nBodySize, offset);
if( 0 > nRet)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "decode msg body failed!{ret=0x%08x, msgid=0x%08x}\n", nRet, MsgID);
continue;
}
CLightFrame::DumpMessage("LibFrame DumpMessage", pMsgHead, pMsgBody);
if(arrPMsgEventInfo[i]->pHandleCS != NULL)
{
if(arrPMsgEventInfo[i]->CSMsgProc != NULL)
{
CS_MSG_PROC proc = arrPMsgEventInfo[i]->CSMsgProc;
nRet = (arrPMsgEventInfo[i]->pHandleCS->*proc)(pMsgHead, pMsgBody, nOptionLen, pOption);
}
else
{
nRet = arrPMsgEventInfo[i]->pHandleCS->OnMessageEvent(pMsgHead, pMsgBody, nOptionLen, pOption);
}
}
else
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "null pointer:msg hander is null!{msgid=0x%08x}\n", MsgID);
continue;
}
}
return nRet;
}
int32_t CFrameMainThread::OnMessageEvent(MessageHeadSS* pMsgHead, const uint16_t nMsgHeadOffset, const uint8_t* buf, const uint32_t size)
{
uint32_t MsgID = pMsgHead->nMessageID;
int32_t nRet = E_UNKNOWN;
uint32_t nBodySize = pMsgHead->nTotalSize - pMsgHead->nHeadSize;
uint16_t nOptionLen = pMsgHead->nHeadSize - nMsgHeadOffset;
char *pOptiondata = (nOptionLen == 0 ? NULL : (char *)buf);
//检查是否走defaultevent
int32_t nDefEventCount = 0;
DefaultEventInfo *arrPDefaultEventInfo[MAX_DEFEVENT_COUNT] = {NULL};
nRet = g_FrameMsgEventMgt.GetDefEvent(MsgID, arrPDefaultEventInfo, nDefEventCount);
if (nRet < 0)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "it's not found default event hander error!{msgid=0x%08x}\n", MsgID);
return nRet;
}
for(int32_t i = 0; i < nDefEventCount; i++)
{
if(arrPDefaultEventInfo[i]->pHandleSS != NULL)
{
arrPDefaultEventInfo[i]->pHandleSS->OnMessageEvent(pMsgHead,&buf[nOptionLen],nBodySize,nOptionLen,pOptiondata);
}
else
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "null pointer:default event hander is null!{msgid=0x%08x}\n", MsgID);
continue;
}
}
//检察是否有会话在等待这个消息
if(pMsgHead->nSessionIndex != enmInvalidSessionIndex && pMsgHead->nMessageType == enmMessageType_Response)
{
if(g_FrameSessionMgt.IsSessionBindingMsg(pMsgHead->nSessionIndex, MsgID))
{
//执行会话事件回调
nRet = g_FrameSessionMgt.OnFrameSessionEvent(pMsgHead->nSessionIndex, pMsgHead, &buf[nOptionLen], nBodySize);
}
}
else
{
//是否为这个消息注册过所有Event接口
MsgEventInfo * arrPMsgEventInfo[MAX_MSGEVENT_COUNT] = {NULL};
int32_t nEventCount = 0;
nRet = g_FrameMsgEventMgt.GetMessageEvent(MsgID, arrPMsgEventInfo, nEventCount);
if ((nRet < 0) || (nEventCount <= 0))
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "it's not found msg hander!{msgid=0x%08x}\n", MsgID);
return nRet;
}
for (int32_t i = 0; i < nEventCount; i++)
{
if(MsgID == CLOSE_NOTIFY_EVENT) //断线通知
{
nRet = arrPMsgEventInfo[i]->pHandleSS->OnMessageEvent(pMsgHead, NULL, nOptionLen, pOptiondata);
}
else
{
//创建msgbody对象
IMsgBody *pMsgBody = CreateMsgBody(MsgID);
if(pMsgBody == NULL)
{
continue;
}
uint32_t offset = 0;
nRet = pMsgBody->MessageDecode(&buf[nOptionLen], nBodySize, offset);
if( 0 > nRet)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "decode msg body failed!{ret=0x%08x, MsgID=0x%08x}\n", nRet, MsgID);
//销毁msgbody
DestroyMsgBody(pMsgBody);
continue;
}
CLightFrame::DumpMessage("LibFrame DumpMessage", pMsgHead, pMsgBody);
if(arrPMsgEventInfo[i]->pHandleSS != NULL)
{
if(arrPMsgEventInfo[i]->SSMsgProc != NULL)
{
SS_MSG_PROC proc = arrPMsgEventInfo[i]->SSMsgProc;
nRet = (arrPMsgEventInfo[i]->pHandleSS->*proc)(pMsgHead, pMsgBody, nOptionLen, pOptiondata);
}
else
{
nRet = arrPMsgEventInfo[i]->pHandleSS->OnMessageEvent(pMsgHead, pMsgBody, nOptionLen, pOptiondata);
}
}
else
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "null pointer:msg hander is null!{MsgID=0x%08x}\n", MsgID);
//continue;
}
//销毁msgbody
DestroyMsgBody(pMsgBody);
}
}
}
return nRet;
}
IMsgBody *CFrameMainThread::CreateMsgBody(uint32_t nMsgID)
{
//获取IMsgBody
IMsgBody *pMsgBody = g_MessageMapDecl.GetMessageBody(nMsgID);
if(NULL == pMsgBody)
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "it's not found msg body!{nMsgID=0x%08x}\n", nMsgID);
return NULL;
}
IMsgBody *pBodyInstance = pMsgBody;
//业务线程是否是多线程
if(g_FrameConfigMgt.GetFrameBaseConfig().GetAppThreadCount() > 1)
{
pBodyInstance = (IMsgBody *)MALLOC(pMsgBody->GetSize());
if(pBodyInstance == NULL)
{
WRITE_MAIN_LOG(enmLogLevel_Error, "malloc msgbody failed!{nMsgID=0x%08x, Size=%d}\n",
nMsgID, pMsgBody->GetSize());
return NULL;
}
// MUTEX_GUARD(MsgBodyLock, m_stMsgBodyLock);
//复制内容到新的msgbody里面
memcpy(pBodyInstance, pMsgBody, pMsgBody->GetSize());
}
return pBodyInstance;
}
void CFrameMainThread::DestroyMsgBody(IMsgBody *pMsgBody)
{
//业务线程是否是多线程
if(g_FrameConfigMgt.GetFrameBaseConfig().GetAppThreadCount() > 1)
{
// MUTEX_GUARD(MsgBodyLock, m_stMsgBodyLock);
FREE((uint8_t *)pMsgBody);
}
}
FRAME_NAMESPACE_END