From 2b3ed5bcdba63d30da427f324a7ab77c9a4b7937 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 12 Mar 2018 10:19:12 +0100 Subject: [PATCH] Store serial in server instance The serial is needed for many server actions, but this is an implementation detail, so the caller should not have to provide it on every call. Instead, store the serial in the server instance on server_start(). This paves the way to implement the "adb forward" fallback properly. --- app/src/scrcpy.c | 12 ++++++------ app/src/server.c | 17 +++++++++++------ app/src/server.h | 6 ++++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 8c549d67..66badb06 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -103,9 +103,9 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b // managed by the event loop. This blocking call blocks the event loop, so // timeout the connection not to block indefinitely in case of SIGTERM. #define SERVER_CONNECT_TIMEOUT_MS 2000 - socket_t device_socket = server_connect_to(&server, serial, SERVER_CONNECT_TIMEOUT_MS); + socket_t device_socket = server_connect_to(&server, SERVER_CONNECT_TIMEOUT_MS); if (device_socket == INVALID_SOCKET) { - server_stop(&server, serial); + server_stop(&server); ret = SDL_FALSE; goto finally_destroy_server; } @@ -117,13 +117,13 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b // therefore, we transmit the screen size before the video stream, to be able // to init the window immediately if (!device_read_info(device_socket, device_name, &frame_size)) { - server_stop(&server, serial); + server_stop(&server); ret = SDL_FALSE; goto finally_destroy_server; } if (!frames_init(&frames)) { - server_stop(&server, serial); + server_stop(&server); ret = SDL_FALSE; goto finally_destroy_server; } @@ -134,7 +134,7 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b // start the decoder if (!decoder_start(&decoder)) { ret = SDL_FALSE; - server_stop(&server, serial); + server_stop(&server); goto finally_destroy_frames; } @@ -165,7 +165,7 @@ finally_destroy_controller: finally_stop_decoder: decoder_stop(&decoder); // stop the server before decoder_join() to wake up the decoder - server_stop(&server, serial); + server_stop(&server); decoder_join(&decoder); finally_destroy_frames: frames_destroy(&frames); diff --git a/app/src/server.c b/app/src/server.c index d5d8b673..1e1c95bd 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -85,6 +85,10 @@ void server_init(struct server *server) { SDL_bool server_start(struct server *server, const char *serial, Uint16 local_port, Uint16 max_size, Uint32 bit_rate) { + if (serial) { + server->serial = SDL_strdup(serial); + } + if (!push_server(serial)) { return SDL_FALSE; } @@ -121,7 +125,7 @@ SDL_bool server_start(struct server *server, const char *serial, Uint16 local_po return SDL_TRUE; } -socket_t server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms) { +socket_t server_connect_to(struct server *server, Uint32 timeout_ms) { server->device_socket = net_accept(server->server_socket); if (server->device_socket == INVALID_SOCKET) { return INVALID_SOCKET; @@ -131,17 +135,17 @@ socket_t server_connect_to(struct server *server, const char *serial, Uint32 tim close_socket(&server->server_socket); // the server is started, we can clean up the jar from the temporary folder - remove_server(serial); // ignore failure + remove_server(server->serial); // ignore failure server->server_copied_to_device = SDL_FALSE; // we don't need the adb tunnel anymore - disable_tunnel(serial); // ignore failure + disable_tunnel(server->serial); // ignore failure server->adb_reverse_enabled = SDL_FALSE; return server->device_socket; } -void server_stop(struct server *server, const char *serial) { +void server_stop(struct server *server) { SDL_assert(server->process != PROCESS_NONE); if (!cmd_terminate(server->process)) { @@ -153,11 +157,11 @@ void server_stop(struct server *server, const char *serial) { if (server->adb_reverse_enabled) { // ignore failure - disable_tunnel(serial); + disable_tunnel(server->serial); } if (server->server_copied_to_device) { - remove_server(serial); // ignore failure + remove_server(server->serial); // ignore failure } } @@ -168,4 +172,5 @@ void server_destroy(struct server *server) { if (server->device_socket != INVALID_SOCKET) { close_socket(&server->device_socket); } + SDL_free((void *) server->serial); } diff --git a/app/src/server.h b/app/src/server.h index c2fe1125..63547343 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -5,6 +5,7 @@ #include "net.h" struct server { + const char *serial; process_t process; socket_t server_socket; socket_t device_socket; @@ -13,6 +14,7 @@ struct server { }; #define SERVER_INITIALIZER { \ + .serial = NULL, \ .process = PROCESS_NONE, \ .server_socket = INVALID_SOCKET, \ .device_socket = INVALID_SOCKET, \ @@ -28,10 +30,10 @@ SDL_bool server_start(struct server *server, const char *serial, Uint16 local_po Uint16 max_size, Uint32 bit_rate); // block until the communication with the server is established -socket_t server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms); +socket_t server_connect_to(struct server *server, Uint32 timeout_ms); // disconnect and kill the server process -void server_stop(struct server *server, const char *serial); +void server_stop(struct server *server); // close and release sockets void server_destroy(struct server *server);