zoukankan      html  css  js  c++  java
  • 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上,然后在回调函数内处理数据,其他细节都不用关心。

  • 相关阅读:
    数据结构----------堆栈
    数据结构----------双向链表
    剑指offer32----之字形打印一颗二叉树
    剑指offer31----栈的压入、弹出序列
    MySQL----日期处理函数
    1.1 文档PUT内部原理
    1 集群状态、增删改查、全量替换、强制创建、设置单个index的分片数副本数
    编译问题:'<invalid-global-code>' does not contain a definition for 'Store' and no extension method 'XXX' accepting a first argument of type '<invalid-global-code>' could be found
    ES timeout 的一些笔记
    elasticsearch 5.0以上不支持consistency 和 quorum
  • 原文地址:https://www.cnblogs.com/studywithallofyou/p/11497296.html
Copyright © 2011-2022 走看看