From dc8703206e3f0f69605c56d0e1127f7e17f3476a Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Tue, 4 Jun 2019 14:34:27 +0200 Subject: Initial commit --- testing/yup-comm/yup-comm-client.c | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 testing/yup-comm/yup-comm-client.c (limited to 'testing/yup-comm/yup-comm-client.c') diff --git a/testing/yup-comm/yup-comm-client.c b/testing/yup-comm/yup-comm-client.c new file mode 100644 index 0000000..d6cc8e4 --- /dev/null +++ b/testing/yup-comm/yup-comm-client.c @@ -0,0 +1,80 @@ +/* Author: Jan Sucan */ + +#include +#include +#include + +#include "mailslot.h" + +#define MAILSLOT_SERVER_NAME "\\\\.\\Mailslot\\yup-comm-server" +#define MAILSLOT_CLIENT_NAME "\\\\.\\Mailslot\\yup-comm-client" +#define MAILSLOT_MAX_MSG_SIZE 400 + +HANDLE mailslot_server = INVALID_HANDLE_VALUE; +HANDLE mailslot_client = INVALID_HANDLE_VALUE; + +void +print_msg(FILE * stream, const char * format, ...) +{ + char buf[1024]; + + va_list args; + va_start(args, format); + vsnprintf(buf, 1024, format, args); + va_end(args); + + fprintf(stream, " yup-comm-client: %s%s", (stream == stderr) ? "ERROR: " : "" , buf); + fflush(stream); +} + +void +clean_exit(int code) +{ + if (mailslot_server != INVALID_HANDLE_VALUE) { + CloseHandle(mailslot_server); + } + + exit(code); +} + +void +usage(void) +{ + printf("Usage: yup-comm-client CMD\n"); + printf(" CMD command for yup-comm-server\n"); + clean_exit(1); +} + +int +main(int argc, char ** argv) +{ + if (argc != 2) { + print_msg(stderr, "Missing argument\n"); + usage(); + } if (strlen(argv[1]) > MAILSLOT_MAX_MSG_SIZE) { + print_msg(stderr, "The command is too long (> 400 characters)\n"); + clean_exit(1); + } + + if ((mailslot_client = mailslot_create(MAILSLOT_CLIENT_NAME)) == INVALID_HANDLE_VALUE) { + print_msg(stderr, "Cannot create mailslot for receiving reply from the server\n"); + clean_exit(1); + } + + if ((mailslot_server = mailslot_connect(MAILSLOT_SERVER_NAME)) == INVALID_HANDLE_VALUE) { + print_msg(stderr, "Cannot open server's mailslot\n"); + clean_exit(1); + } + + if (mailslot_write(mailslot_server, argv[1], strlen(argv[1]) + 1U) != 0) { + print_msg(stderr, "Cannot send command to the server\n"); + clean_exit(1); + } + + print_msg(stdout, "Waiting for reply from the server ...\n"); + + char c; + mailslot_read(mailslot_client, &c, sizeof(c)); + + clean_exit(0); +} -- cgit v1.2.3