“Un-associate” socket from completion port

2019-07-11 05:31发布

问题:

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.