2013-02-20 14:48:32 -05:00
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
|
2018-03-07 12:03:10 +01:00
|
|
|
static DWORD WINAPI message_echo_pipe_client_thread(LPVOID arg)
|
2013-02-20 14:48:32 -05:00
|
|
|
{
|
2017-03-28 16:18:00 +02:00
|
|
|
int index = 0;
|
2019-11-06 15:24:51 +01:00
|
|
|
wMessagePipe* pipe = (wMessagePipe*)arg;
|
2013-02-20 14:48:32 -05:00
|
|
|
|
|
|
|
|
while (index < 100)
|
|
|
|
|
{
|
2026-02-24 20:18:25 +01:00
|
|
|
wMessage message = WINPR_C_ARRAY_INIT;
|
2023-06-05 12:16:57 +02:00
|
|
|
int count = -1;
|
2017-03-28 16:18:00 +02:00
|
|
|
|
2026-02-26 14:32:50 +01:00
|
|
|
if (!MessageQueue_Post(pipe->In, nullptr, 0, (void*)(size_t)index, nullptr))
|
2015-05-23 22:47:18 +02:00
|
|
|
break;
|
2013-02-20 14:48:32 -05:00
|
|
|
|
|
|
|
|
if (!MessageQueue_Wait(pipe->Out))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (!MessageQueue_Peek(pipe->Out, &message, TRUE))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (message.id == WMQ_QUIT)
|
|
|
|
|
break;
|
|
|
|
|
|
2019-11-06 15:24:51 +01:00
|
|
|
count = (int)(size_t)message.wParam;
|
2013-02-20 14:48:32 -05:00
|
|
|
|
|
|
|
|
if (count != index)
|
|
|
|
|
printf("Echo count mismatch: Actual: %d, Expected: %d\n", count, index);
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessageQueue_PostQuit(pipe->In, 0);
|
|
|
|
|
|
2018-03-07 12:03:10 +01:00
|
|
|
return 0;
|
2013-02-20 14:48:32 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-07 12:03:10 +01:00
|
|
|
static DWORD WINAPI message_echo_pipe_server_thread(LPVOID arg)
|
2013-02-20 14:48:32 -05:00
|
|
|
{
|
2026-02-24 20:18:25 +01:00
|
|
|
wMessage message = WINPR_C_ARRAY_INIT;
|
2023-06-05 12:16:57 +02:00
|
|
|
wMessagePipe* pipe = (wMessagePipe*)arg;
|
2013-02-20 14:48:32 -05:00
|
|
|
|
|
|
|
|
while (MessageQueue_Wait(pipe->In))
|
|
|
|
|
{
|
|
|
|
|
if (MessageQueue_Peek(pipe->In, &message, TRUE))
|
|
|
|
|
{
|
|
|
|
|
if (message.id == WMQ_QUIT)
|
|
|
|
|
break;
|
|
|
|
|
|
2015-05-23 22:47:18 +02:00
|
|
|
if (!MessageQueue_Dispatch(pipe->Out, &message))
|
|
|
|
|
break;
|
2013-02-20 14:48:32 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 12:03:10 +01:00
|
|
|
return 0;
|
2013-02-20 14:48:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TestMessagePipe(int argc, char* argv[])
|
|
|
|
|
{
|
2026-02-26 14:32:50 +01:00
|
|
|
HANDLE ClientThread = nullptr;
|
|
|
|
|
HANDLE ServerThread = nullptr;
|
|
|
|
|
wMessagePipe* EchoPipe = nullptr;
|
2015-05-05 13:55:48 +02:00
|
|
|
int ret = 1;
|
2013-02-20 14:48:32 -05:00
|
|
|
|
2021-07-29 10:18:52 +02:00
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
|
WINPR_UNUSED(argv);
|
|
|
|
|
|
2015-05-05 13:55:48 +02:00
|
|
|
if (!(EchoPipe = MessagePipe_New()))
|
|
|
|
|
{
|
|
|
|
|
printf("failed to create message pipe\n");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2013-02-20 14:48:32 -05:00
|
|
|
|
2026-02-26 14:32:50 +01:00
|
|
|
if (!(ClientThread = CreateThread(nullptr, 0, message_echo_pipe_client_thread, (void*)EchoPipe,
|
|
|
|
|
0, nullptr)))
|
2015-05-05 13:55:48 +02:00
|
|
|
{
|
|
|
|
|
printf("failed to create client thread\n");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 14:32:50 +01:00
|
|
|
if (!(ServerThread = CreateThread(nullptr, 0, message_echo_pipe_server_thread, (void*)EchoPipe,
|
|
|
|
|
0, nullptr)))
|
2015-05-05 13:55:48 +02:00
|
|
|
{
|
|
|
|
|
printf("failed to create server thread\n");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2013-02-20 14:48:32 -05:00
|
|
|
|
2024-09-16 05:06:07 +02:00
|
|
|
(void)WaitForSingleObject(ClientThread, INFINITE);
|
|
|
|
|
(void)WaitForSingleObject(ServerThread, INFINITE);
|
2013-02-20 14:48:32 -05:00
|
|
|
|
2015-05-05 13:55:48 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if (EchoPipe)
|
|
|
|
|
MessagePipe_Free(EchoPipe);
|
|
|
|
|
if (ClientThread)
|
2024-09-16 04:58:36 +02:00
|
|
|
(void)CloseHandle(ClientThread);
|
2015-05-05 13:55:48 +02:00
|
|
|
if (ServerThread)
|
2024-09-16 04:58:36 +02:00
|
|
|
(void)CloseHandle(ServerThread);
|
2015-05-05 13:55:48 +02:00
|
|
|
|
|
|
|
|
return ret;
|
2013-02-20 14:48:32 -05:00
|
|
|
}
|