forked from SomeSupport/eMule
-
Notifications
You must be signed in to change notification settings - Fork 97
/
AsyncSocketExLayer.cpp
584 lines (519 loc) · 15 KB
/
AsyncSocketExLayer.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
/*CAsyncSocketEx by Tim Kosse ([email protected])
Version 1.1 (2002-11-01)
--------------------------------------------------------
Introduction:
-------------
CAsyncSocketEx is a replacement for the MFC class CAsyncSocket.
This class was written because CAsyncSocket is not the fastest WinSock
wrapper and it's very hard to add new functionality to CAsyncSocket
derived classes. This class offers the same functionality as CAsyncSocket.
Also, CAsyncSocketEx offers some enhancements which were not possible with
CAsyncSocket without some tricks.
How do I use it?
----------------
Basically exactly like CAsyncSocket.
To use CAsyncSocketEx, just replace all occurrences of CAsyncSocket in your
code with CAsyncSocketEx, if you did not enhance CAsyncSocket yourself in
any way, you won't have to change anything else in your code.
Why is CAsyncSocketEx faster?
-----------------------------
CAsyncSocketEx is slightly faster when dispatching notification event messages.
First have a look at the way CAsyncSocket works. For each thread that uses
CAsyncSocket, a window is created. CAsyncSocket calls WSAAsyncSelect with
the handle of that window. Until here, CAsyncSocketEx works the same way.
But CAsyncSocket uses only one window message (WM_SOCKET_NOTIFY) for all
sockets within one thread. When the window receive WM_SOCKET_NOTIFY, wParam
contains the socket handle and the window looks up an CAsyncSocket instance
using a map. CAsyncSocketEx works differently. It's helper window uses a
wide range of different window messages (WM_USER through 0xBFFF) and passes
a different message to WSAAsyncSelect for each socket. When a message in
the specified range is received, CAsyncSocketEx looks up the pointer to a
CAsyncSocketEx instance in an Array using the index of message - WM_USER.
As you can see, CAsyncSocketEx uses the helper window in a more efficient
way, as it don't have to use the slow maps to lookup it's own instance.
Still, speed increase is not very much, but it may be noticeable when using
a lot of sockets at the same time.
Please note that the changes do not affect the raw data throughput rate,
CAsyncSocketEx only dispatches the notification messages faster.
What else does CAsyncSocketEx offer?
------------------------------------
CAsyncSocketEx offers a flexible layer system. One example is the proxy layer.
Just create an instance of the proxy layer, configure it and add it to the layer
chain of your CAsyncSocketEx instance. After that, you can connect through
proxies.
Benefit: You don't have to change much to use the layer system.
Another layer that is currently in development is the SSL layer to establish
SSL encrypted connections.
License
-------
Feel free to use this class, as long as you don't claim that you wrote it
and this copyright notice stays intact in the source files.
If you use this class in commercial applications, please send a short message
*/
#include "stdafx.h"
#include "AsyncSocketExLayer.h"
#include "AsyncSocketEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
CAsyncSocketExLayer::CAsyncSocketExLayer()
{
m_pOwnerSocket=0;
m_pNextLayer=0;
m_nLayerState=notsock;
m_nCriticalError=0;
}
CAsyncSocketExLayer::~CAsyncSocketExLayer()
{
}
CAsyncSocketExLayer *CAsyncSocketExLayer::AddLayer(CAsyncSocketExLayer *pLayer, CAsyncSocketEx *pOwnerSocket)
{
ASSERT(pLayer);
ASSERT(pOwnerSocket);
if (m_pNextLayer)
{
return m_pNextLayer->AddLayer(pLayer, pOwnerSocket);
}
else
{
ASSERT(m_pOwnerSocket==pOwnerSocket);
pLayer->Init(this, m_pOwnerSocket);
m_pNextLayer=pLayer;
}
return m_pNextLayer;
}
int CAsyncSocketExLayer::Receive(void* lpBuf, int nBufLen, int nFlags /*=0*/)
{
return ReceiveNext(lpBuf, nBufLen, nFlags);
}
int CAsyncSocketExLayer::Send(const void* lpBuf, int nBufLen, int nFlags /*=0*/)
{
return SendNext(lpBuf, nBufLen, nFlags);
}
void CAsyncSocketExLayer::OnReceive(int nErrorCode)
{
if (m_pPrevLayer)
m_pPrevLayer->OnReceive(nErrorCode);
else
if (m_pOwnerSocket->m_lEvent&FD_READ)
m_pOwnerSocket->OnReceive(nErrorCode);
}
void CAsyncSocketExLayer::OnSend(int nErrorCode)
{
if (m_pPrevLayer)
m_pPrevLayer->OnSend(nErrorCode);
else
if (m_pOwnerSocket->m_lEvent&FD_WRITE)
m_pOwnerSocket->OnSend(nErrorCode);
}
void CAsyncSocketExLayer::OnConnect(int nErrorCode)
{
if (m_pPrevLayer)
m_pPrevLayer->OnConnect(nErrorCode);
else
if (m_pOwnerSocket->m_lEvent&FD_CONNECT)
m_pOwnerSocket->OnConnect(nErrorCode);
}
void CAsyncSocketExLayer::OnAccept(int nErrorCode)
{
if (m_pPrevLayer)
m_pPrevLayer->OnAccept(nErrorCode);
else
if (m_pOwnerSocket->m_lEvent&FD_ACCEPT)
m_pOwnerSocket->OnAccept(nErrorCode);
}
void CAsyncSocketExLayer::OnClose(int nErrorCode)
{
if (m_pPrevLayer)
m_pPrevLayer->OnClose(nErrorCode);
else
if (m_pOwnerSocket->m_lEvent&FD_CLOSE)
m_pOwnerSocket->OnClose(nErrorCode);
}
BOOL CAsyncSocketExLayer::TriggerEvent(long lEvent, int nErrorCode, BOOL bPassThrough /*=FALSE*/ )
{
ASSERT( m_pOwnerSocket );
if (m_pOwnerSocket->m_SocketData.hSocket == INVALID_SOCKET)
return FALSE;
if (lEvent & FD_CONNECT)
{
ASSERT( bPassThrough );
if (nErrorCode == 0)
ASSERT( bPassThrough && GetLayerState() == connected);
else
{
SetLayerState(aborted);
m_nCriticalError = nErrorCode;
}
}
else if (lEvent & FD_CLOSE && nErrorCode == 0)
{
SetLayerState(closed);
}
else if (lEvent & FD_CLOSE && nErrorCode != 0)
{
SetLayerState(aborted);
m_nCriticalError = nErrorCode;
}
ASSERT( m_pOwnerSocket->m_pLocalAsyncSocketExThreadData );
ASSERT( m_pOwnerSocket->m_pLocalAsyncSocketExThreadData->m_pHelperWindow );
ASSERT( m_pOwnerSocket->m_SocketData.nSocketIndex != -1);
t_LayerNotifyMsg *pMsg = new t_LayerNotifyMsg;
pMsg->lEvent = (lEvent & 0xffff) + (nErrorCode << 16);
pMsg->pLayer = bPassThrough ? m_pPrevLayer : this;
BOOL res = PostMessage(m_pOwnerSocket->GetHelperWindowHandle(), WM_SOCKETEX_TRIGGER, (WPARAM)m_pOwnerSocket, (LPARAM)pMsg);
if (!res)
delete pMsg;
return res;
}
void CAsyncSocketExLayer::Close()
{
CloseNext();
}
void CAsyncSocketExLayer::CloseNext()
{
SetLayerState(notsock);
if (m_pNextLayer)
m_pNextLayer->Close();
}
BOOL CAsyncSocketExLayer::Connect(LPCSTR lpszHostAddress, UINT nHostPort)
{
return ConnectNext(lpszHostAddress, nHostPort);
}
BOOL CAsyncSocketExLayer::Connect( const SOCKADDR* lpSockAddr, int nSockAddrLen )
{
return ConnectNext(lpSockAddr, nSockAddrLen);
}
int CAsyncSocketExLayer::SendNext(const void *lpBuf, int nBufLen, int nFlags /*=0*/)
{
if (m_nCriticalError)
{
WSASetLastError(m_nCriticalError);
return SOCKET_ERROR;
}
else if (GetLayerState()==notsock)
{
WSASetLastError(WSAENOTSOCK);
return SOCKET_ERROR;
}
else if (GetLayerState()==unconnected || GetLayerState()==connecting || GetLayerState()==listening)
{
WSASetLastError(WSAENOTCONN);
return SOCKET_ERROR;
}
if (!m_pNextLayer)
{
ASSERT(m_pOwnerSocket);
return send(m_pOwnerSocket->GetSocketHandle(), (LPSTR)lpBuf, nBufLen, nFlags);
}
else
return m_pNextLayer->Send(lpBuf, nBufLen, nFlags);
}
int CAsyncSocketExLayer::ReceiveNext(void *lpBuf, int nBufLen, int nFlags /*=0*/)
{
if (m_nCriticalError)
{
WSASetLastError(m_nCriticalError);
return SOCKET_ERROR;
}
else if (GetLayerState()==notsock)
{
WSASetLastError(WSAENOTSOCK);
return SOCKET_ERROR;
}
else if (GetLayerState()==unconnected || GetLayerState()==connecting || GetLayerState()==listening)
{
WSASetLastError(WSAENOTCONN);
return SOCKET_ERROR;
}
if (!m_pNextLayer)
{
ASSERT(m_pOwnerSocket);
return recv(m_pOwnerSocket->GetSocketHandle(), (LPSTR)lpBuf, nBufLen, nFlags);
}
else
return m_pNextLayer->Receive(lpBuf, nBufLen, nFlags);
}
BOOL CAsyncSocketExLayer::ConnectNext(LPCSTR lpszHostAddress, UINT nHostPort)
{
ASSERT( lpszHostAddress != NULL );
ASSERT( GetLayerState() == unconnected );
ASSERT( m_pOwnerSocket );
BOOL res;
if (m_pNextLayer)
res = m_pNextLayer->Connect(lpszHostAddress, nHostPort);
else
{
SOCKADDR_IN sockAddr = {0};
sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress);
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
LPHOSTENT lphost = gethostbyname(lpszHostAddress);
if (lphost == NULL) {
WSASetLastError(WSAEINVAL);
return FALSE;
}
sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
}
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons((u_short)nHostPort);
res = (connect(m_pOwnerSocket->GetSocketHandle(), (SOCKADDR*)&sockAddr, sizeof(sockAddr)) != SOCKET_ERROR);
}
if (res || WSAGetLastError() == WSAEWOULDBLOCK)
SetLayerState(connecting);
return res;
}
BOOL CAsyncSocketExLayer::ConnectNext(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
ASSERT( GetLayerState() == unconnected );
ASSERT( m_pOwnerSocket );
BOOL res;
if (m_pNextLayer)
res = m_pNextLayer->Connect(lpSockAddr, nSockAddrLen);
else
res = (connect(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, nSockAddrLen) != SOCKET_ERROR);
if (res || WSAGetLastError() == WSAEWOULDBLOCK)
SetLayerState(connecting);
return res;
}
//Gets the address of the peer socket to which the socket is connected
#ifdef _AFX
BOOL CAsyncSocketExLayer::GetPeerName( CString& rPeerAddress, UINT& rPeerPort )
{
return GetPeerNameNext(rPeerAddress, rPeerPort);
}
BOOL CAsyncSocketExLayer::GetPeerNameNext( CString& rPeerAddress, UINT& rPeerPort )
{
if (m_pNextLayer)
return m_pNextLayer->GetPeerName(rPeerAddress, rPeerPort);
else
{
ASSERT(m_pOwnerSocket);
SOCKADDR_IN sockAddr = {0};
int nSockAddrLen = sizeof(sockAddr);
if (!getpeername(m_pOwnerSocket->GetSocketHandle(), (SOCKADDR*)&sockAddr, &nSockAddrLen))
{
rPeerPort = ntohs(sockAddr.sin_port);
rPeerAddress = inet_ntoa(sockAddr.sin_addr);
return TRUE;
}
else
return FALSE;
}
}
#endif //_AFX
BOOL CAsyncSocketExLayer::GetPeerName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
return GetPeerNameNext(lpSockAddr, lpSockAddrLen);
}
BOOL CAsyncSocketExLayer::GetPeerNameNext( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
if (m_pNextLayer)
return m_pNextLayer->GetPeerName(lpSockAddr, lpSockAddrLen);
else
{
ASSERT(m_pOwnerSocket);
if ( !getpeername(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, lpSockAddrLen) )
return TRUE;
else
return FALSE;
}
}
void CAsyncSocketExLayer::Init(CAsyncSocketExLayer *pPrevLayer, CAsyncSocketEx *pOwnerSocket)
{
ASSERT(pOwnerSocket);
m_pPrevLayer=pPrevLayer;
m_pOwnerSocket=pOwnerSocket;
m_pNextLayer=0;
}
int CAsyncSocketExLayer::GetLayerState()
{
return m_nLayerState;
}
void CAsyncSocketExLayer::CallEvent(int nEvent, int nErrorCode)
{
if (m_nCriticalError)
return;
m_nCriticalError=nErrorCode;
switch (nEvent)
{
case FD_READ:
case FD_FORCEREAD:
if (GetLayerState()==connected)
{
if (nErrorCode)
SetLayerState(aborted);
OnReceive(nErrorCode);
}
break;
case FD_WRITE:
if (GetLayerState()==connected)
{
if (nErrorCode)
SetLayerState(aborted);
OnSend(nErrorCode);
}
break;
case FD_CONNECT:
if (GetLayerState()==connecting)
{
if (!nErrorCode)
SetLayerState(connected);
else
SetLayerState(aborted);
OnConnect(nErrorCode);
}
break;
case FD_ACCEPT:
if (GetLayerState()==listening)
{
if (!nErrorCode)
SetLayerState(connected);
else
SetLayerState(aborted);
OnAccept(nErrorCode);
}
break;
case FD_CLOSE:
if (GetLayerState()==connected)
{
if (nErrorCode)
SetLayerState(aborted);
else
SetLayerState(closed);
OnClose(nErrorCode);
}
break;
}
}
BOOL CAsyncSocketExLayer::Create(UINT nSocketPort, int nSocketType, long lEvent, LPCSTR lpszSocketAddress)
{
return CreateNext(nSocketPort, nSocketType, lEvent, lpszSocketAddress);
}
BOOL CAsyncSocketExLayer::CreateNext(UINT nSocketPort, int nSocketType, long lEvent, LPCSTR lpszSocketAddress)
{
ASSERT(GetLayerState()==notsock);
BOOL res=FALSE;
if (m_pNextLayer)
res=m_pNextLayer->Create(nSocketPort, nSocketType, lEvent, lpszSocketAddress);
else
{
SOCKET hSocket=socket(AF_INET, nSocketType, 0);
if (hSocket==INVALID_SOCKET)
res=FALSE;
m_pOwnerSocket->m_SocketData.hSocket=hSocket;
m_pOwnerSocket->AttachHandle(hSocket);
if (!m_pOwnerSocket->AsyncSelect(lEvent))
{
m_pOwnerSocket->Close();
res=FALSE;
}
if (m_pOwnerSocket->m_pFirstLayer)
{
if (WSAAsyncSelect(m_pOwnerSocket->m_SocketData.hSocket, m_pOwnerSocket->GetHelperWindowHandle(), m_pOwnerSocket->m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE) )
{
m_pOwnerSocket->Close();
res=FALSE;
}
}
if (!m_pOwnerSocket->Bind(nSocketPort, lpszSocketAddress))
{
m_pOwnerSocket->Close();
res=FALSE;
}
res=TRUE;
}
if (res)
SetLayerState(unconnected);
return res;
}
void CAsyncSocketExLayer::SetLayerState(int nLayerState)
{
ASSERT( m_pOwnerSocket );
int nOldLayerState = GetLayerState();
m_nLayerState = nLayerState;
if (nOldLayerState != nLayerState)
DoLayerCallback(LAYERCALLBACK_STATECHANGE, GetLayerState(), nOldLayerState);
}
int CAsyncSocketExLayer::DoLayerCallback(int nType, int nCode, WPARAM wParam, LPARAM lParam)
{
ASSERT(m_pOwnerSocket);
int nError = WSAGetLastError();
int res = m_pOwnerSocket->OnLayerCallback(this, nType, nCode, wParam, lParam);
WSASetLastError(nError);
return res;
}
BOOL CAsyncSocketExLayer::Listen( int nConnectionBacklog)
{
return ListenNext( nConnectionBacklog);
}
BOOL CAsyncSocketExLayer::ListenNext( int nConnectionBacklog)
{
ASSERT(GetLayerState()==unconnected);
BOOL res;
if (m_pNextLayer)
res=m_pNextLayer->Listen(nConnectionBacklog);
else
res=listen(m_pOwnerSocket->GetSocketHandle(), nConnectionBacklog);
if (res)
{
SetLayerState(listening);
}
return res;
}
BOOL CAsyncSocketExLayer::Accept( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr /*=NULL*/, int* lpSockAddrLen /*=NULL*/ )
{
return AcceptNext(rConnectedSocket, lpSockAddr, lpSockAddrLen);
}
BOOL CAsyncSocketExLayer::AcceptNext( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr /*=NULL*/, int* lpSockAddrLen /*=NULL*/ )
{
ASSERT(GetLayerState()==listening);
BOOL res;
if (m_pNextLayer)
res=m_pNextLayer->Accept(rConnectedSocket, lpSockAddr, lpSockAddrLen);
else
{
SOCKET hTemp = accept(m_pOwnerSocket->m_SocketData.hSocket, lpSockAddr, lpSockAddrLen);
if (hTemp == INVALID_SOCKET)
return FALSE;
VERIFY(rConnectedSocket.InitAsyncSocketExInstance());
rConnectedSocket.m_SocketData.hSocket=hTemp;
rConnectedSocket.AttachHandle(hTemp);
}
return TRUE;
}
BOOL CAsyncSocketExLayer::ShutDown(int nHow /*=sends*/)
{
return ShutDownNext(nHow);
}
BOOL CAsyncSocketExLayer::ShutDownNext(int nHow /*=sends*/)
{
if (m_nCriticalError)
{
WSASetLastError(m_nCriticalError);
return FALSE;
}
else if (GetLayerState()==notsock)
{
WSASetLastError(WSAENOTSOCK);
return FALSE;
}
else if (GetLayerState()==unconnected || GetLayerState()==connecting || GetLayerState()==listening)
{
WSASetLastError(WSAENOTCONN);
return FALSE;
}
if (!m_pNextLayer)
{
ASSERT(m_pOwnerSocket);
return shutdown(m_pOwnerSocket->GetSocketHandle(), nHow);
}
else
return m_pNextLayer->ShutDownNext(nHow);
}