- 1 year ago
Reading named Pipe using overlapped/asynchronous i/o
To read a named pipe using overlapped/asynchronous I/O in Windows, you can follow these steps:
Create a named pipe using the
CreateNamedPipe()
function. This function returns a handle to the named pipe that you can use to communicate with the client.Use the
ConnectNamedPipe()
function to wait for a client to connect to the named pipe. This function blocks until a client connects.Use the
CreateIoCompletionPort()
function to create an I/O completion port. This port is used to receive completion notifications for asynchronous I/O operations.Use the
CreateFile()
function to open the named pipe for asynchronous I/O. Set theFILE_FLAG_OVERLAPPED
flag to enable overlapped I/O.Use the
ReadFile()
function to initiate an asynchronous read operation. Pass a pointer to anOVERLAPPED
structure as thelpOverlapped
parameter. Set thehEvent
member of theOVERLAPPED
structure to a handle to the I/O completion port created in step 3.Wait for the completion notification by calling the
GetQueuedCompletionStatus()
function. This function blocks until an I/O operation is complete or until a timeout occurs.When the
GetQueuedCompletionStatus()
function returns, check the result of the I/O operation by examining thelpNumberOfBytesTransferred
andlpOverlapped
parameters.If the I/O operation was successful, read the data from the buffer.
Repeat steps 5-8 to read additional data from the named pipe.
When finished, use the
DisconnectNamedPipe()
function to disconnect the client and close the named pipe handle.