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;
68 class NamedPipeTests :
public UnitTest 73 :
UnitTest (
"NamedPipe", UnitTestCategories::networking)
76 void runTest()
override 78 const String pipeName (
"TestPipe");
80 beginTest (
"Pre test cleanup");
86 beginTest (
"Create pipe");
98 expect (! otherPipe.createNewPipe (pipeName,
true));
99 expect (! otherPipe.isOpen());
102 beginTest (
"Existing pipe");
112 expect (otherPipe.openExisting (pipeName));
113 expect (otherPipe.isOpen());
116 int sendData = 4684682;
118 beginTest (
"Receive message created pipe");
124 SenderThread sender (pipeName,
false, senderFinished, sendData);
126 sender.startThread();
129 auto bytesRead = pipe.
read (&recvData,
sizeof (recvData), 2000);
131 expect (senderFinished.wait (4000));
133 expectEquals (bytesRead, (
int)
sizeof (recvData));
134 expectEquals (sender.result, (
int) sizeof (sendData));
135 expectEquals (recvData, sendData);
138 beginTest (
"Receive message existing pipe");
141 SenderThread sender (pipeName,
true, senderFinished, sendData);
146 sender.startThread();
149 auto bytesRead = pipe.
read (&recvData,
sizeof (recvData), 2000);
151 expect (senderFinished.
wait (4000));
153 expectEquals (bytesRead, (
int)
sizeof (recvData));
154 expectEquals (sender.result, (
int) sizeof (sendData));
155 expectEquals (recvData, sendData);
158 beginTest (
"Send message created pipe");
164 ReceiverThread receiver (pipeName,
false, receiverFinished);
166 receiver.startThread();
168 auto bytesWritten = pipe.
write (&sendData,
sizeof (sendData), 2000);
170 expect (receiverFinished.wait (4000));
172 expectEquals (bytesWritten, (
int)
sizeof (sendData));
173 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
174 expectEquals (receiver.recvData, sendData);
177 beginTest (
"Send message existing pipe");
180 ReceiverThread receiver (pipeName,
true, receiverFinished);
185 receiver.startThread();
187 auto bytesWritten = pipe.
write (&sendData,
sizeof (sendData), 2000);
189 expect (receiverFinished.
wait (4000));
191 expectEquals (bytesWritten, (
int)
sizeof (sendData));
192 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
193 expectEquals (receiver.recvData, sendData);
199 struct NamedPipeThread :
public Thread 201 NamedPipeThread (
const String& tName,
const String& pName,
203 :
Thread (tName), pipeName (pName), workCompleted (completed)
205 if (shouldCreatePipe)
206 pipe.createNewPipe (pipeName);
208 pipe.openExisting (pipeName);
224 struct SenderThread :
public NamedPipeThread
226 SenderThread (
const String& pName,
bool shouldCreatePipe,
228 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed),
234 result = pipe.write (&sendData,
sizeof (sendData), 2000);
235 workCompleted.signal();
242 struct ReceiverThread :
public NamedPipeThread
244 ReceiverThread (
const String& pName,
bool shouldCreatePipe,
246 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed)
251 result = pipe.read (&recvData,
sizeof (recvData), 2000);
252 workCompleted.signal();
259 static NamedPipeTests namedPipeTests;
int read(void *destBuffer, int maxBytesToRead, int timeOutMilliseconds)
int write(const void *sourceBuffer, int numBytesToWrite, int timeOutMilliseconds)
bool createNewPipe(const String &pipeName, bool mustNotExist=false)
bool wait(int timeOutMilliseconds=-1) const
bool openExisting(const String &pipeName)