欢迎阅读此篇IOCP教程。我将先给出IOCP的定义然后给出它的实现方法,最后剖析一个Echo程序来为您拨开IOCP的谜云,除去你心中对IOCP的烦恼。OK,但我不能保证你明白IOCP的一切,但我会尽我最大的努力。以下是我会在这篇文章中提到的相关技术:
I/O端口
同步/异步
堵塞/非堵塞
服务端/客户端
多线程程序设计
Winsock API 2.0
在这之前,我曾经开发过一个项目,其中一块需要网络支持,当时还考虑到了代码的可移植性,只要使用select,connect,accept,listen,send还有recv,再加上几个#ifdef的封装以用来处理Winsock和BSD套接字[socket]中间的不兼容性,一个网络子系统只用了几个小时很少的代码就写出来了,至今还让我很回味。那以后很长时间也就没再碰了。
前些日子,我们策划做一个网络游戏,我主动承担下网络这一块,想想这还不是小case,心里偷着乐啊。网络游戏好啊,网络游戏为成百上千的玩家提供了乐趣和令人着秘的游戏体验,他们在线上互相战斗或是加入队伍去战胜共同的敌人。我信心满满的准备开写我的网络,于是乎,发现过去的阻塞同步模式模式根本不能拿到一个巨量多玩家[MMP]的架构中去,直接被否定掉了。于是乎,就有了IOCP,如果能过很轻易而举的搞掂IOCP,也就不会有这篇教程了。下面请诸位跟随我进入正题。
什么是IOCP?
先让我们看看对IOCP的评价
I/O完成端口可能是Win32提供的最复杂的内核对象。
[Advanced Windows 3rd] Jeffrey Richter
这是[IOCP]实现高容量网络服务器的最佳方法。
[Windows Sockets2.0:Write Scalable Winsock Apps Using Completion Ports]
Microsoft Corporation
完成端口模型提供了最好的伸缩性。这个模型非常适用来处理数百乃至上千个套接字。
[Windows网络编程2nd] Anthony Jones & Jim Ohlund
I/O completion ports特别显得重要,因为它们是唯一适用于高负载服务器[必须同时维护许多连接线路]的一个技术。Completion ports利用一些线程,帮助平衡由I/O请求所引起的负载。这样的架构特别适合用在SMP系统中产生的”scalable”服务器。
[Win32多线程程序设计] Jim Beveridge & Robert Wiener
看来我们完全有理由相信IOCP是大型网络架构的首选。那IOCP到底是什么呢?
微软在Winsock2中引入了IOCP这一概念 。IOCP全称I/O Completion Port,中文译为I/O完成端口。IOCP是一个异步I/O的API,它可以高效地将I/O事件通知给应用程序。与使用select()或是其它异步方法不同的是,一个套接字[socket]与一个完成端口关联了起来,然后就可继续进行正常的Winsock操作了。然而,当一个事件发生的时候,此完成端口就将被操作系统加入一个队列中。然后应用程序可以对核心层进行查询以得到此完成端口。
这里我要对上面的一些概念略作补充,在解释[完成]两字之前,我想先简单的提一下同步和异步这两个概念,逻辑上来讲做完一件事后再去做另一件事就是同步,而同时一起做两件或两件以上事的话就是异步了。你也可以拿单线程和多线程来作比喻。但是我们一定要将同步和堵塞,异步和非堵塞区分开来,所谓的堵塞函数诸如accept(…),当调用此函数后,此时线程将挂起,直到操作系统来通知它,”HEY兄弟,有人连进来了”,那个挂起的线程将继续进行工作,也就符合”生产者-消费者”模型。堵塞和同步看上去有两分相似,但却是完全不同的概念。大家都知道I/O设备是个相对慢速的设备,不论打印机,调制解调器,甚至硬盘,与CPU相比都是奇慢无比的,坐下来等I/O的完成是一件不甚明智的事情,有时候数据的流动率非常惊人,把数据从你的文件服务器中以Ethernet速度搬走,其速度可能高达每秒一百万字节,如果你尝试从文件服务器中读取100KB,在用户的眼光来看几乎是瞬间完成,但是,要知道,你的线程执行这个命令,已经浪费了10个一百万次CPU周期。所以说,我们一般使用另一个线程来进行I/O。重叠IO[overlapped I/O]是Win32的一项技术,你可以要求操作系统为你传送数据,并且在传送完毕时通知你。这也就是[完成]的含义。这项技术使你的程序在I/O进行过程中仍然能够继续处理事务。事实上,操作系统内部正是以线程来完成overlapped I/O。你可以获得线程所有利益,而不需要付出什么痛苦的代价。
完成端口中所谓的[端口]并不是我们在TCP/IP中所提到的端口,可以说是完全没有关系。我到现在也没想通一个I/O设备[I/O Device]和端口[IOCP中的Port]有什么关系。估计这个端口也迷惑了不少人。IOCP只不过是用来进行读写操作,和文件I/O倒是有些类似。既然是一个读写设备,我们所能要求它的只是在处理读与写上的高效。在文章的第三部分你会轻而易举的发现IOCP设计的真正用意。
IOCP和网络又有什么关系?
int main()
{
WSAStartup(MAKEWORD(2, 2), &wsaData);
ListeningSocket = socket(AF_INET, SOCK_STREAM, 0);
bind(ListeningSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));
listen(ListeningSocket, 5);
int nlistenAddrLen = sizeof(ClientAddr);
while(TRUE)
{
NewConnection = accept(ListeningSocket, (SOCKADDR*)&ClientAddr, &nlistenAddrLen);
HANDLE hThread = CreateThread(NULL, 0, ThreadFunc, (void*) NewConnection, 0, &dwTreadId);
CloseHandle(hThread);
}
return 0;
}
相信只要写过网络的朋友,应该对这样的结构在熟悉不过了。accept后线程被挂起,等待一个客户发出请求,而后创建新线程来处理请求。当新线程处理客户请求时,起初的线程循环回去等待另一个客户请求。处理客户请求的线程处理完毕后终结。
在上述的并发模型中,对每个客户请求都创建了一个线程。其优点在于等待请求的线程只需做很少的工作。大多数时间中,该线程在休眠[因为recv处于堵塞状态]。
但是当并发模型应用在服务器端[基于Windows NT],Windows NT小组注意到这些应用程序的性能没有预料的那么高。特别的,处理很多同时的客户请求意味着很多线程并发地运行在系统中。因为所有这些线程都是可运行的[没有被挂起和等待发生什么事],Microsoft意识到NT内核花费了太多的时间来转换运行线程的上下文[Context],线程就没有得到很多CPU时间来做它们的工作。
大家可能也都感觉到并行模型的瓶颈在于它为每一个客户请求都创建了一个新线程。创建线程比起创建进程开销要小,但也远不是没有开销的。
我们不妨设想一下:如果事先开好N个线程,让它们在那hold[堵塞],然后可以将所有用户的请求都投递到一个消息队列中去。然后那N个线程逐一从消息队列中去取出消息并加以处理。就可以避免针对每一个用户请求都开线程。不仅减少了线程的资源,也提高了线程的利用率。理论上很不错,你想我等泛泛之辈都能想出来的问题,Microsoft又怎会没有考虑到呢?!
这个问题的解决方法就是一个称为I/O完成端口的内核对象,他首次在Windows NT3.5中被引入。
其实我们上面的构想应该就差不多是IOCP的设计机理。其实说穿了IOCP不就是一个消息队列嘛!你说这和[端口]这两字有何联系。我的理解就是IOCP最多是应用程序和操作系统沟通的一个接口罢了。
至于IOCP的具体设计那我也很难说得上来,毕竟我没看过实现的代码,但你完全可以进行模拟,只不过性能可能…,如果想深入理解IOCP, Jeffrey Ritchter的Advanced Windows 3rd其中第13章和第14张有很多宝贵的内容,你可以拿来窥视一下系统是如何完成这一切的。
实现方法
Microsoft为IOCP提供了相应的API函数,主要的就两个,我们逐一的来看一下:
HANDLE CreateIoCompletionPort (
HANDLE FileHandle, // handle to file
HANDLE ExistingCompletionPort, // handle to I/O completion port
ULONG_PTR CompletionKey, // completion key
DWORD NumberOfConcurrentThreads // number of threads to execute concurrently
);
在讨论各参数之前,首先要注意该函数实际用于两个截然不同的目的:
1.用于创建一个完成端口对象
2.将一个句柄[HANDLE]和完成端口关联到一起
在创建一个完成一个端口的时候,我们只需要填写一下NumberOfConcurrentThreads这个参数就可以了。它告诉系统一个完成端口上同时允许运行的线程最大数。在默认情况下,所开线程数和CPU数量相同,但经验给我们一个公式:
线程数 = CPU数 * 2 + 2
要使完成端口有用,你必须把它同一个或多个设备相关联。这也是调用CreateIoCompletionPort完成的。你要向该函数传递一个已有的完成端口的句柄,我们既然要处理网络事件,那也就是将客户的socket作为HANDLE传进去。和一个完成键[对你有意义的一个32位值,也就是一个指针,操作系统并不关心你传什么]。每当你向端口关联一个设备时,系统向该完成端口的设备列表中加入一条信息纪录。
另一个API就是
BOOL GetQueuedCompletionStatus(
HANDLE CompletionPort, // handle to completion port
LPDWORD lpNumberOfBytes, // bytes transferred
PULONG_PTR lpCompletionKey, // file completion key
LPOVERLAPPED *lpOverlapped, // buffer
DWORD dwMilliseconds // optional timeout value
);
第一个参数指出了线程要监视哪一个完成端口。很多服务应用程序只是使用一个I/O完成端口,所有的I/O请求完成以后的通知都将发给该端口。简单的说,GetQueuedCompletionStatus使调用线程挂起,直到指定的端口的I/O完成队列中出现了一项或直到超时。同I/O完成端口相关联的第3个数据结构是使线程得到完成I/O项中的信息:传输的字节数,完成键和OVERLAPPED结构的地址。该信息是通过传递给GetQueuedCompletionSatatus的lpdwNumberOfBytesTransferred,lpdwCompletionKey和lpOverlapped参数返回给线程的。
PostQueuedCompletionStatus Function
Posts an I/O completion packet to an I/O completion port.
BOOL WINAPI PostQueuedCompletionStatus(
__in HANDLE CompletionPort,
__in DWORD dwNumberOfBytesTransferred,
__in ULONG_PTR dwCompletionKey,
__in LPOVERLAPPED lpOverlapped
);
Parameters
CompletionPort
A handle to an I/O completion port to which the I/O completion packet is to be posted.
dwNumberOfBytesTransferred
The value to be returned through the lpNumberOfBytesTransferred parameter of the GetQueuedCompletionStatus function.
dwCompletionKey
The value to be returned through the lpCompletionKey parameter of the GetQueuedCompletionStatus function.
lpOverlapped
The value to be returned through the lpOverlapped parameter of the GetQueuedCompletionStatus function.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError .
Remarks
The I/O completion packet will satisfy an outstanding call to the GetQueuedCompletionStatus function. This function returns with the three values passed as the second, third, and fourth parameters of the call to PostQueuedCompletionStatus. The system does not use or validate these values. In particular, the lpOverlapped parameter need not point to an OVERLAPPED structure.
Requirements
Client
Requires Windows Vista, Windows XP, or Windows 2000 Professional.
Server
Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header
Declared in WinBase.h; include Windows.h.
Library
Use Kernel32.lib.
DLL
Requires Kernel32.dll.
根据到目前为止已经讲到的东西,首先来构建一个frame。下面为您说明了如何使用完成端口来开发一个echo服务器。大致如下:
1.初始化Winsock
2.创建一个完成端口
3.根据服务器线程数创建一定量的线程数
4.准备好一个socket进行bind然后listen
5.进入循环accept等待客户请求
6.创建一个数据结构容纳socket和其他相关信息
7.将连进来的socket同完成端口相关联
8.投递一个准备接受的请求
以后就不断的重复5至8的过程
那好,我们用具体的代码来展示一下细节的操作。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <winsock2.h>
#include <vector>


