“Un-associate” socket from completion port

99封情书 提交于 2019-12-12 09:49:39

问题


CreateIoCompletionPort() is used to associate a socket with a completion port. However, when this socket is closed, then I need to "un-associate" it from the completion port. How can I do that?


回答1:


A handle that is associated with an I/O Completion Port is removed from the port when the handle is closed. In case of a network socket, the handle is closed by calling closesocket().

The documentation for CreateIoCompletionPort contains remarks on resource handling:

The handle passed in the FileHandle parameter can be any handle that supports overlapped I/O. Most commonly, this is a handle opened by the CreateFile function using the FILE_FLAG_OVERLAPPED flag (for example, files, mail slots, and pipes). Objects created by other functions such as socket can also be associated with an I/O completion port. For an example using sockets, see AcceptEx. A handle can be associated with only one I/O completion port, and after the association is made, the handle remains associated with that I/O completion port until it is closed.

...

The I/O completion port handle and every file handle associated with that particular I/O completion port are known as references to the I/O completion port. The I/O completion port is released when there are no more references to it. Therefore, all of these handles must be properly closed to release the I/O completion port and its associated system resources. After these conditions are satisfied, close the I/O completion port handle by calling the CloseHandle function.




回答2:


Microsoft obviously doesn’t want ordinary users to do it, but nonetheless, there is an officially documented way (and it only required a madton of digging to find it):

Calling NtSetInformationFile, passing the value FileReplaceCompletionInformation for the FileInformationClass parameter, will get it done. (This value is defined in FILE_INFORMATION_CLASS)

A copy of the description for this parameter value (emphasis mine):

Change or remove the I/O completion port for the specified file handle. The caller supplies a pointer to a FILE_COMPLETION_INFORMATION structure that specifies a port handle and a completion key. If the port handle is non-NULL, this handle specifies a new I/O completion port to associate with the file handle. To remove the I/O completion port associated with the file handle, set the port handle in the structure to NULL. To get a port handle, a user-mode caller can call the CreateIoCompletionPort function.



来源:https://stackoverflow.com/questions/30688028/un-associate-socket-from-completion-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!