-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame_mainthread.cpp
365 lines (300 loc) · 8.16 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
/*
* 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_logengine.h"
#include "frame_timer_mgt.h"
#include "frame_msgevent_mgt.h"
//#include "frame_connection_mgt.h"
#include "frame_eventid.h"
#include "frame_netthread.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;
}
//处理CS通信数据
if (bIdle)
{
bHasData = ProcessCSLogicData();
if (bHasData)
{
bIdle = false;
}
}
if (bIdle)
{
bHasData = g_FrameNetThread.Execute();
if (bHasData)
{
bIdle = false;
}
}
//若没有任务需要处理
if (bIdle)
{
//写入统计数据
//GET_NETTHREADSTAT_INSTANCE().WriteCount();
Delay(enmThreadExecutePeriod);
}
}
WRITE_MAIN_LOG(enmLogLevel_Debug, "MainThread Stop!\n");
}
//处理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);
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::ProcessCSMessage(uint8_t *pBuf, uint32_t nLength)
{
uint32_t nOffset = 0;
bool bHasData = true;
ConnInfo *pConnInfo = (ConnInfo *)pBuf;
nOffset += sizeof(ConnInfo);
//链接断开
if(pConnInfo->nErrorCode > 0)
{
OnSystemEvent(pConnInfo->nErrorCode, pConnInfo);
if((pConnInfo->nErrorCode != SYS_EVENT_INITIATIVE_CONNECT_SUCCESS) ||
(pConnInfo->nErrorCode != SYS_EVENT_PASSIVE_CONNECT_SUCCESS))
{
//回收资源
//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!{nFd=%d}\n", pConnInfo->nTunnelIndex);
// return bHasData;
// }
// }
uint32_t nBodySize = nLength - nOffset;
//消息处理
OnMessageEvent((CSocket *)pConnInfo->pSocket, &stMessageHead, nOffset, pBuf + nOffset, nBodySize, sizeof(ConnInfo), pConnInfo);
}
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(CSocket *pConnection, 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;
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("Recv Message", pMsgHead, pMsgBody);
if(arrPMsgEventInfo[i]->pHandleCS != NULL)
{
if(arrPMsgEventInfo[i]->CSMsgProc != NULL)
{
CS_MSG_PROC proc = arrPMsgEventInfo[i]->CSMsgProc;
nRet = (arrPMsgEventInfo[i]->pHandleCS->*proc)(pConnection, pMsgHead, pMsgBody, nOptionLen, pOption);
}
else
{
nRet = arrPMsgEventInfo[i]->pHandleCS->OnMessageEvent(pConnection, pMsgHead, pMsgBody, nOptionLen, pOption);
}
}
else
{
WRITE_MAIN_LOG(enmLogLevel_Warning, "null pointer:msg hander is null!{msgid=0x%08x}\n", MsgID);
continue;
}
}
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