ReactOS  0.4.14-dev-1338-g0d187f7
CPipe Class Reference

#include <CPipe.h>

Collaboration diagram for CPipe:

Public Member Functions

 CPipe ()
 
 ~CPipe ()
 
void CloseReadPipe ()
 
void CloseWritePipe ()
 
bool Peek (PVOID Buffer, DWORD BufferSize, PDWORD BytesRead, PDWORD TotalBytesAvailable)
 
DWORD Read (PVOID Buffer, DWORD NumberOfBytesToRead, PDWORD NumberOfBytesRead, DWORD TimeoutMilliseconds)
 
bool Write (LPCVOID Buffer, DWORD NumberOfBytesToWrite, PDWORD NumberOfBytesWritten)
 

Private Attributes

OVERLAPPED m_ReadOverlapped
 
HANDLE m_hReadPipe
 
HANDLE m_hWritePipe
 

Static Private Attributes

static LONG m_lPipeCount = 0
 

Friends

class CPipedProcess
 

Detailed Description

Definition at line 9 of file CPipe.h.

Constructor & Destructor Documentation

◆ CPipe()

CPipe::CPipe ( )

Constructs a CPipe object and initializes read and write handles.

Definition at line 17 of file CPipe.cpp.

18 {
19  SECURITY_ATTRIBUTES SecurityAttributes;
20 
21  SecurityAttributes.nLength = sizeof(SecurityAttributes);
22  SecurityAttributes.bInheritHandle = TRUE;
23  SecurityAttributes.lpSecurityDescriptor = NULL;
24 
25  // Construct a unique pipe name.
26  WCHAR wszPipeName[MAX_PATH];
28  swprintf(wszPipeName, L"\\\\.\\pipe\\TestmanPipe%ld", m_lPipeCount);
29 
30  // Create a named pipe with the default settings, but overlapped (asynchronous) operations.
31  // Latter feature is why we can't simply use CreatePipe.
32  const DWORD dwDefaultBufferSize = 4096;
33  const DWORD dwDefaultTimeoutMilliseconds = 120000;
34 
35  m_hReadPipe = CreateNamedPipeW(wszPipeName,
38  1,
39  dwDefaultBufferSize,
40  dwDefaultBufferSize,
41  dwDefaultTimeoutMilliseconds,
42  &SecurityAttributes);
44  {
45  FATAL("CreateNamedPipe failed\n");
46  }
47 
48  // Use CreateFileW to get the write handle to the pipe.
49  // Writing is done synchronously, so no FILE_FLAG_OVERLAPPED here!
50  m_hWritePipe = CreateFileW(wszPipeName,
52  0,
53  &SecurityAttributes,
56  NULL);
58  {
59  FATAL("CreateFileW failed\n");
60  }
61 
62  // Prepare the OVERLAPPED structure for reading.
66  {
67  FATAL("CreateEvent failed\n");
68  }
69 }
#define FATAL(Message)
Definition: precomp.h:57
#define TRUE
Definition: types.h:120
#define PIPE_WAIT
Definition: winbase.h:171
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define ZeroMemory
Definition: winbase.h:1648
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:651
#define GENERIC_WRITE
Definition: nt_native.h:90
#define PIPE_TYPE_BYTE
Definition: winbase.h:167
HANDLE hEvent
Definition: winbase.h:798
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
__wchar_t WCHAR
Definition: xmlstorage.h:180
OVERLAPPED m_ReadOverlapped
Definition: CPipe.h:14
#define MAX_PATH
Definition: compat.h:26
#define swprintf(buf, format,...)
Definition: sprintf.c:56
unsigned long DWORD
Definition: ntddk_ex.h:95
static LONG m_lPipeCount
Definition: CPipe.h:12
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1250
HANDLE m_hReadPipe
Definition: CPipe.h:15
#define InterlockedIncrement
Definition: armddk.h:53
LPVOID lpSecurityDescriptor
Definition: compat.h:181
#define PIPE_ACCESS_INBOUND
Definition: winbase.h:165
#define CreateFileW
Definition: compat.h:408
HANDLE m_hWritePipe
Definition: CPipe.h:16
HANDLE WINAPI CreateNamedPipeW(LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: npipe.c:246
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46
#define PIPE_READMODE_BYTE
Definition: winbase.h:169

◆ ~CPipe()

CPipe::~CPipe ( )

Destructs a CPipe object and closes all open handles.

Definition at line 74 of file CPipe.cpp.

75 {
76  if (m_hReadPipe)
78  if (m_hWritePipe)
80 }
#define CloseHandle
Definition: compat.h:406
HANDLE m_hReadPipe
Definition: CPipe.h:15
HANDLE m_hWritePipe
Definition: CPipe.h:16

Member Function Documentation

◆ CloseReadPipe()

void CPipe::CloseReadPipe ( )

Closes a CPipe's read pipe, leaving the write pipe unchanged.

Definition at line 86 of file CPipe.cpp.

87 {
88  if (!m_hReadPipe)
89  FATAL("Trying to close already closed read pipe\n");
91  m_hReadPipe = NULL;
92 }
#define FATAL(Message)
Definition: precomp.h:57
#define CloseHandle
Definition: compat.h:406
smooth NULL
Definition: ftsmooth.c:416
HANDLE m_hReadPipe
Definition: CPipe.h:15

◆ CloseWritePipe()

void CPipe::CloseWritePipe ( )

Closes a CPipe's write pipe, leaving the read pipe unchanged.

Definition at line 98 of file CPipe.cpp.

99 {
100  if (!m_hWritePipe)
101  FATAL("Trying to close already closed write pipe\n");
103  m_hWritePipe = NULL;
104 }
#define FATAL(Message)
Definition: precomp.h:57
#define CloseHandle
Definition: compat.h:406
smooth NULL
Definition: ftsmooth.c:416
HANDLE m_hWritePipe
Definition: CPipe.h:16

Referenced by CPipedProcess::CPipedProcess().

◆ Peek()

bool CPipe::Peek ( PVOID  Buffer,
DWORD  BufferSize,
PDWORD  BytesRead,
PDWORD  TotalBytesAvailable 
)

Reads data from a pipe without advancing the read offset and/or retrieves information about available data.

This function must not be called after CloseReadPipe.

Parameters
BufferAn optional buffer to read pipe data into.
BufferSizeThe size of the buffer specified in Buffer, or 0 if no read should be performed.
BytesReadOn return, the number of bytes actually read from the pipe into Buffer.
TotalBytesAvailableOn return, the total number of bytes available to read from the pipe.
Returns
True on success, false on failure; call GetLastError for error information.
See also
PeekNamedPipe

Definition at line 129 of file CPipe.cpp.

130 {
131  if (!m_hReadPipe)
132  FATAL("Trying to peek from a closed read pipe\n");
133 
134  return PeekNamedPipe(m_hReadPipe, Buffer, BufferSize, BytesRead, TotalBytesAvailable, NULL);
135 }
#define FATAL(Message)
Definition: precomp.h:57
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
#define BufferSize
Definition: classpnp.h:419
HANDLE m_hReadPipe
Definition: CPipe.h:15
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesRead
Definition: fltkernel.h:1255
BOOL WINAPI PeekNamedPipe(HANDLE hNamedPipe, LPVOID lpBuffer, DWORD nBufferSize, LPDWORD lpBytesRead, LPDWORD lpTotalBytesAvail, LPDWORD lpBytesLeftThisMessage)
Definition: npipe.c:1214

Referenced by CWineTest::DoListCommand().

◆ Read()

DWORD CPipe::Read ( PVOID  Buffer,
DWORD  NumberOfBytesToRead,
PDWORD  NumberOfBytesRead,
DWORD  TimeoutMilliseconds 
)

Reads data from the read pipe, advancing the read offset accordingly.

This function must not be called after CloseReadPipe.

Parameters
BufferBuffer to read pipe data into.
NumberOfBytesToReadThe number of bytes to read into Buffer.
NumberOfBytesReadOn return, the number of bytes actually read from the pipe into Buffer.
Returns
Returns a Win32 error code. Expected error codes include:
  • ERROR_SUCCESS: The read has completed successfully.
  • WAIT_TIMEOUT: The given timeout has elapsed before any data was read.
  • ERROR_BROKEN_PIPE: The other end of the pipe has been closed.
See also
ReadFile

Definition at line 160 of file CPipe.cpp.

161 {
162  if (!m_hReadPipe)
163  {
164  FATAL("Trying to read from a closed read pipe\n");
165  }
166 
167  if (ReadFile(m_hReadPipe, Buffer, NumberOfBytesToRead, NumberOfBytesRead, &m_ReadOverlapped))
168  {
169  // The asynchronous read request could be satisfied immediately.
170  return ERROR_SUCCESS;
171  }
172 
173  DWORD dwLastError = GetLastError();
174  if (dwLastError == ERROR_IO_PENDING)
175  {
176  // The asynchronous read request could not be satisfied immediately, so wait for it with the given timeout.
177  DWORD dwWaitResult = WaitForSingleObject(m_ReadOverlapped.hEvent, TimeoutMilliseconds);
178  if (dwWaitResult == WAIT_OBJECT_0)
179  {
180  // Fill NumberOfBytesRead.
181  if (GetOverlappedResult(m_hReadPipe, &m_ReadOverlapped, NumberOfBytesRead, FALSE))
182  {
183  // We successfully read NumberOfBytesRead bytes.
184  return ERROR_SUCCESS;
185  }
186 
187  dwLastError = GetLastError();
188  if (dwLastError == ERROR_BROKEN_PIPE)
189  {
190  // The other end of the pipe has been closed.
191  return ERROR_BROKEN_PIPE;
192  }
193  else
194  {
195  // An unexpected error.
196  FATAL("GetOverlappedResult failed\n");
197  }
198  }
199  else
200  {
201  // This may be WAIT_TIMEOUT or an unexpected error.
202  return dwWaitResult;
203  }
204  }
205  else
206  {
207  // This may be ERROR_BROKEN_PIPE or an unexpected error.
208  return dwLastError;
209  }
210 }
#define FATAL(Message)
Definition: precomp.h:57
#define ERROR_SUCCESS
Definition: deptool.c:10
BOOL WINAPI GetOverlappedResult(IN HANDLE hFile, IN LPOVERLAPPED lpOverlapped, OUT LPDWORD lpNumberOfBytesTransferred, IN BOOL bWait)
Definition: iocompl.c:204
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ERROR_IO_PENDING
Definition: dderror.h:15
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
HANDLE hEvent
Definition: winbase.h:798
Definition: bufpool.h:45
#define WAIT_OBJECT_0
Definition: winbase.h:387
OVERLAPPED m_ReadOverlapped
Definition: CPipe.h:14
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_BROKEN_PIPE
Definition: winerror.h:183
HANDLE m_hReadPipe
Definition: CPipe.h:15
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123

Referenced by CWineTest::DoListCommand(), and CWineTest::RunTest().

◆ Write()

bool CPipe::Write ( LPCVOID  Buffer,
DWORD  NumberOfBytesToWrite,
PDWORD  NumberOfBytesWritten 
)

Writes data to the write pipe.

This function must not be called after CloseWritePipe.

Parameters
BufferBuffer containing the data to write.
NumberOfBytesToWriteThe number of bytes to write to the pipe from Buffer.
NumberOfBytesWrittenOn return, the number of bytes actually written to the pipe.
Returns
True on success, false on failure; call GetLastError for error information.
See also
WriteFile

Definition at line 232 of file CPipe.cpp.

233 {
234  if (!m_hWritePipe)
235  FATAL("Trying to write to a closed write pipe\n");
236 
237  return WriteFile(m_hWritePipe, Buffer, NumberOfBytesToWrite, NumberOfBytesWritten, NULL);
238 }
#define FATAL(Message)
Definition: precomp.h:57
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
HANDLE m_hWritePipe
Definition: CPipe.h:16

Friends And Related Function Documentation

◆ CPipedProcess

friend class CPipedProcess
friend

Definition at line 29 of file CPipe.h.

Member Data Documentation

◆ m_hReadPipe

HANDLE CPipe::m_hReadPipe
private

Definition at line 15 of file CPipe.h.

Referenced by CloseReadPipe(), CPipe(), Peek(), Read(), and ~CPipe().

◆ m_hWritePipe

HANDLE CPipe::m_hWritePipe
private

Definition at line 16 of file CPipe.h.

Referenced by CloseWritePipe(), CPipe(), CPipedProcess::InitStartupInfo(), Write(), and ~CPipe().

◆ m_lPipeCount

LONG CPipe::m_lPipeCount = 0
staticprivate

Definition at line 12 of file CPipe.h.

Referenced by CPipe().

◆ m_ReadOverlapped

OVERLAPPED CPipe::m_ReadOverlapped
private

Definition at line 14 of file CPipe.h.

Referenced by CPipe(), and Read().


The documentation for this class was generated from the following files: