FreeRDP
Loading...
Searching...
No Matches
TestMessagePipe.c
1
2#include <winpr/crt.h>
3#include <winpr/thread.h>
4#include <winpr/collections.h>
5
6static DWORD WINAPI message_echo_pipe_client_thread(LPVOID arg)
7{
8 int index = 0;
9 wMessagePipe* pipe = (wMessagePipe*)arg;
10
11 while (index < 100)
12 {
13 wMessage message = { 0 };
14 int count = -1;
15
16 if (!MessageQueue_Post(pipe->In, NULL, 0, (void*)(size_t)index, NULL))
17 break;
18
19 if (!MessageQueue_Wait(pipe->Out))
20 break;
21
22 if (!MessageQueue_Peek(pipe->Out, &message, TRUE))
23 break;
24
25 if (message.id == WMQ_QUIT)
26 break;
27
28 count = (int)(size_t)message.wParam;
29
30 if (count != index)
31 printf("Echo count mismatch: Actual: %d, Expected: %d\n", count, index);
32
33 index++;
34 }
35
36 MessageQueue_PostQuit(pipe->In, 0);
37
38 return 0;
39}
40
41static DWORD WINAPI message_echo_pipe_server_thread(LPVOID arg)
42{
43 wMessage message = { 0 };
44 wMessagePipe* pipe = (wMessagePipe*)arg;
45
46 while (MessageQueue_Wait(pipe->In))
47 {
48 if (MessageQueue_Peek(pipe->In, &message, TRUE))
49 {
50 if (message.id == WMQ_QUIT)
51 break;
52
53 if (!MessageQueue_Dispatch(pipe->Out, &message))
54 break;
55 }
56 }
57
58 return 0;
59}
60
61int TestMessagePipe(int argc, char* argv[])
62{
63 HANDLE ClientThread = NULL;
64 HANDLE ServerThread = NULL;
65 wMessagePipe* EchoPipe = NULL;
66 int ret = 1;
67
68 WINPR_UNUSED(argc);
69 WINPR_UNUSED(argv);
70
71 if (!(EchoPipe = MessagePipe_New()))
72 {
73 printf("failed to create message pipe\n");
74 goto out;
75 }
76
77 if (!(ClientThread =
78 CreateThread(NULL, 0, message_echo_pipe_client_thread, (void*)EchoPipe, 0, NULL)))
79 {
80 printf("failed to create client thread\n");
81 goto out;
82 }
83
84 if (!(ServerThread =
85 CreateThread(NULL, 0, message_echo_pipe_server_thread, (void*)EchoPipe, 0, NULL)))
86 {
87 printf("failed to create server thread\n");
88 goto out;
89 }
90
91 (void)WaitForSingleObject(ClientThread, INFINITE);
92 (void)WaitForSingleObject(ServerThread, INFINITE);
93
94 ret = 0;
95
96out:
97 if (EchoPipe)
98 MessagePipe_Free(EchoPipe);
99 if (ClientThread)
100 (void)CloseHandle(ClientThread);
101 if (ServerThread)
102 (void)CloseHandle(ServerThread);
103
104 return ret;
105}