6af9a2057c8b7b0f3d0bfb5ce8f0deda04acfba0
[reactos.git] / rostests / tests / lpctst / lpcsrv.c
1 /* $Id$
2 *
3 * DESCRIPTION: Simple LPC Server
4 * PROGRAMMER: David Welch
5 */
6 #include <ddk/ntddk.h>
7 #include <rosrtl/string.h>
8 #include <windows.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "lpctest.h"
15
16 static const char * MyName = "LPC-SRV";
17
18 HANDLE OutputHandle;
19 HANDLE InputHandle;
20
21 void debug_printf(char* fmt, ...)
22 {
23 va_list args;
24 char buffer[255];
25
26 va_start(args,fmt);
27 vsprintf(buffer,fmt,args);
28 WriteConsoleA(OutputHandle, buffer, strlen(buffer), NULL, NULL);
29 va_end(args);
30 }
31
32
33 int main(int argc, char* argv[])
34 {
35 UNICODE_STRING PortName = ROS_STRING_INITIALIZER(TEST_PORT_NAME_U);
36 OBJECT_ATTRIBUTES ObjectAttributes;
37 NTSTATUS Status;
38 HANDLE NamedPortHandle;
39 HANDLE PortHandle;
40 LPC_MAX_MESSAGE ConnectMsg;
41
42 printf("%s: Lpc test server\n", MyName);
43
44 InitializeObjectAttributes(&ObjectAttributes,
45 &PortName,
46 0,
47 NULL,
48 NULL);
49
50 printf("%s: Creating port \"%s\"...\n", MyName, TEST_PORT_NAME);
51 Status = NtCreatePort(&NamedPortHandle,
52 &ObjectAttributes,
53 0,
54 0,
55 0);
56 if (!NT_SUCCESS(Status))
57 {
58 printf("%s: NtCreatePort() failed with status = 0x%08lX.\n", MyName, Status);
59 return EXIT_FAILURE;
60 }
61 printf("%s: Port \"%s\" created (0x%x).\n\n", MyName, TEST_PORT_NAME, NamedPortHandle);
62
63 for (;;)
64 {
65 printf("%s: Listening for connections requests on port 0x%x...\n", MyName, NamedPortHandle);
66 Status = NtListenPort(NamedPortHandle,
67 &ConnectMsg.Header);
68 if (!NT_SUCCESS(Status))
69 {
70 printf("%s: NtListenPort() failed with status = 0x%08lX.\n", MyName, Status);
71 return EXIT_FAILURE;
72 }
73
74 printf("%s: Received connection request 0x%08x on port 0x%x.\n", MyName,
75 ConnectMsg.Header.MessageId, NamedPortHandle);
76 printf("%s: Request from: PID=%x, TID=%x.\n", MyName,
77 ConnectMsg.Header.ClientId.UniqueProcess, ConnectMsg.Header.ClientId.UniqueThread);
78
79 printf("%s: Accepting connection request 0x%08x...\n", MyName,
80 ConnectMsg.Header.MessageId);
81 Status = NtAcceptConnectPort(&PortHandle,
82 NamedPortHandle,
83 & ConnectMsg.Header,
84 TRUE,
85 0,
86 NULL);
87 if (!NT_SUCCESS(Status))
88 {
89 printf("%s: NtAcceptConnectPort() failed with status = 0x%08lX.\n", MyName, Status);
90 return EXIT_FAILURE;
91 }
92 printf("%s: Connection request 0x%08x accepted as port 0x%x.\n", MyName,
93 ConnectMsg.Header.MessageId, PortHandle);
94
95 printf("%s: Completing connection for port 0x%x (0x%08x).\n", MyName,
96 PortHandle, ConnectMsg.Header.MessageId);
97 Status = NtCompleteConnectPort(PortHandle);
98 if (!NT_SUCCESS(Status))
99 {
100 printf("%s: NtCompleteConnectPort() failed with status = 0x%08lX.\n", MyName, Status);
101 return EXIT_FAILURE;
102 }
103
104 printf("%s: Entering server loop for port 0x%x...\n", MyName, PortHandle);
105 for(;;)
106 {
107 LPC_MAX_MESSAGE Request;
108
109 Status = NtReplyWaitReceivePort(PortHandle,
110 0,
111 NULL,
112 &Request.Header);
113 if (!NT_SUCCESS(Status))
114 {
115 printf("%s: NtReplyWaitReceivePort() failed with status = 0x%08lX.\n", MyName, Status);
116 return EXIT_FAILURE;
117 }
118
119 if (LPC_DATAGRAM == PORT_MESSAGE_TYPE(Request))
120 {
121 printf("%s: Datagram message contents are <%s>.\n",
122 MyName,
123 Request.Data);
124 }
125 else
126 {
127 printf("%s: Message with type %d received on port 0x%x.\n", MyName,
128 PORT_MESSAGE_TYPE(Request), PortHandle);
129 NtClose(PortHandle);
130 printf("%s: Connected port 0x%x closed.\n\n", MyName, PortHandle);
131 break;
132 }
133 }
134 }
135 return EXIT_SUCCESS;
136 }
137
138
139 /* EOF */