The CreateIoCompletionPort function allows the creation of a new I/O completion port and the registration of file handles to an existing I/O completion port.
Then, I can use any function, like a recv on a socket or a ReadFile on a file with a OVERLAPPED structure to start an asynchronous operation.
I have to check whether the function call returned synchronously although it was called with an OVERLAPPED structure and in this case handle it directly. In the other case, when ERROR_IO_PENDING is returned, I can use the GetQueuedCompletionStatus function to be notified when the operation completes.
The question which arise are:
How can I remove a handle from the I/O completion port? For example, when I add sockets to the IOCP, how can I remove closed ones? Should I just re-register another socket with the same completion key?
Also, is there a way to make the calls ALWAYS go over the I/O completion port and don't return synchronously?
And finally, is it possible for example to
recvasynchronously but tosendsynchronously? For example when a simple echo service is implemented: Can I wait with an asynchronousrecvfor new data butsendthe response in a synchronous way so that code complexity is reduced? In my case, I wouldn'trecva second time anyways before the first request was processed.What happens if an asynchronous
ReadFilehas been requested, but before it completes, aWriteFileto the same file should be processed. Will theReadFilebe cancelled with an error message and I have to restart the read process as soon as the write is complete? Or do I have to cancel theReadFilemanually before writing? This question arises in combination with a communication device; so, the write and read should not do problems if happening concurrently.