int main(int argc, char *argv[])
{
//Validate the input
// if (argc < 2)
// {
// printf("\nUsage: %s port.", argv[0]);
// return 1;
// }

if (false == Initialize())
{
return 1;
}

SOCKET ListenSocket;

struct sockaddr_in ServerAddress;

//Overlapped I/O follows the model established in Windows and can be performed only on
//sockets created through the WSASocket function
ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);

if (INVALID_SOCKET == ListenSocket)
{
printf("\nError occurred while opening socket: %d.", WSAGetLastError());
goto error;
}
else
{
printf("\nWSASocket() successful.");
}

//Cleanup and Init with 0 the ServerAddress
ZeroMemory((char *)&ServerAddress, sizeof(ServerAddress));

//Port number will be supplied as a command line argument
int nPortNo;
nPortNo = 52211;

//Fill up the address structure
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_addr.s_addr = INADDR_ANY; //WinSock will supply address
ServerAddress.sin_port = htons(nPortNo); //comes from commandline

//Assign local address and port number
if (SOCKET_ERROR == bind(ListenSocket, (struct sockaddr *) &ServerAddress, sizeof(ServerAddress)))
{
closesocket(ListenSocket);
printf("\nError occurred while binding.");
goto error;
}
else
{
printf("\nbind() successful.");
}

