npecho_message_server.c...
authorStefan Metzmacher <metze@samba.org>
Mon, 20 Apr 2009 12:32:24 +0000 (14:32 +0200)
committerStefan Metzmacher <metze@samba.org>
Mon, 22 Apr 2013 09:15:02 +0000 (11:15 +0200)
testprogs/win32/npecho/npecho_message_server.c [new file with mode: 0644]

diff --git a/testprogs/win32/npecho/npecho_message_server.c b/testprogs/win32/npecho/npecho_message_server.c
new file mode 100644 (file)
index 0000000..7ab5dd0
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * Simple Named Pipe Client
+ * (C) 2005 Jelmer Vernooij <jelmer@samba.org>
+ * (C) 2009 Stefan Metzmacher <metze@samba.org>
+ * Published to the public domain
+ */
+
+#include <stdio.h>
+#include <windows.h>
+
+#define NPECHO_CMD_SLEEP 0
+#define NPECHO_CMD_READ 1
+#define NPECHO_CMD_WRITE 2
+#define NPECHO_CMD_NEXT_CTRL 3
+
+int main(int argc, char *argv[])
+{
+       HANDLE h;
+       DWORD numio = 0;
+       DWORD i;
+       DWORD status = 0;
+       BYTE buffer[0xFFFF];
+       struct {
+               DWORD cmd;
+               DWORD val;
+       } cmds[256];
+       DWORD num_cmds = 0;
+
+       memset(buffer, 0, sizeof(buffer));
+
+       if (argc == 9999) {
+               goto usage;
+       }
+
+       printf("create message mode named pipe\n");
+       h = CreateNamedPipe("\\\\.\\pipe\\NPECHO_MESSAGE",
+                           PIPE_ACCESS_DUPLEX,
+                           PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
+                           PIPE_UNLIMITED_INSTANCES,
+                           4096,
+                           4096,
+                           0,
+                           NULL);
+       if (h == INVALID_HANDLE_VALUE) {
+               printf("Error opening: %d\n", GetLastError());
+               return -1;
+       }
+
+wait_for_client:
+       printf("Wait for client\n");
+       ConnectNamedPipe(h, NULL);
+       printf("Connected\n");
+
+next_ctrl:
+       if (!ReadFile(h, cmds, sizeof(cmds), &numio, NULL)) {
+               printf("Error ReadFile: %d\n", GetLastError());
+               goto disconnect;
+       }
+
+       if ((numio % sizeof(cmds[0])) != 0) {
+               status = ERROR_INVALID_PARAMETER;
+               printf("Invalid numread: %d\n", numio);
+               goto send_status;
+       }
+
+       num_cmds = numio / sizeof(cmds[0]);
+
+       /* check the instructions */
+       for (i=0; i < num_cmds; i++) {
+               switch (cmds[i].cmd) {
+               case NPECHO_CMD_SLEEP:
+                       break;
+               case NPECHO_CMD_READ:
+               case NPECHO_CMD_WRITE:
+                       if (cmds[i].val > 0xFFFF) {
+                               status = ERROR_INVALID_PARAMETER;
+                               printf("cmd[%d] Invalid value: %d\n",
+                                      i, cmds[i].val);
+                               goto send_status;
+                       }
+                       break;
+               case NPECHO_CMD_NEXT_CTRL:
+                       break;
+               default:
+                       status = STATUS_ILLEGAL_INSTRUCTION;
+                       printf("cmd[%d] Invalid cmd: %d\n",
+                              i, cmds[i].cmd);
+                       goto send_status;
+               }
+       }
+
+       status = 0;
+       if (!WriteFile(h, &status, sizeof(status), &numio, NULL)) {
+               printf("Error writing: %d\n", GetLastError());
+               goto disconnect;
+       }
+
+       /* now do what the client told us */
+       for (i=0; i < num_cmds; i++) {
+               switch (cmds[i].cmd) {
+               case NPECHO_CMD_SLEEP:
+                       Sleep(cmds[i].val);
+                       break;
+               case NPECHO_CMD_READ:
+                       if (!ReadFile(h, buffer, cmds[i].val, &numio, NULL)) {
+                               printf("Error reading: %d\n", GetLastError());
+                               goto disconnect;
+                       }
+                       break;
+               case NPECHO_CMD_WRITE:
+                       buffer[0] = i;
+                       if (!WriteFile(h, buffer, cmds[i].val, &numio, NULL)) {
+                               printf("Error writing: %d\n", GetLastError());
+                               goto disconnect;
+                       }
+                       buffer[0] = 0;
+                       break;
+               case NPECHO_CMD_NEXT_CTRL:
+                       goto next_ctrl;
+               }
+       }
+disconnect:
+       FlushFileBuffers(h);
+       DisconnectNamedPipe(h);
+
+       goto wait_for_client;
+
+       CloseHandle(h);
+
+       return 0;
+send_status:
+       if (!WriteFile(h, &status, sizeof(status), &numio, NULL)) {
+               printf("Error writing: %d\n", GetLastError());
+               return -1;
+       }
+       return -1;
+usage:
+       printf("Usage: %s pipename [mode]\n", argv[0]);
+       printf("  Where pipename is something like \\\\servername\\PIPE\\NPECHO\n");
+       printf("  Where mode is 'byte' or 'message'\n");
+       return -1;
+}