Transactions on Named Pipes

A named pipe transaction is a client-server communication that combines a write operation and a read operation into a single network operation. A transaction can be used only on a duplex, message-type pipe. Transactions improve the performance of network communications between a client and a remote server. Processes can use the TransactNamedPipe and CallNamedPipe functions to perform named pipe transactions.

TransactNamedPipe is typically used by a client process to write a request message to the named pipe server and read the server's response message. The client process must specify GENERIC_READ | GENERIC_WRITE access when it opens its pipe handle by calling the CreateFile function. Then, the client process sets the pipe handle to message-read mode by calling the SetNamedPipeHandleState function. If the read buffer specified in the call to TransactNamedPipe is not large enough to hold the entire message written by the server, the function returns FALSE and GetLastError returns ERROR_MORE_DATA. The client can read the remainder of the message by calling either the ReadFile, ReadFileEx, or PeekNamedPipe function.

TransactNamedPipe is typically called by client processes, but can also be used by a server process.

The following example shows a client process using TransactNamedPipe. The example assumes that the client process has used CreateFile to connect to the pipe and SetNamedPipeHandleState to set the pipe handle's read mode, as shown in the client process example in the preceding section.

fSuccess = TransactNamedPipe(

hPipe, // pipe handle

lpszWrite, // message to server

strlen(lpszWrite)+1, // message length

chReadBuf, // buffer to receive reply

512, // size of read buffer

&cbRead, // number of bytes read

NULL); // not overlapped

// Exit if an error occurs, unless the error indicates there is more

// data in the message.

if (!fSuccess && (GetLastError() != ERROR_MORE_DATA))

MyErrExit("TransactNamedPipe");

while(1)

{

// Data from the pipe is written to STDOUT.

if (! WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),

chReadBuf, cbRead, &cbWritten, NULL) )

break;

// Break if TransactNamedPipe or ReadFile is successful.

if (fSuccess)

break;

// Read from the pipe if there is more data in the message.

fSuccess = ReadFile(

hPipe, // pipe handle

chReadBuf, // buffer to receive reply

512, // size of buffer

&cbRead, // number of bytes read

NULL); // not overlapped

// Exit if an error other than ERROR_MORE_DATA occurs.

if (! fSuccess && (GetLastError() != ERROR_MORE_DATA))

break;

}

A client process uses CallNamedPipe to combine calling the CreateFile, WaitNamedPipe (if necessary), TransactNamedPipe, and CloseHandle function into a single call. Because the pipe handle is closed before the function returns, any additional bytes in the message are lost if the message is larger than the specified size of the read buffer. The following example shows the use of CallNamedPipe.

// Combines connect, wait, write, read, and close operations.

fSuccess = CallNamedPipe(

lpszPipename, // pipe name

lpszWrite, // message to server

strlen(lpszWrite)+1, // message length

chReadBuf, // buffer to receive reply

512, // size of read buffer

&cbRead, // number of bytes read

20000); // waits for 20 seconds

if (fSuccess || GetLastError() == ERROR_MORE_DATA)

{

// Data from the pipe is written to STDOUT.

WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),

chReadBuf, cbRead, &cbWritten, NULL);

// The pipe is closed, so no more bytes can be read from the

// message.

if (! fSuccess)

printf("\n...extra data in message was lost\n");

}