//Make the socket a listening socket
if (SOCKET_ERROR == listen(ListenSocket,SOMAXCONN))
{
closesocket(ListenSocket);
printf("\nError occurred while listening.");
goto error;
}
else
{
printf("\nlisten() successful.");
}

g_hAcceptEvent = WSACreateEvent();

if (WSA_INVALID_EVENT == g_hAcceptEvent)
{
printf("\nError occurred while WSACreateEvent().");
goto error;
}

if (SOCKET_ERROR == WSAEventSelect(ListenSocket, g_hAcceptEvent, FD_ACCEPT))
{
printf("\nError occurred while WSAEventSelect().");
WSACloseEvent(g_hAcceptEvent);
goto error;
}

printf("\nTo exit this server, hit a key at any time on this console
");

DWORD nThreadID;
g_hAcceptThread = CreateThread(0, 0, AcceptThread, (void *)ListenSocket, 0, &nThreadID);

//Hang in there till a key is hit
while(!_kbhit())
{
Sleep(0); //switch to some other thread
}

WriteToConsole("\nServer is shutting down
");

//Start cleanup
CleanUp();

//Close open sockets
closesocket(ListenSocket);

DeInitialize();

return 0; //success

error:
closesocket(ListenSocket);
DeInitialize();
return 1;
}

bool Initialize()
{
//Find out number of processors and threads
g_nThreads = WORKER_THREADS_PER_PROCESSOR * GetNoOfProcessors();

printf("\nNumber of processors on host: %d", GetNoOfProcessors());

printf("\nThe following number of worker threads will be created: %d", g_nThreads);

//Allocate memory to store thread handless
g_phWorkerThreads = new HANDLE[g_nThreads];

//Initialize the Console Critical Section
InitializeCriticalSection(&g_csConsole);

//Initialize the Client List Critical Section
InitializeCriticalSection(&g_csClientList);

//Create shutdown event
g_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

// Initialize Winsock
WSADATA wsaData;

int nResult;
nResult = WSAStartup(MAKEWORD(2,2), &wsaData);

if (NO_ERROR != nResult)
{
printf("\nError occurred while executing WSAStartup().");
return false; //error
}
else
{
printf("\nWSAStartup() successful.");
}

if (false == InitializeIOCP())
{
printf("\nError occurred while initializing IOCP");
return false;
}
else
{
printf("\nIOCP initialization successful.");
}

return true;
}

//Function to Initialize IOCP
bool InitializeIOCP()
{
//Create I/O completion port
g_hIOCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0 );

if ( NULL == g_hIOCompletionPort)
{
printf("\nError occurred while creating IOCP: %d.", WSAGetLastError());
return false;
}

DWORD nThreadID;

//Create worker threads
for (int ii = 0; ii < g_nThreads; ii++)
{
g_phWorkerThreads[ii] = CreateThread(0, 0, WorkerThread, (void *)(ii+1), 0, &nThreadID);
}

return true;
}

void CleanUp()
{
//Ask all threads to start shutting down
SetEvent(g_hShutdownEvent);

//Let Accept thread go down
WaitForSingleObject(g_hAcceptThread, INFINITE);

for (int i = 0; i < g_nThreads; i++)
{
//Help threads get out of blocking - GetQueuedCompletionStatus()
PostQueuedCompletionStatus(g_hIOCompletionPort, 0, (DWORD) NULL, NULL);
}

//Let Worker Threads shutdown
WaitForMultipleObjects(g_nThreads, g_phWorkerThreads, TRUE, INFINITE);

//We are done with this event
WSACloseEvent(g_hAcceptEvent);

//Cleanup dynamic memory allocations, if there are any.
CleanClientList();
}

void DeInitialize()
{
//Delete the Console Critical Section.
DeleteCriticalSection(&g_csConsole);

//Delete the Client List Critical Section.
DeleteCriticalSection(&g_csClientList);

//Cleanup IOCP.
CloseHandle(g_hIOCompletionPort);

//Clean up the event.
CloseHandle(g_hShutdownEvent);

//Clean up memory allocated for the storage of thread handles
delete[] g_phWorkerThreads;

//Cleanup Winsock
WSACleanup();
}

//This thread will look for accept event
DWORD WINAPI AcceptThread(LPVOID lParam)
{
SOCKET ListenSocket = (SOCKET)lParam;

WSANETWORKEVENTS WSAEvents;

//Accept thread will be around to look for accept event, until a Shutdown event is not Signaled.
while(WAIT_OBJECT_0 != WaitForSingleObject(g_hShutdownEvent, 0))
{
if (WSA_WAIT_TIMEOUT != WSAWaitForMultipleEvents(1, &g_hAcceptEvent, FALSE, WAIT_TIMEOUT_INTERVAL, FALSE))
{
WSAEnumNetworkEvents(ListenSocket, g_hAcceptEvent, &WSAEvents);
if ((WSAEvents.lNetworkEvents & FD_ACCEPT) && (0 == WSAEvents.iErrorCode[FD_ACCEPT_BIT]))
{
//Process it
AcceptConnection(ListenSocket);
}
}
}

return 0;
}

//This function will process the accept event
void AcceptConnection(SOCKET ListenSocket)
{
sockaddr_in ClientAddress;
int nClientLength = sizeof(ClientAddress);

//Accept remote connection attempt from the client
SOCKET Socket = accept(ListenSocket, (sockaddr*)&ClientAddress, &nClientLength);

if (INVALID_SOCKET == Socket)
{
WriteToConsole("\nError occurred while accepting socket: %ld.", WSAGetLastError());
}

//Display Client's IP
WriteToConsole("\nClient connected from: %s", inet_ntoa(ClientAddress.sin_addr));

//Create a new ClientContext for this newly accepted client
CClientContext *pClientContext = new CClientContext;

pClientContext->SetOpCode(OP_READ);
pClientContext->SetSocket(Socket);

//Store this object
AddToClientList(pClientContext);

if (true == AssociateWithIOCP(pClientContext))
{
//Once the data is successfully received, we will print it.
pClientContext->SetOpCode(OP_WRITE);

WSABUF *p_wbuf = pClientContext->GetWSABUFPtr();
OVERLAPPED *p_ol = pClientContext->GetOVERLAPPEDPtr();

//Get data.
DWORD dwFlags = 0;
DWORD dwBytes = 0;

//Post initial Recv
//This is a right place to post a initial Recv
//Posting a initial Recv in WorkerThread will create scalability issues.
int nBytesRecv = WSARecv(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);

if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
WriteToConsole("\nError in Initial Post.");
}
}
}

