[CDFS]
[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 m_hReadPipe = NULL;
46 }
47
48 /**
49 * Closes a CPipe's write pipe, leaving the read pipe unchanged.
50 */
51 void
52 CPipe::CloseWritePipe()
53 {
54 if (!m_hWritePipe)
55 FATAL("Trying to close already closed write pipe");
56 CloseHandle(m_hWritePipe);
57 m_hWritePipe = NULL;
58 }
59
60 /**
61 * Reads data from a pipe without advancing the read offset and/or retrieves information about available data.
62 *
63 * This function must not be called after CloseReadPipe.
64 *
65 * @param Buffer
66 * An optional buffer to read pipe data into.
67 *
68 * @param BufferSize
69 * The size of the buffer specified in Buffer, or 0 if no read should be performed.
70 *
71 * @param BytesRead
72 * On return, the number of bytes actually read from the pipe into Buffer.
73 *
74 * @param TotalBytesAvailable
75 * On return, the total number of bytes available to read from the pipe.
76 *
77 * @return
78 * True on success, false on failure; call GetLastError for error information.
79 *
80 * @see PeekNamedPipe
81 */
82 bool
83 CPipe::Peek(PVOID Buffer, DWORD BufferSize, PDWORD BytesRead, PDWORD TotalBytesAvailable)
84 {
85 if (!m_hReadPipe)
86 FATAL("Trying to peek from a closed read pipe");
87
88 return PeekNamedPipe(m_hReadPipe, Buffer, BufferSize, BytesRead, TotalBytesAvailable, NULL);
89 }
90
91 /**
92 * Reads data from the read pipe, advancing the read offset accordingly.
93 *
94 * This function must not be called after CloseReadPipe.
95 *
96 * @param Buffer
97 * Buffer to read pipe data into.
98 *
99 * @param NumberOfBytesToRead
100 * The number of bytes to read into Buffer.
101 *
102 * @param NumberOfBytesRead
103 * On return, the number of bytes actually read from the pipe into Buffer.
104 *
105 * @return
106 * True on success, false on failure; call GetLastError for error information.
107 *
108 * @see ReadFile
109 */
110 bool
111 CPipe::Read(PVOID Buffer, DWORD NumberOfBytesToRead, PDWORD NumberOfBytesRead)
112 {
113 if (!m_hReadPipe)
114 FATAL("Trying to read from a closed read pipe");
115
116 return ReadFile(m_hReadPipe, Buffer, NumberOfBytesToRead, NumberOfBytesRead, NULL);
117 }
118
119 /**
120 * Writes data to the write pipe.
121 *
122 * This function must not be called after CloseWritePipe.
123 *
124 * @param Buffer
125 * Buffer containing the data to write.
126 *
127 * @param NumberOfBytesToWrite
128 * The number of bytes to write to the pipe from Buffer.
129 *
130 * @param NumberOfBytesWritten
131 * On return, the number of bytes actually written to the pipe.
132 *
133 * @return
134 * True on success, false on failure; call GetLastError for error information.
135 *
136 * @see WriteFile
137 */
138 bool
139 CPipe::Write(LPCVOID Buffer, DWORD NumberOfBytesToWrite, PDWORD NumberOfBytesWritten)
140 {
141 if (!m_hWritePipe)
142 FATAL("Trying to write to a closed write pipe");
143
144 return WriteFile(m_hWritePipe, Buffer, NumberOfBytesToWrite, NumberOfBytesWritten, NULL);
145 }