diff --git a/app/src/server.c b/app/src/server.c index 7877f052..f0ad75fe 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -461,25 +461,41 @@ run_wait_server(void *data) { return 0; } -bool -server_start(struct server *server) { +static int +run_server(void *data) { + struct server *server = data; + const struct server_params *params = &server->params; + const struct server_callbacks *cbs = &server->cbs; + void *userdata = server->userdata; if (!push_server(params->serial)) { - return false; + cbs->on_connection_failed(server); + goto end; } if (!enable_tunnel_any_port(server, params->port_range, params->force_adb_forward)) { - return false; + cbs->on_connection_failed(server); + goto end; } // server will connect to our server socket server->process = execute_server(server, params); if (server->process == PROCESS_NONE) { - goto error; + cbs->on_connection_failed(server); + goto end; } + process_wait(server->process, false); // ignore exit code + +end: + return 0; +} + +bool +server_start(struct server *server) { + // If the server process dies before connecting to the server socket, then // the client will be stuck forever on accept(). To avoid the problem, we // must be able to wake up the accept() call when the server dies. To keep diff --git a/app/src/server.h b/app/src/server.h index dddbee0f..c40cfc50 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -51,6 +51,16 @@ struct server { // The internal allocated strings are copies owned by the server struct server_params params; + + const struct server_callbacks *cbs; + void *userdata; +}; + +struct server_callbacks { + void (*on_connection_failed)(struct server *server); + void (*on_connected)(struct server *server, const char *name, + struct size size, void *userdata); + void (*on_disconnected)(struct server *server, void *userdata); }; // init server fields @@ -59,7 +69,8 @@ server_init(struct server *server, const struct server_params *params); // push, enable tunnel et start the server bool -server_start(struct server *server); +server_start(struct server *server, const struct server_callbacks *cbs, + void *userdata); // block until the communication with the server is established bool