bool AssociateWithIOCP(CClientContext *pClientContext)
{
//Associate the socket with IOCP
HANDLE hTemp = CreateIoCompletionPort((HANDLE)pClientContext->GetSocket(), g_hIOCompletionPort, (DWORD)pClientContext, 0);

if (NULL == hTemp)
{
WriteToConsole("\nError occurred while executing CreateIoCompletionPort().");

//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);

return false;
}

return true;
}

//Worker thread will service IOCP requests
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
int nThreadNo = (int)lpParam;

void *lpContext = NULL;
OVERLAPPED *pOverlapped = NULL;
CClientContext *pClientContext = NULL;
DWORD dwBytesTransfered = 0;
int nBytesRecv = 0;
int nBytesSent = 0;
DWORD dwBytes = 0, dwFlags = 0;

//Worker thread will be around to process requests, until a Shutdown event is not Signaled.
while (WAIT_OBJECT_0 != WaitForSingleObject(g_hShutdownEvent, 0))
{
BOOL bReturn = GetQueuedCompletionStatus(
g_hIOCompletionPort,
&dwBytesTransfered,
(LPDWORD)&lpContext,
&pOverlapped,
INFINITE);

if (NULL == lpContext)
{
//We are shutting down
break;
}

//Get the client context
pClientContext = (CClientContext *)lpContext;

if ((FALSE == bReturn) || ((TRUE == bReturn) && (0 == dwBytesTransfered)))
{
//Client connection gone, remove it.
RemoveFromClientListAndFreeMemory(pClientContext);
continue;
}

WSABUF *p_wbuf = pClientContext->GetWSABUFPtr();
OVERLAPPED *p_ol = pClientContext->GetOVERLAPPEDPtr();

switch (pClientContext->GetOpCode())
{
case OP_READ:

pClientContext->IncrSentBytes(dwBytesTransfered);

//Write operation was finished, see if all the data was sent.
//Else post another write.
if(pClientContext->GetSentBytes() < pClientContext->GetTotalBytes())
{
pClientContext->SetOpCode(OP_READ);

p_wbuf->buf += pClientContext->GetSentBytes();
p_wbuf->len = pClientContext->GetTotalBytes() - pClientContext->GetSentBytes();

dwFlags = 0;

//Overlapped send
nBytesSent = WSASend(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, dwFlags, p_ol, NULL);

if ((SOCKET_ERROR == nBytesSent) && (WSA_IO_PENDING != WSAGetLastError()))
{
//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
}
}
else
{
//Once the data is successfully received, we will print it.
pClientContext->SetOpCode(OP_WRITE);
pClientContext->ResetWSABUF();

dwFlags = 0;

//Get the data.
nBytesRecv = WSARecv(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);

if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
WriteToConsole("\nThread %d: Error occurred while executing WSARecv().", nThreadNo);

//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
}
}

break;

case OP_WRITE:

char szBuffer[MAX_BUFFER_LEN];

//Display the message we recevied
pClientContext->GetBuffer(szBuffer);

WriteToConsole("\nThread %d: The following message was received: %s", nThreadNo, szBuffer);

//Send the message back to the client.
pClientContext->SetOpCode(OP_READ);


pClientContext->SetTotalBytes(dwBytesTransfered);
pClientContext->SetSentBytes(0);

p_wbuf->len = dwBytesTransfered;

dwFlags = 0;

//Overlapped send
nBytesSent = WSASend(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, dwFlags, p_ol, NULL);

if ((SOCKET_ERROR == nBytesSent) && (WSA_IO_PENDING != WSAGetLastError()))
{
WriteToConsole("\nThread %d: Error occurred while executing WSASend().", nThreadNo);

//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
}

break;

default:
//We should never be reaching here, under normal circumstances.
break;
} // switch
} // while

return 0;
}

//Function to synchronize console output
//Threads need to be synchronized while they write to console.
//WriteConsole() API can be used, it is thread-safe, I think.
//I have created my own function.
void WriteToConsole(char *szFormat,
)
{
EnterCriticalSection(&g_csConsole);

va_list args;
va_start(args, szFormat);

vprintf(szFormat, args );

va_end(args);

LeaveCriticalSection(&g_csConsole);
}

//Store client related information in a vector
void AddToClientList(CClientContext *pClientContext)
{
EnterCriticalSection(&g_csClientList);

//Store these structures in vectors
g_ClientContext.push_back(pClientContext);

LeaveCriticalSection(&g_csClientList);
}

//This function will allow to remove one single client out of the list
void RemoveFromClientListAndFreeMemory(CClientContext *pClientContext)
{
EnterCriticalSection(&g_csClientList);

std::vector <CClientContext *>::iterator IterClientContext;

//Remove the supplied ClientContext from the list and release the memory
for (IterClientContext = g_ClientContext.begin(); IterClientContext != g_ClientContext.end(); IterClientContext++)
{
if (pClientContext == *IterClientContext)
{
g_ClientContext.erase(IterClientContext);

//i/o will be cancelled and socket will be closed by destructor.
delete pClientContext;
break;
}
}

LeaveCriticalSection(&g_csClientList);
}

//Clean up the list, this function will be executed at the time of shutdown
void CleanClientList()
{
EnterCriticalSection(&g_csClientList);

std::vector <CClientContext *>::iterator IterClientContext;

for (IterClientContext = g_ClientContext.begin(); IterClientContext != g_ClientContext.end( ); IterClientContext++)
{
//i/o will be cancelled and socket will be closed by destructor.
delete *IterClientContext;
}

g_ClientContext.clear();

LeaveCriticalSection(&g_csClientList);
}

