-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame_outsidesocket.cpp
243 lines (199 loc) · 5.12 KB
/
frame_outsidesocket.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
/*
* frame_clientsocket.cpp
*
* Created on: 2012-12-3
* Author: jimm
*/
#include "frame_netqueue.h"
#include "frame_outsidesocket.h"
#include "common/common_codeengine.h"
#include "frame_eventid.h"
#include "frame_configmgt.h"
//#include "frame_connection_mgt.h"
#include "frame_mem_mgt.h"
#include "frame_msgevent_mgt.h"
FRAME_NAMESPACE_BEGIN
COutsideSocket::COutsideSocket()
{
Reset();
}
COutsideSocket::~COutsideSocket()
{
}
void COutsideSocket::Reset()
{
m_nSocketAttr = enmSocketAttr_Outside;
Clear();
}
void COutsideSocket::Clear()
{
CSocket::Clear();
//m_nRoleID = enmInvalidRoleID;
if(m_pPacket != NULL)
{
FREE((uint8_t *)m_pPacket);
}
m_pPacket = NULL;
m_nCurPacketSize = 0;
m_nPacketOffset = 0;
}
int32_t COutsideSocket::OnRead(int32_t nErrorCode)
{
uint8_t arrBuf[enmMaxMessageSize];
int32_t nCloseCode = 0;
int32_t nRecvBytes = 0;
int32_t nRet = Recv(arrBuf, enmMaxMessageSize, nRecvBytes);
if(nRet != S_OK)
{
nCloseCode = nRet;
}
m_stRecvBuffer.Write(arrBuf, nRecvBytes);
//提取消息包
MakeMessage();
if(nCloseCode != 0)
{
CloseSocket(nCloseCode);
return E_SOCKETERROR;
}
return S_OK;
}
int32_t COutsideSocket::OnWrite(int32_t nErrorCode)
{
if(nErrorCode != 0)
{
CloseSocket(SYS_EVENT_CONN_ERROR);
return E_SOCKETERROR;
}
int32_t nRet = SendRestData();
if(nRet < 0)
{
CloseSocket(SYS_EVENT_CONN_ERROR);
return E_SOCKETERROR;
}
return S_OK;
}
int32_t COutsideSocket::OnError(int32_t nErrorCode)
{
CloseSocket(SYS_EVENT_CONN_ERROR);
return S_OK;
}
int32_t COutsideSocket::OnConnected()
{
//刚连接成功是不会有数据的
MakeMessage();
MakeSystemEvent(SYS_EVENT_INITIATIVE_CONNECT_SUCCESS);
return S_OK;
}
int32_t COutsideSocket::OnDisconnect(int32_t nCloseCode)
{
m_pEpoll->DeleteEvent(this);
//没有通过认证的就不给应用层发送断开连接事件
//if(g_FrameConnectionMgt.GetConnection(m_nSocketFD) != NULL)
{
MakeMessage();
//客户端主动关闭
if(nCloseCode == (int32_t)E_SOCKETDISCONNECTED)
{
MakeSystemEvent(SYS_EVENT_CONN_CLIENT_CLOSED);
}
else
{
MakeSystemEvent(SYS_EVENT_CONN_ERROR);
}
}
// //解除peer端与此socket对象的映射
// g_FrameSocketMgt.DelSocket(m_nPeerType, m_nSocketFD);
// //回收此socket对象
// g_FrameSocketMgt.DestroySocketObject(this);
return S_OK;
}
//生成链接关闭事件
int32_t COutsideSocket::MakeSystemEvent(uint16_t nEventID)
{
int32_t nPacketLen = sizeof(NetPacket) + sizeof(ConnInfo);
uint8_t *pMem = MALLOC(nPacketLen);//g_FrameMemMgt.AllocBlock(nPacketLen);
if(pMem == NULL)
{
return E_NULLPOINTER;
}
NetPacket *pNetPacket = new(pMem)NetPacket();
pNetPacket->m_nNetPacketLen = nPacketLen;
ConnInfo *pConnInfo = new(&pNetPacket->m_pNetPacket[0])ConnInfo;
pConnInfo->pSocket = this;
pConnInfo->nTunnelIndex = (uint16_t)m_nSocketFD;
pConnInfo->nErrorCode = nEventID;
g_FrameNetQueue.PushRecvCSQueue(pNetPacket);
return S_OK;
}
int32_t COutsideSocket::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);
m_pPacket = NULL;
m_nPacketOffset = 0;
m_nCurPacketSize = 0;
return nRet;
}
//m_nRoleID = stMsgHead.nRoleID;
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;
}
FRAME_NAMESPACE_END