[ROSAUTOTEST]
[reactos.git] / rostests / rosautotest / CPipe.cpp
1 /*
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>
6 */
7
8 #include "precomp.h"
9
10 /**
11 * Constructs a CPipe object and initializes read and write handles.
12 */
13 CPipe::CPipe()
14 {
15 SECURITY_ATTRIBUTES SecurityAttributes;
16
17 SecurityAttributes.nLength = sizeof(SecurityAttributes);
18 SecurityAttributes.bInheritHandle = TRUE;
19 SecurityAttributes.lpSecurityDescriptor = NULL;
20
21 if(!CreatePipe(&m_hReadPipe, &m_hWritePipe, &SecurityAttributes, 0))
22 FATAL("CreatePipe failed\n");
23 }
24
25 /**
26 * Destructs a CPipe object and closes all open handles.
27 */
28 CPipe::~CPipe()
29 {
30 if (m_hReadPipe)
31 CloseHandle(m_hReadPipe);
32 if (m_hWritePipe)
33 CloseHandle(m_hWritePipe);
34 }
35
36 /**
37 * Closes a CPipe's read pipe, leaving the write pipe unchanged.
38 */
39 void
40 CPipe::CloseReadPipe()
41 {
42 if (!m_hReadPipe)
43 FATAL("Trying to close already closed read pipe");
44 CloseHandle(m_hReadPipe);
45 }
46
47 /**
48 * Closes a CPipe's write pipe, leaving the read pipe unchanged.
49 */
50 void
51 CPipe::CloseWritePipe()
52 {
53 if (!m_hWritePipe)
54 FATAL("Trying to close already closed write pipe");
55 CloseHandle(m_hWritePipe);
56 }
57
58 /**
59 * Reads data from a pipe without advancing the read offset and/or retrieves information about available data.
60 *
61 * This function must not be called after CloseReadPipe.
62 *
63 * @param Buffer
64 * An optional buffer to read pipe data into.
65 *
66 * @param BufferSize
67 * The size of the buffer specified in Buffer, or 0 if no read should be performed.
68 *
69 * @param BytesRead
70 * On return, the number of bytes actually read from the pipe into Buffer.
71 *
72 * @param TotalBytesAvailable
73 * On return, the total number of bytes available to read from the pipe.
74 *
75 * @return
76 * True on success, false on failure; call GetLastError for error information.
77 *
78 * @see PeekNamedPipe
79 */
80 bool
81 CPipe::Peek(PVOID Buffer, DWORD BufferSize, PDWORD BytesRead, PDWORD TotalBytesAvailable)
82 {
83 if (!m_hReadPipe)
84 FATAL("Trying to peek from a closed read pipe");
85
86 return PeekNamedPipe(m_hReadPipe, Buffer, BufferSize, BytesRead, TotalBytesAvailable, NULL);
87 }
88
89 /**
90 * Reads data from the read pipe, advancing the read offset accordingly.
91 *
92 * This function must not be called after CloseReadPipe.
93 *
94 * @param Buffer
95 * Buffer to read pipe data into.
96 *
97 * @param NumberOfBytesToRead
98 * The number of bytes to read into Buffer.
99 *
100 * @param NumberOfBytesRead
101 * On return, the number of bytes actually read from the pipe into Buffer.
102 *
103 * @return
104 * True on success, false on failure; call GetLastError for error information.
105 *
106 * @see ReadFile
107 */
108 bool
109 CPipe::Read(PVOID Buffer, DWORD NumberOfBytesToRead, PDWORD NumberOfBytesRead)
110 {
111 if (!m_hReadPipe)
112 FATAL("Trying to read from a closed read pipe");
113
114 return ReadFile(m_hReadPipe, Buffer, NumberOfBytesToRead, NumberOfBytesRead, NULL);
115 }
116
117 /**
118 * Writes data to the write pipe.
119 *
120 * This function must not be called after CloseWritePipe.
121 *
122 * @param Buffer
123 * Buffer containing the data to write.
124 *
125 * @param NumberOfBytesToWrite
126 * The number of bytes to write to the pipe from Buffer.
127 *
128 * @param NumberOfBytesWritten
129 * On return, the number of bytes actually written to the pipe.
130 *
131 * @return
132 * True on success, false on failure; call GetLastError for error information.
133 *
134 * @see WriteFile
135 */
136 bool
137 CPipe::Write(LPCVOID Buffer, DWORD NumberOfBytesToWrite, PDWORD NumberOfBytesWritten)
138 {
139 if (!m_hWritePipe)
140 FATAL("Trying to write to a closed write pipe");
141
142 return WriteFile(m_hWritePipe, Buffer, NumberOfBytesToWrite, NumberOfBytesWritten, NULL);
143 }