# include
# include
# include
# include
# include
using namespace std;
# define RELEASE (x) {if (x! X=NULL) {delete; X=NULL; }}
# define RELEASE_HANDLE (x) {if (x!=NULL & amp; & X!!! INVALID_HANDLE_VALUE)={CloseHandle (x); X=NULL; }}
# define RELEASE_SOCKET (x) {if (x!=INVALID_SOCKET) {closesocket (x); X=INVALID_SOCKET; }}
Template
Void RemoveAt (t vec, int num)
{
T: : iterator it=vec. The begin () + num.
Vec. Erase (it);
}
Typedef enum _OPERATION_TYPE
{
ACCEPT_POSTED,
SEND_POSTED,
RECV_POSTED,
NULL_POSTED
} OPERATION_TYPE;
Typedef struct _PER_IO_CONTEXT
{
OVERLAPPED m_Overlapped;
The SOCKET m_sockAccept;
WSABUF m_wsaBuf;
Char m_szBuffer [8192].
OPERATION_TYPE m_OpType;
_PER_IO_CONTEXT ()
{
ZeroMemory (& amp; M_Overlapped, sizeof (m_Overlapped));
ZeroMemory (m_szBuffer, 8192);
M_sockAccept=INVALID_SOCKET;
M_wsaBuf. Buf=m_szBuffer;
M_wsaBuf. Len=8192;
M_OpType=NULL_POSTED;
}
~ _PER_IO_CONTEXT ()
{
If (m_sockAccept!=INVALID_SOCKET)
{
Closesocket (m_sockAccept);
M_sockAccept=INVALID_SOCKET;
}
}
Void ResetBuffer ()
{
ZeroMemory (m_szBuffer, 8192);
}
} PER_IO_CONTEXT;
Typedef struct _PER_SOCKET_CONTEXT
{
The SOCKET m_Socket;
SOCKADDR_IN m_ClientAddr;
Vector<_PER_IO_CONTEXT * & gt; M_arrayIoContext;
_PER_SOCKET_CONTEXT ()
{
M_Socket=INVALID_SOCKET;
Memset (& amp; M_ClientAddr, 0, sizeof (m_ClientAddr));
}
~ _PER_SOCKET_CONTEXT ()
{
If (m_Socket!=INVALID_SOCKET)
{
Closesocket (m_Socket);
M_Socket=INVALID_SOCKET;
}
for (int i=0; i
The delete m_arrayIoContext [I];
}
M_arrayIoContext. The clear ();
}
_PER_IO_CONTEXT * GetNewIoContext ()
{
_PER_IO_CONTEXT * p=new _PER_IO_CONTEXT;
M_arrayIoContext. Push_back (p);
return p;
}
Void RemoveContext (_PER_IO_CONTEXT * pContext)
{
Assert (pContext!=NULL);
for (int i=0; I & lt; M_arrayIoContext. The size (); I++)
{
If (pContext==m_arrayIoContext [I])
{
The delete pContext;
PContext=NULL;
RemoveAt (m_arrayIoContext, I);
break;
}
}
}
} PER_SOCKET_CONTEXT;
Class C
{
Public:
HANDLE m_hShutdownEvent;
HANDLE m_hIOCompletionPort;
HANDLE * m_phWorkerThreads;
Int m_nThreads;
String m_strIP;
Int m_nPort;
CRITICAL_SECTION m_csContextList;
Vector
LPFN_ACCEPTEX m_lpfnAcceptEx;
LPFN_GETACCEPTEXSOCKADDRS m_lpfnGetAcceptExSockAddrs;
} C;
Int _GetNoOfProcessors ()
{
SYSTEM_INFO si;
GetSystemInfo (& amp; Si);
Return si. DwNumberOfProcessors + 1;
}
Bool _IsSocketAlive (SOCKET s)
{
Int nByteSent=send (s, "", 0, 0).
If (1==nByteSent)
{
return false;
}
return true;
}
Bool _PostSend (PER_IO_CONTEXT * pIoContext, char * buf)
{
Size_t leng=strlen (buf);
DWORD dwBytes=0;
WSABUF p_wbuf;
P_wbuf. Buf=new char [leng];
P_wbuf. Len=leng;
Memcpy (p_wbuf buf, buf, sizeof (char) * leng);
OVERLAPPED * p_ol=& amp; PIoContext - & gt; M_Overlapped;
PIoContext - & gt; M_OpType=SEND_POSTED;
If (SOCKET_ERROR==WSASend (pIoContext - & gt; M_sockAccept, & amp; P_wbuf, 1, & amp; DwBytes, NULL, p_ol, NULL) & amp; & WSA_IO_PENDING!=WSAGetLastError ())
{
return false;
}
return true;
}
Void _DoSend (PER_SOCKET_CONTEXT * pSocketContext, PER_IO_CONTEXT * pIoContext)
{
The EnterCriticalSection (& amp; C. _csContextList);
PSocketContext - & gt; RemoveContext (pIoContext);
LeaveCriticalSection (& amp; C. _csContextList);
}
Bool _DoRecv (PER_IO_CONTEXT * pIoContext)
{
DWORD dwFlags=0;
DWORD dwBytes=0;
WSABUF * p_wbuf=& amp; PIoContext - & gt; M_wsaBuf;
OVERLAPPED * p_ol=& amp; PIoContext - & gt; M_Overlapped;
Cout & lt; <(* p_wbuf). Buf;
PIoContext - & gt; ResetBuffer ();
PIoContext - & gt; M_OpType=RECV_POSTED;
If (SOCKET_ERROR==WSARecv (pIoContext - & gt; M_sockAccept, p_wbuf, 1, & amp; DwBytes, & amp; DwFlags p_ol, NULL) & amp; & WSA_IO_PENDING!=WSAGetLastError ())
{
return false;
}
return true;
}
Void _AddToContextList (PER_SOCKET_CONTEXT * pHandleData)
{
The EnterCriticalSection (& amp; C. _csContextList);
C. _arrayClientContext. Push_back (pHandleData);
LeaveCriticalSection (& amp; C. _csContextList);
}
Void _RemoveContext (PER_SOCKET_CONTEXT * pSocketContext)
{
The EnterCriticalSection (& amp; C. _csContextList);
for (int i=0; i