-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.cpp
212 lines (180 loc) · 5.28 KB
/
connection.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
/*
* connection.cpp
*
* Created on: 2013年8月22日
* Author: 1854
*/
#include "server_namespace.h"
#include "common/common_api.h"
#include "connection.h"
#include "player.h"
#include "frame_mem_mgt.h"
#include "frame_netqueue.h"
#include "message_define.h"
#include "frame_msgevent_mgt.h"
FRAME_ROBOT_NAMESPACE_BEGIN
CConnection::CConnection()
{
m_pPlayer = NULL;
m_pConnectSucccess = NULL;
m_pConnectTimeout = NULL;
}
CConnection::~CConnection()
{
}
int32_t CConnection::MakeMessage()
{
//开始组包
while(m_stRecvBuffer.Size() > 0)
{
MessageHeadCS stMsgHead;
if(m_nCurPacketSize <= 0)
{
//获取包大小字段所占的字节数
int32_t nPacketLengthSize = sizeof(stMsgHead.nTotalSize);
if(m_stRecvBuffer.Size() < nPacketLengthSize)
{
break;
}
//取出包头前面的包大小字段,然后解码出来
uint8_t arrPacketSize[nPacketLengthSize];
m_stRecvBuffer.PeekRead(arrPacketSize, nPacketLengthSize);
uint32_t nOffset = 0;
uint32_t nPacketSize = *((uint32_t *)arrPacketSize);
m_nCurPacketSize = nPacketSize + nPacketLengthSize;
//CCodeEngine::Decode(arrPacketSize, nPacketLengthSize, nOffset, m_nCurPacketSize);
}
int32_t nWantDataSize = (int32_t)(m_nCurPacketSize - m_nPacketOffset);
int32_t nRealGetDataSize = (m_stRecvBuffer.Size() >= nWantDataSize ? nWantDataSize : m_stRecvBuffer.Size());
if(nRealGetDataSize <= 0)
{
break;
}
if(m_pPacket == NULL)
{
int32_t nPacketLen = m_nCurPacketSize + sizeof(ConnInfo);
uint8_t *pMem = MALLOC(sizeof(NetPacket) + nPacketLen);
if(pMem == NULL)
{
break;
}
m_pPacket = new(pMem)NetPacket();
m_pPacket->m_nNetPacketLen = nPacketLen;
}
//前面存放conninfo结构
uint8_t *pPacketHead = &m_pPacket->m_pNetPacket[sizeof(ConnInfo)];
if(m_stRecvBuffer.Read(&pPacketHead[m_nPacketOffset], nRealGetDataSize) <= 0)
{
break;
}
m_nPacketOffset += nRealGetDataSize;
if(m_nPacketOffset >= m_nCurPacketSize)
{
uint32_t nOffset = 0;
int32_t nRet = stMsgHead.MessageDecode(pPacketHead, m_nPacketOffset, nOffset);
if(nRet < 0)
{
FREE((uint8_t *)m_pPacket);//g_FrameMemMgt.RecycleBlock((uint8_t *)m_pPacket);
m_pPacket = NULL;
m_nPacketOffset = 0;
m_nCurPacketSize = 0;
return nRet;
}
//过滤不关心的消息包
if(!g_FrameMsgEventMgt.IsHasMessageEvent(stMsgHead.nMessageID))
{
FREE((uint8_t *)m_pPacket);
m_pPacket = NULL;
m_nPacketOffset = 0;
m_nCurPacketSize = 0;
continue;
}
//m_nRoleID = stMsgHead.nRoleID;
MsgEventInfo * arrPMsgEventInfo[MAX_MSGEVENT_COUNT] = {NULL};
int32_t nEventCount = 0;
nRet = g_FrameMsgEventMgt.GetMessageEvent(stMsgHead.nMessageID, arrPMsgEventInfo, nEventCount);
if ((nRet < 0) || (nEventCount <= 0))
{
// WRITE_MAIN_LOG(enmLogLevel_Warning, "it's not found msg hander!{msgid=0x%08x}\n", stMsgHead.nMessageID);
// FREE((uint8_t *)m_pPacket);
// m_pPacket = NULL;
// m_nPacketOffset = 0;
// m_nCurPacketSize = 0;
// return ret;
}
for (int32_t i = 0; i < nEventCount; i++)
{
//获取IMsgBody
IMsgBody *pMsgBody = g_MessageMapDecl.GetMessageBody(stMsgHead.nMessageID);
if(NULL == pMsgBody)
{
//WRITE_MAIN_LOG(enmLogLevel_Warning, "it's not found msg body!{msgid=0x%08x}\n", stMsgHead.nMessageID);
continue;
}
nRet = pMsgBody->MessageDecode(pPacketHead, m_nPacketOffset, nOffset);
if( 0 > nRet)
{
//WRITE_MAIN_LOG(enmLogLevel_Error, "decode msg body failed!{ret=0x%08x, msgid=0x%08x}\n", ret, stMsgHead.nMessageID);
continue;
}
if(stMsgHead.nMessageID == 18)
{
char arrTimeString[1024];
GetTimeString(arrTimeString);
uint32_t offset = 0;
char szLog[enmMaxLogInfoLength];
szLog[0] = 0;
sprintf(szLog + offset, "{RecvTime : %s} MessageHeadCS=", arrTimeString);
offset = (uint32_t)strlen(szLog);
stMsgHead.Dump(szLog, enmMaxLogInfoLength, offset);
sprintf(szLog + offset, " MessageBody=");
offset = (uint32_t)strlen(szLog);
pMsgBody->Dump(szLog + offset, enmMaxLogInfoLength, offset);
WRITE_MAIN_LOG(enmLogLevel_Notice, "%s\n", szLog);
}
}
ConnInfo *pConnInfo = new(&m_pPacket->m_pNetPacket[0]) ConnInfo;
pConnInfo->pSocket = this;
pConnInfo->nTunnelIndex = (uint16_t)m_nSocketFD;
pConnInfo->nErrorCode = 0;
g_FrameNetQueue.PushRecvCSQueue(m_pPacket);
m_pPacket = NULL;
m_nPacketOffset = 0;
m_nCurPacketSize = 0;
}
}
return S_OK;
}
int32_t CConnection::OnConnected()
{
int32_t ret = S_OK;
if((m_pPlayer != NULL) && (m_pConnectSucccess != NULL))
{
ret = m_pPlayer->ConnectServerSuccess();
}
return ret;
}
int32_t CConnection::OnConnectTimeout()
{
int32_t ret = S_OK;
if((m_pPlayer != NULL) && (m_pConnectTimeout != NULL))
{
ret = m_pPlayer->ConnectServerTimeout();
}
return ret;
}
void CConnection::SetConnectSuccessCallback(CPlayer *pPlayer, connectsuccess proc)
{
m_pPlayer = pPlayer;
m_pConnectSucccess = proc;
}
void CConnection::SetConnectTimeoutCallback(CPlayer *pPlayer, connecttimeout proc)
{
m_pPlayer = pPlayer;
m_pConnectTimeout = proc;
}
CPlayer *CConnection::GetPlayerObj()
{
return m_pPlayer;
}
FRAME_ROBOT_NAMESPACE_END