2013-07-23 02:20:34 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/pipe.h>
|
|
|
|
#include <winpr/file.h>
|
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/winpr.h>
|
2013-07-23 06:53:44 +04:00
|
|
|
#include <winpr/print.h>
|
2013-07-23 02:20:34 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
#define PIPE_BUFFER_SIZE 32
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
static HANDLE ReadyEvent;
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
static LPTSTR lpszPipeName = _T("\\\\.\\pipe\\winpr_test_pipe");
|
|
|
|
|
|
|
|
static void* named_pipe_client_thread(void* arg)
|
|
|
|
{
|
|
|
|
HANDLE hNamedPipe;
|
2013-07-23 06:53:44 +04:00
|
|
|
BYTE* lpReadBuffer;
|
|
|
|
BYTE* lpWriteBuffer;
|
|
|
|
BOOL fSuccess = FALSE;
|
|
|
|
DWORD nNumberOfBytesToRead;
|
|
|
|
DWORD nNumberOfBytesToWrite;
|
|
|
|
DWORD lpNumberOfBytesRead;
|
|
|
|
DWORD lpNumberOfBytesWritten;
|
|
|
|
|
|
|
|
WaitForSingleObject(ReadyEvent, INFINITE);
|
2013-07-23 02:20:34 +04:00
|
|
|
|
|
|
|
hNamedPipe = CreateFile(lpszPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
if (!hNamedPipe)
|
|
|
|
{
|
|
|
|
printf("Named Pipe CreateFile failure: NULL handle\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hNamedPipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
printf("Named Pipe CreateFile failure: INVALID_HANDLE_VALUE\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpReadBuffer = (BYTE*) malloc(PIPE_BUFFER_SIZE);
|
|
|
|
lpWriteBuffer = (BYTE*) malloc(PIPE_BUFFER_SIZE);
|
|
|
|
|
|
|
|
lpNumberOfBytesWritten = 0;
|
|
|
|
nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
|
|
|
|
|
2013-07-23 07:17:08 +04:00
|
|
|
FillMemory(lpWriteBuffer, PIPE_BUFFER_SIZE, 0x59);
|
2013-07-23 06:53:44 +04:00
|
|
|
|
|
|
|
fSuccess = WriteFile(hNamedPipe, lpWriteBuffer, nNumberOfBytesToWrite, &lpNumberOfBytesWritten, NULL);
|
|
|
|
|
|
|
|
if (!fSuccess || (lpNumberOfBytesWritten == 0))
|
|
|
|
{
|
|
|
|
printf("Client NamedPipe WriteFile failure\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpNumberOfBytesRead = 0;
|
|
|
|
nNumberOfBytesToRead = PIPE_BUFFER_SIZE;
|
|
|
|
|
|
|
|
ZeroMemory(lpReadBuffer, PIPE_BUFFER_SIZE);
|
|
|
|
|
|
|
|
fSuccess = ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, &lpNumberOfBytesRead, NULL);
|
|
|
|
|
|
|
|
if (!fSuccess || (lpNumberOfBytesRead == 0))
|
|
|
|
{
|
|
|
|
printf("Client NamedPipe ReadFile failure\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Client ReadFile (%d):\n", lpNumberOfBytesRead);
|
|
|
|
winpr_HexDump(lpReadBuffer, lpNumberOfBytesRead);
|
|
|
|
|
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
|
2013-07-23 07:17:08 +04:00
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* named_pipe_server_thread(void* arg)
|
|
|
|
{
|
|
|
|
HANDLE hNamedPipe;
|
|
|
|
BYTE* lpReadBuffer;
|
|
|
|
BYTE* lpWriteBuffer;
|
|
|
|
BOOL fSuccess = FALSE;
|
|
|
|
BOOL fConnected = FALSE;
|
|
|
|
DWORD nNumberOfBytesToRead;
|
|
|
|
DWORD nNumberOfBytesToWrite;
|
|
|
|
DWORD lpNumberOfBytesRead;
|
|
|
|
DWORD lpNumberOfBytesWritten;
|
|
|
|
|
|
|
|
hNamedPipe = CreateNamedPipe(lpszPipeName,
|
2013-07-23 23:03:08 +04:00
|
|
|
PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
|
2013-07-23 02:20:34 +04:00
|
|
|
PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 0, NULL);
|
|
|
|
|
|
|
|
if (!hNamedPipe)
|
|
|
|
{
|
|
|
|
printf("CreateNamedPipe failure: NULL handle\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hNamedPipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
printf("CreateNamedPipe failure: INVALID_HANDLE_VALUE\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
SetEvent(ReadyEvent);
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
fConnected = ConnectNamedPipe(hNamedPipe, NULL);
|
|
|
|
|
|
|
|
if (!fConnected)
|
|
|
|
fConnected = (GetLastError() == ERROR_PIPE_CONNECTED);
|
|
|
|
|
|
|
|
if (!fConnected)
|
|
|
|
{
|
|
|
|
printf("ConnectNamedPipe failure\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpReadBuffer = (BYTE*) malloc(PIPE_BUFFER_SIZE);
|
|
|
|
lpWriteBuffer = (BYTE*) malloc(PIPE_BUFFER_SIZE);
|
|
|
|
|
|
|
|
lpNumberOfBytesRead = 0;
|
|
|
|
nNumberOfBytesToRead = PIPE_BUFFER_SIZE;
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
ZeroMemory(lpReadBuffer, PIPE_BUFFER_SIZE);
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
fSuccess = ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, &lpNumberOfBytesRead, NULL);
|
|
|
|
|
|
|
|
if (!fSuccess || (lpNumberOfBytesRead == 0))
|
|
|
|
{
|
2013-07-23 06:53:44 +04:00
|
|
|
printf("Server NamedPipe ReadFile failure\n");
|
2013-07-23 02:20:34 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
printf("Server ReadFile (%d):\n", lpNumberOfBytesRead);
|
|
|
|
winpr_HexDump(lpReadBuffer, lpNumberOfBytesRead);
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
lpNumberOfBytesWritten = 0;
|
|
|
|
nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
|
|
|
|
|
2013-07-23 07:17:08 +04:00
|
|
|
FillMemory(lpWriteBuffer, PIPE_BUFFER_SIZE, 0x45);
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
fSuccess = WriteFile(hNamedPipe, lpWriteBuffer, nNumberOfBytesToWrite, &lpNumberOfBytesWritten, NULL);
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
if (!fSuccess || (lpNumberOfBytesWritten == 0))
|
2013-07-23 02:20:34 +04:00
|
|
|
{
|
2013-07-23 06:53:44 +04:00
|
|
|
printf("Server NamedPipe WriteFile failure\n");
|
2013-07-23 02:20:34 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
|
2013-07-23 07:17:08 +04:00
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestPipeCreateNamedPipe(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
HANDLE ClientThread;
|
|
|
|
HANDLE ServerThread;
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
ReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
ClientThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) named_pipe_client_thread, NULL, 0, NULL);
|
|
|
|
ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) named_pipe_server_thread, NULL, 0, NULL);
|
|
|
|
|
|
|
|
WaitForSingleObject(ClientThread, INFINITE);
|
|
|
|
WaitForSingleObject(ServerThread, INFINITE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|