名前付きパイプのトランザクション
名前付きパイプ トランザクションは、書き込み操作と読み取り操作を 1 つのネットワーク操作に組み合わせたクライアント/サーバー通信です。 トランザクションは、双方向のメッセージ型パイプでのみ使用できます。 トランザクションにより、クライアントとリモート サーバー間のネットワーク通信のパフォーマンスが向上します。 プロセスでは、TransactNamedPipe と CallNamedPipe関数を使用して、名前付きパイプ トランザクションを実行できます。
TransactNamedPipe 関数は、名前付きパイプ サーバーに要求メッセージを書き込み、サーバーの応答メッセージを読み取るためにパイプ クライアントによって最も一般的に使用されます。 パイプ クライアントは、GENERIC_READ | を指定する必要があります。CreateFile 関数を呼び出して、パイプ ハンドルを開いたときにアクセスGENERIC_WRITE。 次に、パイプ クライアントは、SetNamedPipeHandleState 関数を呼び出すことによって、パイプ ハンドルをメッセージ読み取りモードに設定します。 TransactNamedPipe の呼び出しで指定された読み取りバッファーが、サーバーによって書き込まれたメッセージ全体を保持するのに十分な大きさでない場合、関数は 0 を返し、GetLastErrorERROR_MORE_DATAを返します。 クライアントは、ReadFile、ReadFileEx、または PeekNamedPipe関数を呼び出して、メッセージの残りの部分読み取ることができます。
TransactNamedPipe は通常、パイプ クライアントによって呼び出されますが、パイプ サーバーでも使用できます。
次の例は、TransactNamedPipe使用するパイプ クライアントを示しています。 このパイプ クライアントは、[関連項目] の下に一覧表示されている任意のパイプ サーバーと共に使用できます。
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUFSIZE 512
int _tmain(int argc, TCHAR *argv[])
{
HANDLE hPipe;
LPTSTR lpszWrite = TEXT("Default message from client");
TCHAR chReadBuf[BUFSIZE];
BOOL fSuccess;
DWORD cbRead, dwMode;
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe");
if( argc > 1)
{
lpszWrite = argv[1];
}
// Try to open a named pipe; wait for it, if necessary.
while (1)
{
hPipe = CreateFile(
lpszPipename, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid.
if (hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
printf("Could not open pipe\n");
return 0;
}
// All pipe instances are busy, so wait for 20 seconds.
if (! WaitNamedPipe(lpszPipename, 20000) )
{
printf("Could not open pipe\n");
return 0;
}
}
// The pipe connected; change to message-read mode.
dwMode = PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
if (!fSuccess)
{
printf("SetNamedPipeHandleState failed.\n");
return 0;
}
// Send a message to the pipe server and read the response.
fSuccess = TransactNamedPipe(
hPipe, // pipe handle
lpszWrite, // message to server
(lstrlen(lpszWrite)+1)*sizeof(TCHAR), // message length
chReadBuf, // buffer to receive reply
BUFSIZE*sizeof(TCHAR), // size of read buffer
&cbRead, // bytes read
NULL); // not overlapped
if (!fSuccess && (GetLastError() != ERROR_MORE_DATA))
{
printf("TransactNamedPipe failed.\n");
return 0;
}
while(1)
{
_tprintf(TEXT("%s\n"), chReadBuf);
// 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
BUFSIZE*sizeof(TCHAR), // 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;
else _tprintf( TEXT("%s\n"), chReadBuf);
}
_getch();
CloseHandle(hPipe);
return 0;
}
パイプ クライアントは、CallNamedPipe を使用して、CreateFile、WaitNamedPipe(必要な場合)、TransactNamedPipe、CloseHandle関数呼び出しを 1 回の呼び出しにします。 関数が戻る前にパイプ ハンドルが閉じられるため、メッセージが指定されたサイズの読み取りバッファーより大きい場合、メッセージ内の追加バイトは失われます。 次の例は、CallNamedPipe 使用するように書き換えた前の例です。
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUFSIZE 512
int _tmain(int argc, TCHAR *argv[])
{
LPTSTR lpszWrite = TEXT("Default message from client");
TCHAR chReadBuf[BUFSIZE];
BOOL fSuccess;
DWORD cbRead;
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe");
if( argc > 1)
{
lpszWrite = argv[1];
}
fSuccess = CallNamedPipe(
lpszPipename, // pipe name
lpszWrite, // message to server
(lstrlen(lpszWrite)+1)*sizeof(TCHAR), // message length
chReadBuf, // buffer to receive reply
BUFSIZE*sizeof(TCHAR), // size of read buffer
&cbRead, // number of bytes read
20000); // waits for 20 seconds
if (fSuccess || GetLastError() == ERROR_MORE_DATA)
{
_tprintf( TEXT("%s\n"), chReadBuf );
// The pipe is closed; no more data can be read.
if (! fSuccess)
{
printf("\nExtra data in message was lost\n");
}
}
_getch();
return 0;
}
関連トピック
-
マルチスレッド パイプ サーバー の
-
重複した I/O を使用した名前付きパイプ サーバーの
-
完了ルーチン を使用した名前付きパイプ サーバーの