IOCP另一种实现
参考
https://docs.microsoft.com/zh-cn/windows/win32/api/winbase/nf-winbase-bindiocompletioncallback
https://young2code.wordpress.com/2009/08/16/network-programming-with-iocp-and-thread-pool-intro/
前言
IOCP除了使用IOCP Input/Output Completion Port IO完成端口里面的方法,还有一种更简单的,换句话说也就是微软为了方便开发封装的更多的一种用法(这种方法是我用过的,除了这种还有一种CreateThreadpoolIo,在上面参考文章中作者有介绍)。
1. BindIoCompletionCallback function
Associates the I/O completion port owned by the thread pool with the specified file handle. On completion of an I/O request involving this file, a non-I/O worker thread will execute the specified callback function.
BOOL BindIoCompletionCallback(
HANDLE FileHandle,
LPOVERLAPPED_COMPLETION_ROUTINE Function,
ULONG Flags
);
Parameters
FileHandle
A handle to the file opened for overlapped I/O completion. This handle is returned by the CreateFile function, with the FILE_FLAG_OVERLAPPED flag.
我们需要监听绑定到IOCP的socket。
Function
A pointer to the callback function to be executed in a non-I/O worker thread when the I/O operation is complete. This callback function must not call the TerminateThread function.
For more information about the completion routine, see FileIOCompletionRoutine.
IOCP回调的函数。
Flags
This parameter must be zero.
设置为0.
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 the GetLastError function. The value returned is an NTSTATUS error code. To retrieve the corresponding system error code, use the RtlNtStatusToDosError function.
Remarks
The callback function might not be executed if the process issues an asynchronous request on the file specified by the FileHandle parameter but the request returns immediately with an error code other than ERROR_IO_PENDING.
Be sure that the thread that initiates the asynchronous I/O request does not terminate before the request is completed. Also, if a function in a DLL is queued to a worker thread, be sure that the function in the DLL has completed execution before the DLL is unloaded.
The thread pool maintains an I/O completion port. When you call BindIoCompletionCallback, it associates the specified file with the thread pool's I/O completion port. Asynchronous requests on that file object will complete by posting to the completion port, where they will be picked up by thread pool worker threads. For callbacks that must issue an I/O request that completes as an asynchronous procedure call, the thread pool provides an I/O worker pool. The I/O worker threads do not wait on the completion port; they sleep in an alertable wait state so that I/O request packets that complete can wake them up. Both types of worker threads check whether there is I/O pending on them and if there is, they do not exit. For more information, see Asynchronous Procedure Calls.
To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later. For more information, see Using the Windows Headers.
这个用法更简单,不需要创建IOCP句柄,也不需要创建工作线程,一切都交给系统处理。我们只需知道调用这个函数把socket绑定到IOCP上,然后在回调函数内处理数据,其他细节都不用关心。
IOCP另一种实现的更多相关文章
- IOCP 浅析
http://www.ibm.com/developerworks/cn/java/j-lo-iocp/ https://msdn.microsoft.com/en-us/library/window ...
- IOCP编程小结(上)
前段时间接手了一个网络游戏前端连接服务器的开发工作,由于服务器需要在windows平台上部署,并且需要处理大量的客户端连接,因此采用IOCP来做为服务器端的编程模型就成了不二选择.虽然我对服务器开发并 ...
- (转载)IOCP 浅析
转自:http://www.ibm.com/developerworks/cn/java/j-lo-iocp/#author 郭 仁祥, 软件工程师, IBM 简介: 传统的 Server/Cli ...
- BIO\NIO\AIO记录
IO操作可以分为3类:同步阻塞(BIO).同步非阻塞(NIO).异步(AIO). 同步阻塞(BIO):在此种方式下,用户线程发起一个IO操作以后,必须等待IO操作的完成,只有当真正完成了IO操作以后, ...
- Socket编程模式
Socket编程模式 本文主要分析了几种Socket编程的模式.主要包括基本的阻塞Socket.非阻塞Socket.I/O多路复用.其中,阻塞和非阻塞是相对于套接字来说的,而其他的模式本质上来说是基于 ...
- Socket编程模式理解与对比
本文主要分析了几种Socket编程的模式.主要包括基本的阻塞Socket.非阻塞Socket.I/O多路复用.其中,阻塞和非阻塞是相对于套接字来说的,而其他的模式本质上来说是基于Socket的并发模式 ...
- socket 异步通信的一些问题
socket通信在使用时被封装很简单,像操作文件一样简单,正是因为简单里面好多细节需要深入研究一下. windows下通信有select和iocp方式,select是传统方式,在socket里使用re ...
- 源码剖析Linux epoll实现机制及Linux上惊群
转载:https://blog.csdn.net/tgxallen/article/details/78086360 看源码是对一个技术认识最直接且最有效的方式了,之前用Linux Epoll做过一个 ...
- 【IOCP】 IOCP模型属于一种通讯模型- 较难
http://baike.baidu.com/link?url=e9vXkKd2aHp8VDr1XTURdwQB4K85r28IYjeMwRIyuaXtsrCsXHY1eohiFgsDXRYRlj6x ...
随机推荐
- java设计模式结构型模式
结构型模式: – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结 构,用来解决更大的问题 分类: • 适配器模式.代理模式.桥接模式. 装饰模式.组合模式.外观模式.享元模式 结构型模式 ...
- web api与mvc的区别
MVC主要用来构建网站,既关心数据也关心页面展示,而Web API只关注数据 Web API支持格式协商,客户端可以通过Accept header通知服务器期望的格式 Web API支持Self Ho ...
- 如何简单使用tensorboard展示(一)
我使用tensorboard中的graph做了展示,至于其它功能可以类推,其代码如下: import numpy as npimport tensorflow as tf x_img = np.arr ...
- P2704 [NOI2001]炮兵阵地 (状压DP)
题目: P2704 [NOI2001]炮兵阵地 解析: 和互不侵犯一样 就是多了一格 用\(f[i][j][k]\)表示第i行,上一行状态为\(j\),上上行状态为\(k\)的最多的可以放的炮兵 发现 ...
- uni-app项目配置记录
新建项目 直接使用编辑器快速新建,具体方法很简单,官方文档很详细,这里不在叙说 配置项目: 项目搭建好了之后,我们配置一些 api 和 router,这些直接在插件市场上面进行配置,非常好用 封装的r ...
- 易语言 MD5生成
下载MD5脚本 https://download.csdn.net/download/zhangxuechao_/10573121 添加脚本组件 定义常量 生成MD5
- vscode vue 去掉语法提示
在vscode中,点击file->preferences->settings, 然后输入vetur, 滚到最下面,那个勾去掉,然后关闭,重启vscode就可以了
- mysql 连接数用完,root也无法登陆的处理方法
gdb -p $(pidof mysqld) -ex "set max_connections=1500" -batch 使用 gdb 临时调大 参数 max_connection ...
- elastalert基本配置说明
elastalert 配置语法: 简单rule规则: es_host,es_port:查询elasticsearch集群 name: 规则的唯一名称.如果相同,则elastalert不会启动. typ ...
- Linux命令查找文件目录
座右铭:长风破浪会有时,直挂云帆济沧海. linux一般查看文件或者目录有几种方法. /查看文件类容--------cat/more/less/head/tail 只能查看文本型(txt) (1) ...