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
);
49 * Closes a CPipe's write pipe, leaving the read pipe unchanged.
52 CPipe::CloseWritePipe()
55 FATAL("Trying to close already closed write pipe");
56 CloseHandle(m_hWritePipe
);
61 * Reads data from a pipe without advancing the read offset and/or retrieves information about available data.
63 * This function must not be called after CloseReadPipe.
66 * An optional buffer to read pipe data into.
69 * The size of the buffer specified in Buffer, or 0 if no read should be performed.
72 * On return, the number of bytes actually read from the pipe into Buffer.
74 * @param TotalBytesAvailable
75 * On return, the total number of bytes available to read from the pipe.
78 * True on success, false on failure; call GetLastError for error information.
83 CPipe::Peek(PVOID Buffer
, DWORD BufferSize
, PDWORD BytesRead
, PDWORD TotalBytesAvailable
)
86 FATAL("Trying to peek from a closed read pipe");
88 return PeekNamedPipe(m_hReadPipe
, Buffer
, BufferSize
, BytesRead
, TotalBytesAvailable
, NULL
);
92 * Reads data from the read pipe, advancing the read offset accordingly.
94 * This function must not be called after CloseReadPipe.
97 * Buffer to read pipe data into.
99 * @param NumberOfBytesToRead
100 * The number of bytes to read into Buffer.
102 * @param NumberOfBytesRead
103 * On return, the number of bytes actually read from the pipe into Buffer.
106 * True on success, false on failure; call GetLastError for error information.
111 CPipe::Read(PVOID Buffer
, DWORD NumberOfBytesToRead
, PDWORD NumberOfBytesRead
)
114 FATAL("Trying to read from a closed read pipe");
116 return ReadFile(m_hReadPipe
, Buffer
, NumberOfBytesToRead
, NumberOfBytesRead
, NULL
);
120 * Writes data to the write pipe.
122 * This function must not be called after CloseWritePipe.
125 * Buffer containing the data to write.
127 * @param NumberOfBytesToWrite
128 * The number of bytes to write to the pipe from Buffer.
130 * @param NumberOfBytesWritten
131 * On return, the number of bytes actually written to the pipe.
134 * True on success, false on failure; call GetLastError for error information.
139 CPipe::Write(LPCVOID Buffer
, DWORD NumberOfBytesToWrite
, PDWORD NumberOfBytesWritten
)
142 FATAL("Trying to write to a closed write pipe");
144 return WriteFile(m_hWritePipe
, Buffer
, NumberOfBytesToWrite
, NumberOfBytesWritten
, NULL
);