<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
TIP 1: Use Winsock2 IOCP-capable functions, such as WSASend and WSARecv, over Win32 file I/O functions, such as WriteFile and ReadFile.
提示1:尽量使用WSASend和WSARecv进行数据收发
Socket handles from Microsoft-based protocol providers are IFS handles so you can use Win32 file I/O calls with the handle. However, the interactions between the provider and file system involve many kernel/user mode transition, thread context switches, and parameter marshals that result in a significant performance penalty. You should use only Winsock2 IOCP- capable functions with IOCP.
The additional parameter marshals and mode transitions in ReadFile and WriteFile only occur if the provider does not have XP1_IFS_HANDLES bit set in dwServiceFlags1 of its WSAPROTOCOL_INFO structure.
NOTE: These providers have an unavoidable additional mode transition, even in the case of WSASend and WSARecv, although ReadFile and WriteFile will have more of them.
TIP 2: Choose the number of the concurrent worker threads allowed and the total number of the worker threads to spawn.
提示2:确定合适的工作者线程数目
The number of worker threads and the number of concurrent threads that the IOCP uses are not the same thing. You can decide to have a maximum of 2 concurrent threads used by the IOCP and a pool of 10 worker threads. You have a pool of worker threads greater than or equal to the number of concurrent threads used by the IOCP so that a worker thread handling a dequeued completion packet can call one of the Win32 "wait" functions without delaying the handling of other queued I/O packets.
If there are completion packets waiting to be dequeued, the system will wake up another worker thread. Eventually, the first thread satisfies it's Wait and it can be run again. When this happens, the number of the threads that can be run is higher than the concurrency allowed on the IOCP (for example, NumberOfConcurrentThreads). However, when next worker thread calls GetQueueCompletionStatus and enters wait status, the system does not wake it up. In other words, the system tries to keep your requested number of concurrent worker threads.
Typically, you only need one concurrent worker thread per CPU for IOCP. To do this, enter 0 for NumberOfConcurrentThreads in the CreateIoCompletionPort call when you first create the IOCP.
TIP 3: Associate a posted I/O operation with a dequeued completion packet.
提示3:根据出队的完成包进行IO操作。
GetQueuedCompletionStatus returns a completion key and an overlapped structure for the I/O when dequeuing a completion packet. You should use these two structures to return per handle and per I/O operation information, respectively. You can use your socket handle as the completion key when you register the socket with the IOCP to provide per handle information. To provide per I/O operation "extend" the overlapped structure to contain your application-specific I/O-state information. Also, make sure you provide a unique overlapped structure for each overlapped I/O. When an I/O completes, the same pointer to the overlapped I/O structure is returned.
TIP 4: I/O completion packet queuing behavior.
提示4:完成包的异常处理。
The order in which I/O completion packets are queued in the IOCP is not necessarily the same order the Winsock2 I/O calls were made. Additionally, if a Winsock2 I/O call returns SUCCESS or IO_PENDING, it is guaranteed that a completion packet will be queued to the IOCP when the I/O completes, regardless of whether the socket handle is closed. After you close a socket handle, future calls to WSASend, WSASendTo, WSARecv, or WSARecvFrom will fail with a return code other than SUCCESS or IO_PENDING, which will not generate a completion packet. The status of the completion packet retrieved by GetQueuedCompletionStatus for I/O previously posted could indicate a failure in this case.
If you delete the IOCP itself, no more I/O can be posted to the IOCP because the IOCP handle itself is invalid. However, the system's underlying IOCP kernel structures do not go away until all successfully posted I/Os are completed.
TIP 5: IOCP cleanup.
提示5:完成端口的释放。
The most important thing to remember when performing ICOP cleanup is the same when using overlapped I/O: do not free an overlapped structure if the I/O for it has not yet completed. The HasOverlappedIoCompleted macro allows you to detect if an I/O has completed from its overlapped structure.
There are typically two scenarios for shutting down a server. In the first scenario, you do not care about the completion status of outstanding I/Os and you just want to shut down as fast as you can. In the second scenario, you want to shut down the server, but you do need to know the completion status of each outstanding I/O.
In the first scenario, you can call PostQueueCompletionStatus (N times, where N is the number of worker threads) to post a special completion packet that informs the worker thread to exit immediately, close all socket handles and their associated overlapped structures, and then close the completion port. Again, make sure you use HasOverlappedIoCompleted to check the completion status of an overlapped structure before you free it. If a socket is closed, all outstanding I/O on the socket eventually complete quickly.
In the second scenario, you can delay exiting worker threads so that all completion packets can be properly dequeued. You can start by closing all socket handles and the IOCP. However, you need to maintain a count of the number of outstanding I/Os so that your worker thread can know when it is safe to exit the thread. The performance penalty of having a global I/O counter protected with a critical section for an IOCP server is not as bad as might be expected because the active worker thread does not switch out if there are more completion packets waiting in the queue.
分享到:
相关推荐
用IOCP完成端口编写的服务器,性能稳定.可以连接几千个客户端.zip
小猪的IOCP教程和代码是目前我见过的最好的资源. 速度最快,架构最清晰, 建议到他空间去看一下:http://blog.csdn.net/piggyxp/article/details/6922277#quote 原作中使用MFC做的界面, 我这儿封装了一个SDK版的, 供不...
iocp开发一个最简单的例子,iocp开发一个最简单的例子
基本的IOCP框架已经完成,希望对于初学者有点用处。
IOCP 服务器端 IOCP 服务器端 IOCP 服务器端
IOCP文件客户端 IOCP文件客户端 IOCP文件客户端
IOCP的详细介绍文档,描述了IOCP的使用方法和注意事项。
本文主要探讨一下windows平台上的完成端口开发及其与之相关的几个重要的技术概念,这些概念都是与基于IOCP的开发密切相关的,对开发人员来讲,又不得不给予足够重视的几个概念: 1) 基于IOCP实现的服务吞吐量 2)IOCP...
一个关于iocp的很好的例子 学习iocp的例子
Windows下IOCP模型 socket服务器端实例 1. 创建服务器socket, 并将socket设置为非阻塞模式 2. bind()绑定IP地此与端口 3. listen() 4. 创建IO完成端口,将socket绑定到IO完成端口上 5. 根据当前机器CPU个数创建工作...
iocp 资料
{**************************************************** IOCP Socket Server 主要参考自CodeProject下载源码, 特别感谢张无忌的帮助 这个版本还很粗糙,很多地方没有处理好,最近 比较忙,没有精力和时间来完善它...
iocp .
iocp 客户端
C# iocp IO模型C# iocp IO模型C# iocp IO模型C# iocp IO模型C# iocp IO模型C# iocp IO模型C# iocp IO模型C# iocp IO模型C# iocp IO模型
vc开发的iocp的服务器实例,可作参考。 包括iocp类,包括demo。 iocp是windows下最后的socket服务器模型。
IOCP,微软提供IOCP例子,非常经典的IOCP例子'。非入门iocp的非常有参考价值。IOCP,微软提供IOCP例子,非常经典的IOCP例子'。非入门iocp的非常有参考价值。
自己写的IOCP源代码,可重用。用时标明出处即可。 一个连接同一时刻仅有一个待处理的接收投递。
IOCP源代码,里面还包括了其他的几个i/o模型。期待你的下载,进行学习交流