参考

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另一种实现的更多相关文章

  1. IOCP 浅析

    http://www.ibm.com/developerworks/cn/java/j-lo-iocp/ https://msdn.microsoft.com/en-us/library/window ...

  2. IOCP编程小结(上)

    前段时间接手了一个网络游戏前端连接服务器的开发工作,由于服务器需要在windows平台上部署,并且需要处理大量的客户端连接,因此采用IOCP来做为服务器端的编程模型就成了不二选择.虽然我对服务器开发并 ...

  3. (转载)IOCP 浅析

    转自:http://www.ibm.com/developerworks/cn/java/j-lo-iocp/#author   郭 仁祥, 软件工程师, IBM 简介: 传统的 Server/Cli ...

  4. BIO\NIO\AIO记录

    IO操作可以分为3类:同步阻塞(BIO).同步非阻塞(NIO).异步(AIO). 同步阻塞(BIO):在此种方式下,用户线程发起一个IO操作以后,必须等待IO操作的完成,只有当真正完成了IO操作以后, ...

  5. Socket编程模式

    Socket编程模式 本文主要分析了几种Socket编程的模式.主要包括基本的阻塞Socket.非阻塞Socket.I/O多路复用.其中,阻塞和非阻塞是相对于套接字来说的,而其他的模式本质上来说是基于 ...

  6. Socket编程模式理解与对比

    本文主要分析了几种Socket编程的模式.主要包括基本的阻塞Socket.非阻塞Socket.I/O多路复用.其中,阻塞和非阻塞是相对于套接字来说的,而其他的模式本质上来说是基于Socket的并发模式 ...

  7. socket 异步通信的一些问题

    socket通信在使用时被封装很简单,像操作文件一样简单,正是因为简单里面好多细节需要深入研究一下. windows下通信有select和iocp方式,select是传统方式,在socket里使用re ...

  8. 源码剖析Linux epoll实现机制及Linux上惊群

    转载:https://blog.csdn.net/tgxallen/article/details/78086360 看源码是对一个技术认识最直接且最有效的方式了,之前用Linux Epoll做过一个 ...

  9. 【IOCP】 IOCP模型属于一种通讯模型- 较难

    http://baike.baidu.com/link?url=e9vXkKd2aHp8VDr1XTURdwQB4K85r28IYjeMwRIyuaXtsrCsXHY1eohiFgsDXRYRlj6x ...

随机推荐

  1. golang 学习笔记 -- struct interface的使用

    一个 interface 类型定义了一个方法集做接口. 区分goalng的方法和函数 func go() { fmt.Println('go to home') } 这是函数 type car str ...

  2. Flink基本的API(续)

    上一篇介绍了编写 Flink 程序的基本步骤,以及一些常见 API,如:map.filter.keyBy 等,重点介绍了 keyBy 方法.本篇将继续介绍 Flink 中常用的 API,主要内容为 指 ...

  3. redis 实战操作RDB和AOF快照持久化

    前言:redis是我们常用的缓存方式,今天就来介绍下两种持久化的方式吧,先科普概念,再实战操作 一.RDB Redis将某一时刻的快照(备份的数据库数据)保存成一种称为RDB格式的文件中,这种格式是经 ...

  4. mysql启动时出现ERROR 2003问题的解决方法

    目录 写在前面 问题描述 分析原因 问题解决 写在前面 今天,在打开Navicat Permium 链接MySQL 的时候出现Error 2003 的错误. 遂记录产生的原因以及解决方法. 问题描述 ...

  5. C# VB .NET生成条形码,支持多种格式类型

    条形码简单,方便印刷,因此在各个领域得到了广泛的应用.我们自己的项目里也可以将一些特定的数据以条形码的方式来展示和应用,实现一码走天下.那么如何在C#,.Net平台代码里生成条形码呢?答案是使用Sha ...

  6. python 进程和线程-线程和线程变量ThreadLocal

    线程 线程是由若干个进程组成的,所以一个进程至少包含一个线程:并且线程是操作系统直接支持的执行单元.多任务可以由多进程完成,也可由一个进程的多个线程来完成 Python的线程是真正的Posix Thr ...

  7. laravel框架之即时更改

    表单//@foreach($res as $k=>$v) <tr id="{{$v->id}}" > <td>{{$v->id}}< ...

  8. redis不能保存bean对象

    可用JSON转为json格式 // 2.3 将用户信息存储在redis中 String memberToJson = JSON.toJSON(member).toString(); 需要maven坐标 ...

  9. PCI_PCIe_miniPCIe规格说明

    PCI PCI是一种本地总线(并行),规格书名称:PCI Local Bus Specification.并行总线,插槽规格统一. PCI stands for Peripheral Componen ...

  10. AI-数据标注类型

        随着数据的暴增和计算机硬件技术的发展,也催生了AI技术在各行各业的应用渗透.而想将AI技术应用到各行各业,数据是必需品.因为数据直接影响到AI最终训练出来的模型好坏.AI建模没有太大门槛,但数 ...