From f5a0b8a5e24cede05e15ab696f0e15257a503525 Mon Sep 17 00:00:00 2001 From: Angelo Mantellini Date: Thu, 31 Jan 2019 18:20:48 +0100 Subject: [HICN24] Windows compatibility for hicn-light Change-Id: I8e19e52c9b4ec0fcbd7344c28765f5da1937569c Signed-off-by: Angelo Mantellini --- .../controller/hicnLightControl_main.c | 57 +++++++++++++++------- 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'hicn-light/src/command_line/controller') diff --git a/hicn-light/src/command_line/controller/hicnLightControl_main.c b/hicn-light/src/command_line/controller/hicnLightControl_main.c index 4641bddf5..03f74692f 100644 --- a/hicn-light/src/command_line/controller/hicnLightControl_main.c +++ b/hicn-light/src/command_line/controller/hicnLightControl_main.c @@ -15,13 +15,18 @@ #include +#ifndef _WIN32 +#include #include +#include +#include +#include +#include +#endif #include #include #include #include -#include -#include #include #include @@ -71,22 +76,17 @@ static int payloadLengthController[LAST_COMMAND_VALUE] = { sizeof(mapme_timing_command), sizeof(mapme_timing_command)}; -#include -#include -#include -#include - typedef struct controller_main_state { ControlState *controlState; } ControlMainState; -static void _displayForwarderLogo(void){ - const char cli_banner [] = - "\033[0;31m ____ ___ _ \033[0m __ _ __ _ __ __\n" - "\033[0;31m / __// _ \\ (_)___ \033[0m / / (_)____ ___ ____/ /(_)___ _ / / / /_\n" - "\033[0;31m / _/ / // /_ / // _ \\ \033[0m / _ \\ / // __// _ \\___/ // // _ `// _ \\/ __/\n" - "\033[0;31m/_/ /____/(_)/_/ \\___/ \033[0m/_//_//_/ \\__//_//_/ /_//_/ \\_, //_//_/\\__/\n" - " /___/ \n"; +static void _displayForwarderLogo(void) { + const char cli_banner[] = + "\033[0;31m ____ ___ _ \033[0m __ _ __ _ __ __\n" + "\033[0;31m / __// _ \\ (_)___ \033[0m / / (_)____ ___ ____/ /(_)___ _ / / / /_\n" + "\033[0;31m / _/ / // /_ / // _ \\ \033[0m / _ \\ / // __// _ \\___/ // // _ `// _ \\/ __/\n" + "\033[0;31m/_/ /____/(_)/_/ \\___/ \033[0m/_//_//_/ \\__//_//_/ /_//_/ \\_, //_//_/\\__/\n" + " /___/ \n"; printf("%s", cli_banner); printf("\n"); } @@ -165,15 +165,25 @@ struct iovec *_writeAndReadMessage(ControlState *state, struct iovec *msg) { 0) { // command with payload // write header + payload (compatibility issue: two write needed instead of // the writev) - if (write(sockfd, msg[0].iov_base, msg[0].iov_len) < 0 || - write(sockfd, msg[1].iov_base, msg[1].iov_len) < 0) { +#ifndef _WIN32 + if (write(sockfd, msg[0].iov_base, (unsigned int)msg[0].iov_len) < 0 || + write(sockfd, msg[1].iov_base, (unsigned int)msg[1].iov_len) < 0) { +#else + if (send(sockfd, msg[0].iov_base, msg[0].iov_len, 0) == SOCKET_ERROR || + send(sockfd, msg[1].iov_base, msg[1].iov_len, 0) == SOCKET_ERROR) { +#endif printf("\nError while sending the Message: cannot write on socket \n"); exit(EXIT_FAILURE); } parcMemory_Deallocate(&msg[1].iov_base); } else { // command without payload, e.g. 'list' - // write header only + // write header only +#ifndef _WIN32 if (write(sockfd, msg[0].iov_base, msg[0].iov_len) < 0) { +#else + int result = send(sockfd, msg[0].iov_base, msg[0].iov_len, 0); + if (result == SOCKET_ERROR) { +#endif printf("\nError while sending the Message: cannot write on socket \n"); exit(EXIT_FAILURE); } @@ -193,8 +203,12 @@ struct iovec *_writeAndReadMessage(ControlState *state, struct iovec *msg) { if (headerResponse->messageType < RESPONSE_LIGHT || headerResponse->messageType >= LAST_MSG_TYPE_VALUE) { char *checkFinMsg = parcMemory_Reallocate(headerResponse, 32); +#ifndef _WIN32 if (recv(sockfd, checkFinMsg, sizeof(checkFinMsg), MSG_PEEK | MSG_DONTWAIT) == 0) { +#else + if (recv(sockfd, checkFinMsg, sizeof(checkFinMsg), MSG_PEEK) == 0) { +#endif // if recv returns zero, that means the connection has been closed: close(sockfd); printf("\nConnection terminated by the Daemon. Exiting... \n"); @@ -236,6 +250,11 @@ struct iovec *_writeAndReadMessage(ControlState *state, struct iovec *msg) { int main(int argc, char *argv[]) { _displayForwarderLogo(); +#ifdef _WIN32 + WSADATA wsaData; + WSAStartup(MAKEWORD(2, 2), &wsaData); +#endif + if (argc == 2 && strcmp("-h", argv[1]) == 0) { _displayUsage(argv[0]); exit(EXIT_SUCCESS); @@ -279,6 +298,8 @@ int main(int argc, char *argv[]) { parcList_Release(&commands); controlState_Destroy(&mainState.controlState); - +#ifdef _WIN32 + WSACleanup(); +#endif return EXIT_SUCCESS; } -- cgit 1.2.3-korg