//The use of static variable will ensure that
//we will make a call to GetSystemInfo()
//to find out number of processors,
//only if we don't have the information already.
//Repeated use of this function will be efficient.
int GetNoOfProcessors()
{
static int nProcessors = 0;

if (0 == nProcessors)
{
SYSTEM_INFO si;

GetSystemInfo(&si);

nProcessors = si.dwNumberOfProcessors;
}

return nProcessors;
}
I/O端口
同步/异步
堵塞/非堵塞
服务端/客户端
多线程程序设计
Winsock API 2.0
在这之前,我曾经开发过一个项目,其中一块需要网络支持,当时还考虑到了代码的可移植性,只要使用select,connect,accept,listen,send还有recv,再加上几个#ifdef的封装以用来处理Winsock和BSD套接字[socket]中间的不兼容性,一个网络子系统只用了几个小时很少的代码就写出来了,至今还让我很回味。那以后很长时间也就没再碰了。
前些日子,我们策划做一个网络游戏,我主动承担下网络这一块,想想这还不是小case,心里偷着乐啊。网络游戏好啊,网络游戏为成百上千的玩家提供了乐趣和令人着秘的游戏体验,他们在线上互相战斗或是加入队伍去战胜共同的敌人。我信心满满的准备开写我的网络,于是乎,发现过去的阻塞同步模式模式根本不能拿到一个巨量多玩家[MMP]的架构中去,直接被否定掉了。于是乎,就有了IOCP,如果能过很轻易而举的搞掂IOCP,也就不会有这篇教程了。下面请诸位跟随我进入正题。
什么是IOCP?
先让我们看看对IOCP的评价
I/O完成端口可能是Win32提供的最复杂的内核对象。
[Advanced Windows 3rd] Jeffrey Richter
这是[IOCP]实现高容量网络服务器的最佳方法。
[Windows Sockets2.0:Write Scalable Winsock Apps Using Completion Ports]
Microsoft Corporation
完成端口模型提供了最好的伸缩性。这个模型非常适用来处理数百乃至上千个套接字。
[Windows网络编程2nd] Anthony Jones & Jim Ohlund
I/O completion ports特别显得重要,因为它们是唯一适用于高负载服务器[必须同时维护许多连接线路]的一个技术。Completion ports利用一些线程,帮助平衡由I/O请求所引起的负载。这样的架构特别适合用在SMP系统中产生的”scalable”服务器。
[Win32多线程程序设计] Jim Beveridge & Robert Wiener
看来我们完全有理由相信IOCP是大型网络架构的首选。那IOCP到底是什么呢?
微软在Winsock2中引入了IOCP这一概念 。IOCP全称I/O Completion Port,中文译为I/O完成端口。IOCP是一个异步I/O的API,它可以高效地将I/O事件通知给应用程序。与使用select()或是其它异步方法不同的是,一个套接字[socket]与一个完成端口关联了起来,然后就可继续进行正常的Winsock操作了。然而,当一个事件发生的时候,此完成端口就将被操作系统加入一个队列中。然后应用程序可以对核心层进行查询以得到此完成端口。
这里我要对上面的一些概念略作补充,在解释[完成]两字之前,我想先简单的提一下同步和异步这两个概念,逻辑上来讲做完一件事后再去做另一件事就是同步,而同时一起做两件或两件以上事的话就是异步了。你也可以拿单线程和多线程来作比喻。但是我们一定要将同步和堵塞,异步和非堵塞区分开来,所谓的堵塞函数诸如accept(…),当调用此函数后,此时线程将挂起,直到操作系统来通知它,”HEY兄弟,有人连进来了”,那个挂起的线程将继续进行工作,也就符合”生产者-消费者”模型。堵塞和同步看上去有两分相似,但却是完全不同的概念。大家都知道I/O设备是个相对慢速的设备,不论打印机,调制解调器,甚至硬盘,与CPU相比都是奇慢无比的,坐下来等I/O的完成是一件不甚明智的事情,有时候数据的流动率非常惊人,把数据从你的文件服务器中以Ethernet速度搬走,其速度可能高达每秒一百万字节,如果你尝试从文件服务器中读取100KB,在用户的眼光来看几乎是瞬间完成,但是,要知道,你的线程执行这个命令,已经浪费了10个一百万次CPU周期。所以说,我们一般使用另一个线程来进行I/O。重叠IO[overlapped I/O]是Win32的一项技术,你可以要求操作系统为你传送数据,并且在传送完毕时通知你。这也就是[完成]的含义。这项技术使你的程序在I/O进行过程中仍然能够继续处理事务。事实上,操作系统内部正是以线程来完成overlapped I/O。你可以获得线程所有利益,而不需要付出什么痛苦的代价。
完成端口中所谓的[端口]并不是我们在TCP/IP中所提到的端口,可以说是完全没有关系。我到现在也没想通一个I/O设备[I/O Device]和端口[IOCP中的Port]有什么关系。估计这个端口也迷惑了不少人。IOCP只不过是用来进行读写操作,和文件I/O倒是有些类似。既然是一个读写设备,我们所能要求它的只是在处理读与写上的高效。在文章的第三部分你会轻而易举的发现IOCP设计的真正用意。
IOCP和网络又有什么关系?
int main()
{
WSAStartup(MAKEWORD(2, 2), &wsaData);
ListeningSocket = socket(AF_INET, SOCK_STREAM, 0);
bind(ListeningSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));
listen(ListeningSocket, 5);
int nlistenAddrLen = sizeof(ClientAddr);
while(TRUE)
{
NewConnection = accept(ListeningSocket, (SOCKADDR*)&ClientAddr, &nlistenAddrLen);
HANDLE hThread = CreateThread(NULL, 0, ThreadFunc, (void*) NewConnection, 0, &dwTreadId);
CloseHandle(hThread);
}
return 0;
}
相信只要写过网络的朋友,应该对这样的结构在熟悉不过了。accept后线程被挂起,等待一个客户发出请求,而后创建新线程来处理请求。当新线程处理客户请求时,起初的线程循环回去等待另一个客户请求。处理客户请求的线程处理完毕后终结。
在上述的并发模型中,对每个客户请求都创建了一个线程。其优点在于等待请求的线程只需做很少的工作。大多数时间中,该线程在休眠[因为recv处于堵塞状态]。
但是当并发模型应用在服务器端[基于Windows NT],Windows NT小组注意到这些应用程序的性能没有预料的那么高。特别的,处理很多同时的客户请求意味着很多线程并发地运行在系统中。因为所有这些线程都是可运行的[没有被挂起和等待发生什么事],Microsoft意识到NT内核花费了太多的时间来转换运行线程的上下文[Context],线程就没有得到很多CPU时间来做它们的工作。
大家可能也都感觉到并行模型的瓶颈在于它为每一个客户请求都创建了一个新线程。创建线程比起创建进程开销要小,但也远不是没有开销的。
我们不妨设想一下:如果事先开好N个线程,让它们在那hold[堵塞],然后可以将所有用户的请求都投递到一个消息队列中去。然后那N个线程逐一从消息队列中去取出消息并加以处理。就可以避免针对每一个用户请求都开线程。不仅减少了线程的资源,也提高了线程的利用率。理论上很不错,你想我等泛泛之辈都能想出来的问题,Microsoft又怎会没有考虑到呢?!
这个问题的解决方法就是一个称为I/O完成端口的内核对象,他首次在Windows NT3.5中被引入。
其实我们上面的构想应该就差不多是IOCP的设计机理。其实说穿了IOCP不就是一个消息队列嘛!你说这和[端口]这两字有何联系。我的理解就是IOCP最多是应用程序和操作系统沟通的一个接口罢了。
至于IOCP的具体设计那我也很难说得上来,毕竟我没看过实现的代码,但你完全可以进行模拟,只不过性能可能…,如果想深入理解IOCP, Jeffrey Ritchter的Advanced Windows 3rd其中第13章和第14张有很多宝贵的内容,你可以拿来窥视一下系统是如何完成这一切的。
实现方法
Microsoft为IOCP提供了相应的API函数,主要的就两个,我们逐一的来看一下:
HANDLE CreateIoCompletionPort (
HANDLE FileHandle, // handle to file
HANDLE ExistingCompletionPort, // handle to I/O completion port
ULONG_PTR CompletionKey, // completion key
DWORD NumberOfConcurrentThreads // number of threads to execute concurrently
);
在讨论各参数之前,首先要注意该函数实际用于两个截然不同的目的:
1.用于创建一个完成端口对象
2.将一个句柄[HANDLE]和完成端口关联到一起
在创建一个完成一个端口的时候,我们只需要填写一下NumberOfConcurrentThreads这个参数就可以了。它告诉系统一个完成端口上同时允许运行的线程最大数。在默认情况下,所开线程数和CPU数量相同,但经验给我们一个公式:
线程数 = CPU数 * 2 + 2
要使完成端口有用,你必须把它同一个或多个设备相关联。这也是调用CreateIoCompletionPort完成的。你要向该函数传递一个已有的完成端口的句柄,我们既然要处理网络事件,那也就是将客户的socket作为HANDLE传进去。和一个完成键[对你有意义的一个32位值,也就是一个指针,操作系统并不关心你传什么]。每当你向端口关联一个设备时,系统向该完成端口的设备列表中加入一条信息纪录。
另一个API就是
BOOL GetQueuedCompletionStatus(
HANDLE CompletionPort, // handle to completion port
LPDWORD lpNumberOfBytes, // bytes transferred
PULONG_PTR lpCompletionKey, // file completion key
LPOVERLAPPED *lpOverlapped, // buffer
DWORD dwMilliseconds // optional timeout value
);
第一个参数指出了线程要监视哪一个完成端口。很多服务应用程序只是使用一个I/O完成端口,所有的I/O请求完成以后的通知都将发给该端口。简单的说,GetQueuedCompletionStatus使调用线程挂起,直到指定的端口的I/O完成队列中出现了一项或直到超时。同I/O完成端口相关联的第3个数据结构是使线程得到完成I/O项中的信息:传输的字节数,完成键和OVERLAPPED结构的地址。该信息是通过传递给GetQueuedCompletionSatatus的lpdwNumberOfBytesTransferred,lpdwCompletionKey和lpOverlapped参数返回给线程的。
PostQueuedCompletionStatus Function
Posts an I/O completion packet to an I/O completion port.
BOOL WINAPI PostQueuedCompletionStatus(
__in HANDLE CompletionPort,
__in DWORD dwNumberOfBytesTransferred,
__in ULONG_PTR dwCompletionKey,
__in LPOVERLAPPED lpOverlapped
);
Parameters
CompletionPort
A handle to an I/O completion port to which the I/O completion packet is to be posted.
dwNumberOfBytesTransferred
The value to be returned through the lpNumberOfBytesTransferred parameter of the GetQueuedCompletionStatus function.
dwCompletionKey
The value to be returned through the lpCompletionKey parameter of the GetQueuedCompletionStatus function.
lpOverlapped
The value to be returned through the lpOverlapped parameter of the GetQueuedCompletionStatus function.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError .
Remarks
The I/O completion packet will satisfy an outstanding call to the GetQueuedCompletionStatus function. This function returns with the three values passed as the second, third, and fourth parameters of the call to PostQueuedCompletionStatus. The system does not use or validate these values. In particular, the lpOverlapped parameter need not point to an OVERLAPPED structure.
Requirements
Client
Requires Windows Vista, Windows XP, or Windows 2000 Professional.
Server
Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header
Declared in WinBase.h; include Windows.h.
Library
Use Kernel32.lib.
DLL
Requires Kernel32.dll.
根据到目前为止已经讲到的东西,首先来构建一个frame。下面为您说明了如何使用完成端口来开发一个echo服务器。大致如下:
1.初始化Winsock
2.创建一个完成端口
3.根据服务器线程数创建一定量的线程数
4.准备好一个socket进行bind然后listen
5.进入循环accept等待客户请求
6.创建一个数据结构容纳socket和其他相关信息
7.将连进来的socket同完成端口相关联
8.投递一个准备接受的请求
以后就不断的重复5至8的过程
那好,我们用具体的代码来展示一下细节的操作。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <winsock2.h>
#include <vector>

