-
Notifications
You must be signed in to change notification settings - Fork 14
/
PackageIOCPClient.cpp
311 lines (240 loc) · 6.95 KB
/
PackageIOCPClient.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
#include "IOCPCommon.h"
#include "IOCPBuffer.h"
#include "IOCPBufferWriter.h"
#include "IOCPBufferReader.h"
#include "BaseIOCPServer.h"
#include "PackageIOCP.h"
#include "PackageIOCPClient.h"
#pragma comment(lib,"ws2_32.lib")
PackageIOCPClient::PackageIOCPClient()
{
WSAStartup(MAKEWORD(2,2),&m_stWSAData);
m_bIsConnected = FALSE;
m_bShutdown = false;
m_hThread = (HANDLE)_beginthreadex(NULL,0,AsyncProcessThread,this,0,NULL);
}
PackageIOCPClient::~PackageIOCPClient()
{
DWORD dwExitCode;
m_bShutdown = true;
WaitForSingleObject(m_hThread,10000);
if(GetExitCodeThread(m_hThread,&dwExitCode))
{
if(dwExitCode != 0){
TerminateThread(m_hThread,0);
}
}
CloseHandle(m_hThread);
if(m_bIsConnected){
CloseConnect();
}
}
VOID PackageIOCPClient::CloseConnect(){
closesocket(m_ClientContext.sdSocketCopy);
m_ClientContext.sdSocketCopy = INVALID_SOCKET;
while(m_ClientContext.m_SendBufferList.Empty() == false){
delete m_ClientContext.m_SendBufferList.Pop();
}
if(m_ClientContext.m_SendingBytes){
delete m_ClientContext.m_SendingBytes;
m_ClientContext.m_SendingBytes = NULL;
}
m_ClientContext.m_ReceivedBytes.GetBuffer()->m_pData->m_dwDataLength = 0;
}
SOCKET PackageIOCPClient::GetSockets(){
SOCKET sd = WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,NULL,0);
if(sd != INVALID_SOCKET){
u_long mode = 1;
ioctlsocket(sd,FIONBIO,&mode);
}
return sd;
}
VOID PackageIOCPClient::AsyncConnect(u_long adr,u_short port)
{
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.S_un.S_addr = adr;
sin.sin_port = htons(port);
connect(m_ClientContext.sdSocketCopy,(sockaddr *)&sin,sizeof(sin));
}
VOID PackageIOCPClient::AsyncProcessReceive(){
char buf[0x1000];
int buf_len = recv(m_ClientContext.sdSocketCopy,buf,sizeof(buf),0);
if(buf_len <= 0){
if(WSAGetLastError() == WSAEWOULDBLOCK){
return;
}
CloseConnect();
return;
}
m_ClientContext.m_ReceivedBytes.Write((const unsigned char*)&buf,buf_len);
while(true){
CIOCPBuffer* lpBuffer = m_ClientContext.m_ReceivedBytes.GetBuffer();
//等待下次接受数据
if(lpBuffer->GetLength() < sizeof(NETPackage)){
return;
}
NETPackage* Package = (NETPackage*)lpBuffer->GetBytes();
//包数据大小
if(Package->length < lpBuffer->GetLength()){
return;
}
//非法的数据
if(Package->title != NET_PACKAGE_TITLE){
CloseConnect();
return;
}
CIOCPBuffer NotifBuffer;
NotifBuffer.Append(&lpBuffer->GetBytes()[sizeof(NETPackage)],(Package->length - sizeof(NETPackage)));
NotifyReceivedFormatPackage(&NotifBuffer);
memcpy(lpBuffer->GetBytes(),
&lpBuffer->GetBytes()[Package->length],
lpBuffer->GetLength() - Package->length);
lpBuffer->m_pData->m_dwDataLength = lpBuffer->GetLength() - Package->length;
}
}
VOID PackageIOCPClient::AsyncProcessSending(){
if(m_ClientContext.m_SendBufferList.Empty() == TRUE && m_ClientContext.m_SendingBytes == NULL){
return;
}
if(m_ClientContext.m_SendingBytes == NULL){
CIOCPBuffer* lpBuffer = m_ClientContext.m_SendBufferList.Pop();
if(lpBuffer){
m_ClientContext.m_SendingBytes = new CIOCPBufferReader(lpBuffer);
OP_DELETE<CIOCPBuffer>(lpBuffer,_FILE_AND_LINE_);
}
}
if(m_ClientContext.m_SendingBytes == NULL){
return;
}
CIOCPBufferReader* lpBufferReader = m_ClientContext.m_SendingBytes;
BYTE* lpData = &lpBufferReader->GetBytes()[lpBufferReader->m_nPosition];
DWORD dwLength = lpBufferReader->GetLength() - lpBufferReader->m_nPosition;
//DumpBuffer(lpData,dwLength);
int SentLength = send(m_ClientContext.sdSocketCopy,(const char*)lpData,dwLength,0);
if(SentLength <= 0){
if(SentLength == -1){
if(WSAGetLastError() == WSAEWOULDBLOCK){
return;
}
}
AsyncProcessDisconnected();
return;
}
lpBufferReader->m_nPosition += SentLength;
if(lpBufferReader->m_nPosition == lpBufferReader->GetLength()){
delete m_ClientContext.m_SendingBytes;
m_ClientContext.m_SendingBytes = NULL;
}
}
VOID PackageIOCPClient::AsyncProcessConnected(){
m_bIsConnected = TRUE;
NotifyConnectionStatus(IOCPClient_ConnectionType_Connected);
}
VOID PackageIOCPClient::AsyncProcessDisconnected(){
if(m_bIsConnected == FALSE){
NotifyConnectionStatus(IOCPClient_ConnectionType_ConnectFailed);
} else {
NotifyConnectionStatus(IOCPClient_ConnectionType_Disconnected);
m_bIsConnected = FALSE;
}
CloseConnect();
}
UINT WINAPI PackageIOCPClient::AsyncProcessThread(LPVOID Param){
fd_set readfds;
fd_set writefds;
fd_set exceptfds;
timeval timeout = {0,0};
PackageIOCPClient* self = (PackageIOCPClient*)Param;
while(self->m_bShutdown == false){
if(self->m_ClientContext.sdSocketCopy != INVALID_SOCKET){
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
#define _SOCKET_FD_CURRENT_ self->m_ClientContext.sdSocketCopy
FD_SET(_SOCKET_FD_CURRENT_,&readfds);
FD_SET(_SOCKET_FD_CURRENT_,&writefds);
FD_SET(_SOCKET_FD_CURRENT_,&exceptfds);
int result = select(_SOCKET_FD_CURRENT_ + 1,&readfds,&writefds,&exceptfds,&timeout);
//what the fuck?
if(result == SOCKET_ERROR){
return -1;
}
self->m_Mutex.Lock();
if(result > 0){
if(FD_ISSET(_SOCKET_FD_CURRENT_,&exceptfds)){
self->AsyncProcessDisconnected();
self->m_Mutex.UnLock();
continue;
}
if(FD_ISSET(_SOCKET_FD_CURRENT_,&writefds)){
if(self->m_bIsConnected == FALSE){
self->AsyncProcessConnected();
}
self->AsyncProcessSending();
}
if(FD_ISSET(_SOCKET_FD_CURRENT_,&readfds)){
self->AsyncProcessReceive();
}
}
self->m_Mutex.UnLock();
#undef _SOCKET_FD_CURRENT_
}
Sleep(30);
}
return 0;
}
BOOL PackageIOCPClient::Connect(char* szHostName,USHORT Port)
{
struct hostent * serverent;
if(m_ClientContext.sdSocketCopy != INVALID_SOCKET){
return FALSE;
}
serverent = gethostbyname(szHostName);
if (serverent == NULL)
{
return FALSE;
}
if(serverent->h_addr_list == NULL)
{
return FALSE;
}
in_addr adr;
memcpy((char *)&adr, (char *)serverent->h_addr, serverent->h_length);
m_ClientContext.sdSocketCopy = GetSockets();
AsyncConnect(adr.S_un.S_addr,Port);
return TRUE;
}
VOID PackageIOCPClient::NotifyConnectionStatus(IOCPClient_ConnectionType ConnectionType)
{
}
VOID PackageIOCPClient::NotifyReceivedFormatPackage(CIOCPBuffer* lpBuffer)
{
}
VOID PackageIOCPClient::Send(CIOCPBuffer buffer){
NETPackage info;
CIOCPBufferWriter Writer;
info.length = buffer.GetLength() + sizeof(NETPackage);
info.title = NET_PACKAGE_TITLE;
Writer.Write(info);
Writer.Write(buffer);
m_Mutex.Lock();
if(m_ClientContext.sdSocketCopy != INVALID_SOCKET){
m_ClientContext.m_SendBufferList.Push(new CIOCPBuffer(Writer.GetBuffer()));
}
m_Mutex.UnLock();
}
VOID PackageIOCPClient::Send(BYTE* Buffer,DWORD Size){
NETPackage info;
CIOCPBufferWriter Writer;
info.length = Size + sizeof(NETPackage);
info.title = NET_PACKAGE_TITLE;
Writer.Write(info);
Writer.Write(Buffer,Size);
m_Mutex.Lock();
if(m_ClientContext.sdSocketCopy != INVALID_SOCKET){
CIOCPBuffer* TempBuffer = new CIOCPBuffer(Writer.GetBuffer());
m_ClientContext.m_SendBufferList.Push(TempBuffer);
}
m_Mutex.UnLock();
}