38 currentPipeName = pipeName;
39 return openInternal (pipeName,
false,
false);
44 return pimpl !=
nullptr;
52 currentPipeName = pipeName;
53 return openInternal (pipeName,
true, mustNotExist);
58 return currentPipeName;
67 class NamedPipeTests :
public UnitTest 72 :
UnitTest (
"NamedPipe",
"Networking")
75 void runTest()
override 77 const String pipeName (
"TestPipe");
79 beginTest (
"Pre test cleanup");
85 beginTest (
"Create pipe");
97 expect (! otherPipe.createNewPipe (pipeName,
true));
98 expect (! otherPipe.isOpen());
101 beginTest (
"Existing pipe");
111 expect (otherPipe.openExisting (pipeName));
112 expect (otherPipe.isOpen());
115 int sendData = 4684682;
117 beginTest (
"Receive message created pipe");
123 SenderThread sender (pipeName,
false, senderFinished, sendData);
125 sender.startThread();
128 auto bytesRead = pipe.
read (&recvData,
sizeof (recvData), 2000);
130 expect (senderFinished.wait (4000));
132 expectEquals (bytesRead, (
int)
sizeof (recvData));
133 expectEquals (sender.result, (
int) sizeof (sendData));
134 expectEquals (recvData, sendData);
137 beginTest (
"Receive message existing pipe");
140 SenderThread sender (pipeName,
true, senderFinished, sendData);
145 sender.startThread();
148 auto bytesRead = pipe.
read (&recvData,
sizeof (recvData), 2000);
150 expect (senderFinished.
wait (4000));
152 expectEquals (bytesRead, (
int)
sizeof (recvData));
153 expectEquals (sender.result, (
int) sizeof (sendData));
154 expectEquals (recvData, sendData);
157 beginTest (
"Send message created pipe");
163 ReceiverThread receiver (pipeName,
false, receiverFinished);
165 receiver.startThread();
167 auto bytesWritten = pipe.
write (&sendData,
sizeof (sendData), 2000);
169 expect (receiverFinished.wait (4000));
171 expectEquals (bytesWritten, (
int)
sizeof (sendData));
172 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
173 expectEquals (receiver.recvData, sendData);
176 beginTest (
"Send message existing pipe");
179 ReceiverThread receiver (pipeName,
true, receiverFinished);
184 receiver.startThread();
186 auto bytesWritten = pipe.
write (&sendData,
sizeof (sendData), 2000);
188 expect (receiverFinished.
wait (4000));
190 expectEquals (bytesWritten, (
int)
sizeof (sendData));
191 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
192 expectEquals (receiver.recvData, sendData);
198 struct NamedPipeThread :
public Thread 200 NamedPipeThread (
const String& threadName,
const String& pName,
202 :
Thread (threadName), pipeName (pName), workCompleted (completed)
204 if (shouldCreatePipe)
205 pipe.createNewPipe (pipeName);
207 pipe.openExisting (pipeName);
218 struct SenderThread :
public NamedPipeThread
220 SenderThread (
const String& pName,
bool shouldCreatePipe,
222 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed),
228 result = pipe.write (&sendData,
sizeof (sendData), 2000);
229 workCompleted.signal();
236 struct ReceiverThread :
public NamedPipeThread
238 ReceiverThread (
const String& pName,
bool shouldCreatePipe,
240 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed)
245 result = pipe.read (&recvData,
sizeof (recvData), 2000);
246 workCompleted.signal();
253 static NamedPipeTests namedPipeTests;
void close()
Closes the pipe, if it's open.
Allows threads to wait for events triggered by other threads.
int read(void *destBuffer, int maxBytesToRead, int timeOutMilliseconds)
Reads data from the pipe.
NamedPipe()
Creates a NamedPipe.
bool isOpen() const
True if the pipe is currently open.
This is a base class for classes that perform a unit test.
int write(const void *sourceBuffer, int numBytesToWrite, int timeOutMilliseconds)
Writes some data to the pipe.
A cross-process pipe that can have data written to and read from it.
bool createNewPipe(const String &pipeName, bool mustNotExist=false)
Tries to create a new pipe.
Automatically locks and unlocks a ReadWriteLock object.
String getName() const
Returns the last name that was used to try to open this pipe.
bool wait(int timeOutMilliseconds=-1) const noexcept
Suspends the calling thread until the event has been signalled.
bool openExisting(const String &pipeName)
Tries to open a pipe that already exists.