int main(int argc, char *argv[])
{
//Validate the input
// if (argc < 2)
// {
// printf("\nUsage: %s port.", argv[0]);
// return 1;
// }
if (false == Initialize())
{
return 1;
}
SOCKET ListenSocket; 
struct sockaddr_in ServerAddress;
//Overlapped I/O follows the model established in Windows and can be performed only on
//sockets created through the WSASocket function
ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if (INVALID_SOCKET == ListenSocket)
{
printf("\nError occurred while opening socket: %d.", WSAGetLastError());
goto error;
}
else
{
printf("\nWSASocket() successful.");
}
//Cleanup and Init with 0 the ServerAddress
ZeroMemory((char *)&ServerAddress, sizeof(ServerAddress));
//Port number will be supplied as a command line argument
int nPortNo;
nPortNo = 52211;
//Fill up the address structure
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_addr.s_addr = INADDR_ANY; //WinSock will supply address
ServerAddress.sin_port = htons(nPortNo); //comes from commandline
//Assign local address and port number
if (SOCKET_ERROR == bind(ListenSocket, (struct sockaddr *) &ServerAddress, sizeof(ServerAddress)))
{
closesocket(ListenSocket);
printf("\nError occurred while binding.");
goto error;
}
else
{
printf("\nbind() successful.");
}
//Make the socket a listening socket
if (SOCKET_ERROR == listen(ListenSocket,SOMAXCONN))
{
closesocket(ListenSocket);
printf("\nError occurred while listening.");
goto error;
}
else
{
printf("\nlisten() successful.");
}
g_hAcceptEvent = WSACreateEvent();
if (WSA_INVALID_EVENT == g_hAcceptEvent)
{
printf("\nError occurred while WSACreateEvent().");
goto error;
}
if (SOCKET_ERROR == WSAEventSelect(ListenSocket, g_hAcceptEvent, FD_ACCEPT))
{
printf("\nError occurred while WSAEventSelect().");
WSACloseEvent(g_hAcceptEvent);
goto error;
}
printf("\nTo exit this server, hit a key at any time on this console
");
DWORD nThreadID;
g_hAcceptThread = CreateThread(0, 0, AcceptThread, (void *)ListenSocket, 0, &nThreadID);
//Hang in there till a key is hit
while(!_kbhit())
{
Sleep(0); //switch to some other thread
}
WriteToConsole("\nServer is shutting down
");
//Start cleanup
CleanUp();
//Close open sockets
closesocket(ListenSocket);
DeInitialize();
return 0; //success
error:
closesocket(ListenSocket);
DeInitialize();
return 1;
}
bool Initialize()
{
//Find out number of processors and threads
g_nThreads = WORKER_THREADS_PER_PROCESSOR * GetNoOfProcessors();
printf("\nNumber of processors on host: %d", GetNoOfProcessors());
printf("\nThe following number of worker threads will be created: %d", g_nThreads);
//Allocate memory to store thread handless
g_phWorkerThreads = new HANDLE[g_nThreads];
//Initialize the Console Critical Section
InitializeCriticalSection(&g_csConsole);
//Initialize the Client List Critical Section
InitializeCriticalSection(&g_csClientList);
//Create shutdown event
g_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// Initialize Winsock
WSADATA wsaData;
int nResult;
nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (NO_ERROR != nResult)
{
printf("\nError occurred while executing WSAStartup().");
return false; //error
}
else
{
printf("\nWSAStartup() successful.");
}
if (false == InitializeIOCP())
{
printf("\nError occurred while initializing IOCP");
return false;
}
else
{
printf("\nIOCP initialization successful.");
}
return true;
}
//Function to Initialize IOCP
bool InitializeIOCP()
{
//Create I/O completion port
g_hIOCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0 );
if ( NULL == g_hIOCompletionPort)
{
printf("\nError occurred while creating IOCP: %d.", WSAGetLastError());
return false;
}
DWORD nThreadID;
//Create worker threads
for (int ii = 0; ii < g_nThreads; ii++)
{
g_phWorkerThreads[ii] = CreateThread(0, 0, WorkerThread, (void *)(ii+1), 0, &nThreadID);
}
return true;
}
void CleanUp()
{
//Ask all threads to start shutting down
SetEvent(g_hShutdownEvent);
//Let Accept thread go down
WaitForSingleObject(g_hAcceptThread, INFINITE);
for (int i = 0; i < g_nThreads; i++)
{
//Help threads get out of blocking - GetQueuedCompletionStatus()
PostQueuedCompletionStatus(g_hIOCompletionPort, 0, (DWORD) NULL, NULL);
}
//Let Worker Threads shutdown
WaitForMultipleObjects(g_nThreads, g_phWorkerThreads, TRUE, INFINITE);
//We are done with this event
WSACloseEvent(g_hAcceptEvent);
//Cleanup dynamic memory allocations, if there are any.
CleanClientList();
}
void DeInitialize()
{
//Delete the Console Critical Section.
DeleteCriticalSection(&g_csConsole);
//Delete the Client List Critical Section.
DeleteCriticalSection(&g_csClientList);
//Cleanup IOCP.
CloseHandle(g_hIOCompletionPort);
//Clean up the event.
CloseHandle(g_hShutdownEvent);
//Clean up memory allocated for the storage of thread handles
delete[] g_phWorkerThreads;
//Cleanup Winsock
WSACleanup();
}
//This thread will look for accept event
DWORD WINAPI AcceptThread(LPVOID lParam)
{
SOCKET ListenSocket = (SOCKET)lParam;
WSANETWORKEVENTS WSAEvents;
//Accept thread will be around to look for accept event, until a Shutdown event is not Signaled.
while(WAIT_OBJECT_0 != WaitForSingleObject(g_hShutdownEvent, 0))
{
if (WSA_WAIT_TIMEOUT != WSAWaitForMultipleEvents(1, &g_hAcceptEvent, FALSE, WAIT_TIMEOUT_INTERVAL, FALSE))
{
WSAEnumNetworkEvents(ListenSocket, g_hAcceptEvent, &WSAEvents);
if ((WSAEvents.lNetworkEvents & FD_ACCEPT) && (0 == WSAEvents.iErrorCode[FD_ACCEPT_BIT]))
{
//Process it
AcceptConnection(ListenSocket);
}
}
}
return 0;
}
//This function will process the accept event
void AcceptConnection(SOCKET ListenSocket)
{
sockaddr_in ClientAddress;
int nClientLength = sizeof(ClientAddress);
//Accept remote connection attempt from the client
SOCKET Socket = accept(ListenSocket, (sockaddr*)&ClientAddress, &nClientLength);
if (INVALID_SOCKET == Socket)
{
WriteToConsole("\nError occurred while accepting socket: %ld.", WSAGetLastError());
}
//Display Client's IP
WriteToConsole("\nClient connected from: %s", inet_ntoa(ClientAddress.sin_addr));
//Create a new ClientContext for this newly accepted client
CClientContext *pClientContext = new CClientContext;
pClientContext->SetOpCode(OP_READ);
pClientContext->SetSocket(Socket);
//Store this object
AddToClientList(pClientContext);
if (true == AssociateWithIOCP(pClientContext))
{
//Once the data is successfully received, we will print it.
pClientContext->SetOpCode(OP_WRITE);
WSABUF *p_wbuf = pClientContext->GetWSABUFPtr();
OVERLAPPED *p_ol = pClientContext->GetOVERLAPPEDPtr();
//Get data.
DWORD dwFlags = 0;
DWORD dwBytes = 0;
//Post initial Recv
//This is a right place to post a initial Recv
//Posting a initial Recv in WorkerThread will create scalability issues.
int nBytesRecv = WSARecv(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);
if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
WriteToConsole("\nError in Initial Post.");
}
}
}
bool AssociateWithIOCP(CClientContext *pClientContext)
{
//Associate the socket with IOCP
HANDLE hTemp = CreateIoCompletionPort((HANDLE)pClientContext->GetSocket(), g_hIOCompletionPort, (DWORD)pClientContext, 0);
if (NULL == hTemp)
{
WriteToConsole("\nError occurred while executing CreateIoCompletionPort().");
//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
return false;
}
return true;
}
//Worker thread will service IOCP requests
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
int nThreadNo = (int)lpParam;
void *lpContext = NULL;
OVERLAPPED *pOverlapped = NULL;
CClientContext *pClientContext = NULL;
DWORD dwBytesTransfered = 0;
int nBytesRecv = 0;
int nBytesSent = 0;
DWORD dwBytes = 0, dwFlags = 0;
//Worker thread will be around to process requests, until a Shutdown event is not Signaled.
while (WAIT_OBJECT_0 != WaitForSingleObject(g_hShutdownEvent, 0))
{
BOOL bReturn = GetQueuedCompletionStatus(
g_hIOCompletionPort,
&dwBytesTransfered,
(LPDWORD)&lpContext,
&pOverlapped,
INFINITE);
if (NULL == lpContext)
{
//We are shutting down
break;
}
//Get the client context
pClientContext = (CClientContext *)lpContext;
if ((FALSE == bReturn) || ((TRUE == bReturn) && (0 == dwBytesTransfered)))
{
//Client connection gone, remove it.
RemoveFromClientListAndFreeMemory(pClientContext);
continue;
}
WSABUF *p_wbuf = pClientContext->GetWSABUFPtr();
OVERLAPPED *p_ol = pClientContext->GetOVERLAPPEDPtr();
switch (pClientContext->GetOpCode())
{
case OP_READ:
pClientContext->IncrSentBytes(dwBytesTransfered);
//Write operation was finished, see if all the data was sent.
//Else post another write.
if(pClientContext->GetSentBytes() < pClientContext->GetTotalBytes())
{
pClientContext->SetOpCode(OP_READ);
p_wbuf->buf += pClientContext->GetSentBytes();
p_wbuf->len = pClientContext->GetTotalBytes() - pClientContext->GetSentBytes();
dwFlags = 0;
//Overlapped send
nBytesSent = WSASend(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, dwFlags, p_ol, NULL);
if ((SOCKET_ERROR == nBytesSent) && (WSA_IO_PENDING != WSAGetLastError()))
{
//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
}
}
else
{
//Once the data is successfully received, we will print it.
pClientContext->SetOpCode(OP_WRITE);
pClientContext->ResetWSABUF();
dwFlags = 0;
//Get the data.
nBytesRecv = WSARecv(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, &dwFlags, p_ol, NULL);
if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
WriteToConsole("\nThread %d: Error occurred while executing WSARecv().", nThreadNo);
//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
}
}
break;
case OP_WRITE:
char szBuffer[MAX_BUFFER_LEN];
//Display the message we recevied
pClientContext->GetBuffer(szBuffer);
WriteToConsole("\nThread %d: The following message was received: %s", nThreadNo, szBuffer);
//Send the message back to the client.
pClientContext->SetOpCode(OP_READ);

pClientContext->SetTotalBytes(dwBytesTransfered);
pClientContext->SetSentBytes(0);
p_wbuf->len = dwBytesTransfered;
dwFlags = 0;
//Overlapped send
nBytesSent = WSASend(pClientContext->GetSocket(), p_wbuf, 1,
&dwBytes, dwFlags, p_ol, NULL);
if ((SOCKET_ERROR == nBytesSent) && (WSA_IO_PENDING != WSAGetLastError()))
{
WriteToConsole("\nThread %d: Error occurred while executing WSASend().", nThreadNo);
//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
}
break;
default:
//We should never be reaching here, under normal circumstances.
break;
} // switch
} // while
return 0;
}
//Function to synchronize console output
//Threads need to be synchronized while they write to console.
//WriteConsole() API can be used, it is thread-safe, I think.
//I have created my own function.
void WriteToConsole(char *szFormat,
)
{
EnterCriticalSection(&g_csConsole);
va_list args;
va_start(args, szFormat);
vprintf(szFormat, args );
va_end(args);
LeaveCriticalSection(&g_csConsole);
}
//Store client related information in a vector
void AddToClientList(CClientContext *pClientContext)
{
EnterCriticalSection(&g_csClientList);
//Store these structures in vectors
g_ClientContext.push_back(pClientContext);
LeaveCriticalSection(&g_csClientList);
}
//This function will allow to remove one single client out of the list
void RemoveFromClientListAndFreeMemory(CClientContext *pClientContext)
{
EnterCriticalSection(&g_csClientList);
std::vector <CClientContext *>::iterator IterClientContext;
//Remove the supplied ClientContext from the list and release the memory
for (IterClientContext = g_ClientContext.begin(); IterClientContext != g_ClientContext.end(); IterClientContext++)
{
if (pClientContext == *IterClientContext)
{
g_ClientContext.erase(IterClientContext);
//i/o will be cancelled and socket will be closed by destructor.
delete pClientContext;
break;
}
}
LeaveCriticalSection(&g_csClientList);
}
//Clean up the list, this function will be executed at the time of shutdown
void CleanClientList()
{
EnterCriticalSection(&g_csClientList);
std::vector <CClientContext *>::iterator IterClientContext;
for (IterClientContext = g_ClientContext.begin(); IterClientContext != g_ClientContext.end( ); IterClientContext++)
{
//i/o will be cancelled and socket will be closed by destructor.
delete *IterClientContext;
}
g_ClientContext.clear();
LeaveCriticalSection(&g_csClientList);
}
//The use of static variable will ensure that
//we will make a call to GetSystemInfo()
//to find out number of processors,
//only if we don't have the information already.
//Repeated use of this function will be efficient.
int GetNoOfProcessors()
{
static int nProcessors = 0;
if (0 == nProcessors)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
nProcessors = si.dwNumberOfProcessors;
}
return nProcessors;
}


浙公网安备 33010602011771号