2 * PROJECT: ReactOS Automatic Testing Utility
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Class that managed an unidirectional anonymous byte stream pipe
5 * COPYRIGHT: Copyright 2015 Thomas Faber <thomas.faber@reactos.org>
11 * Constructs a CPipe object and initializes read and write handles.
15 SECURITY_ATTRIBUTES SecurityAttributes
;
17 SecurityAttributes
.nLength
= sizeof(SecurityAttributes
);
18 SecurityAttributes
.bInheritHandle
= TRUE
;
19 SecurityAttributes
.lpSecurityDescriptor
= NULL
;
21 if(!CreatePipe(&m_hReadPipe
, &m_hWritePipe
, &SecurityAttributes
, 0))
22 FATAL("CreatePipe failed\n");
26 * Destructs a CPipe object and closes all open handles.
31 CloseHandle(m_hReadPipe
);
33 CloseHandle(m_hWritePipe
);
37 * Closes a CPipe's read pipe, leaving the write pipe unchanged.
40 CPipe::CloseReadPipe()
43 FATAL("Trying to close already closed read pipe");
44 CloseHandle(m_hReadPipe
);
48 * Closes a CPipe's write pipe, leaving the read pipe unchanged.
51 CPipe::CloseWritePipe()
54 FATAL("Trying to close already closed write pipe");
55 CloseHandle(m_hWritePipe
);
59 * Reads data from a pipe without advancing the read offset and/or retrieves information about available data.
61 * This function must not be called after CloseReadPipe.
64 * An optional buffer to read pipe data into.
67 * The size of the buffer specified in Buffer, or 0 if no read should be performed.
70 * On return, the number of bytes actually read from the pipe into Buffer.
72 * @param TotalBytesAvailable
73 * On return, the total number of bytes available to read from the pipe.
76 * True on success, false on failure; call GetLastError for error information.
81 CPipe::Peek(PVOID Buffer
, DWORD BufferSize
, PDWORD BytesRead
, PDWORD TotalBytesAvailable
)
84 FATAL("Trying to peek from a closed read pipe");
86 return PeekNamedPipe(m_hReadPipe
, Buffer
, BufferSize
, BytesRead
, TotalBytesAvailable
, NULL
);
90 * Reads data from the read pipe, advancing the read offset accordingly.
92 * This function must not be called after CloseReadPipe.
95 * Buffer to read pipe data into.
97 * @param NumberOfBytesToRead
98 * The number of bytes to read into Buffer.
100 * @param NumberOfBytesRead
101 * On return, the number of bytes actually read from the pipe into Buffer.
104 * True on success, false on failure; call GetLastError for error information.
109 CPipe::Read(PVOID Buffer
, DWORD NumberOfBytesToRead
, PDWORD NumberOfBytesRead
)
112 FATAL("Trying to read from a closed read pipe");
114 return ReadFile(m_hReadPipe
, Buffer
, NumberOfBytesToRead
, NumberOfBytesRead
, NULL
);
118 * Writes data to the write pipe.
120 * This function must not be called after CloseWritePipe.
123 * Buffer containing the data to write.
125 * @param NumberOfBytesToWrite
126 * The number of bytes to write to the pipe from Buffer.
128 * @param NumberOfBytesWritten
129 * On return, the number of bytes actually written to the pipe.
132 * True on success, false on failure; call GetLastError for error information.
137 CPipe::Write(LPCVOID Buffer
, DWORD NumberOfBytesToWrite
, PDWORD NumberOfBytesWritten
)
140 FATAL("Trying to write to a closed write pipe");
142 return WriteFile(m_hWritePipe
, Buffer
, NumberOfBytesToWrite
, NumberOfBytesWritten
, NULL
);