From d421741a83a1d7114a85d286f4bb92c52b20f31b Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 28 Mar 2020 23:15:15 +0100 Subject: [PATCH] Wait server from a separate thread Create a thread just to wait for the server process exit. This paves the way to simply wake up a blocking accept() in a portable way. --- app/src/server.c | 24 +++++++++++++++++++++--- app/src/server.h | 3 +++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/src/server.c b/app/src/server.c index 2923c6c7..77978d19 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -332,6 +333,14 @@ server_init(struct server *server) { *server = (struct server) SERVER_INITIALIZER; } +static int +run_wait_server(void *data) { + struct server *server = data; + cmd_simple_wait(server->process, NULL); // ignore exit code + LOGD("Server terminated"); + return 0; +} + bool server_start(struct server *server, const char *serial, const struct server_params *params) { @@ -358,6 +367,16 @@ server_start(struct server *server, const char *serial, goto error2; } + server->wait_server_thread = + SDL_CreateThread(run_wait_server, "wait-server", server); + if (!server->wait_server_thread) { + if (!cmd_terminate(server->process)) { + LOGW("Could not terminate server"); + } + cmd_simple_wait(server->process, NULL); // ignore exit code + goto error2; + } + server->tunnel_enabled = true; return true; @@ -430,13 +449,12 @@ server_stop(struct server *server) { LOGW("Could not terminate server"); } - cmd_simple_wait(server->process, NULL); // ignore exit code - LOGD("Server terminated"); - if (server->tunnel_enabled) { // ignore failure disable_tunnel(server); } + + SDL_WaitThread(server->wait_server_thread, NULL); } void diff --git a/app/src/server.h b/app/src/server.h index cc712b63..050f7f01 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -3,6 +3,7 @@ #include #include +#include #include "config.h" #include "command.h" @@ -12,6 +13,7 @@ struct server { char *serial; process_t process; + SDL_Thread *wait_server_thread; socket_t server_socket; // only used if !tunnel_forward socket_t video_socket; socket_t control_socket; @@ -24,6 +26,7 @@ struct server { #define SERVER_INITIALIZER { \ .serial = NULL, \ .process = PROCESS_NONE, \ + .wait_server_thread = NULL, \ .server_socket = INVALID_SOCKET, \ .video_socket = INVALID_SOCKET, \ .control_socket = INVALID_SOCKET, \