diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index df660c3e..924aab99 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -99,13 +99,16 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b return SDL_FALSE; } + SDL_bool ret = SDL_TRUE; + // to reduce startup time, we could be tempted to init other stuff before blocking here // but we should not block after SDL_Init since it handles the signals (Ctrl+C) in its // event loop: blocking could lead to deadlock TCPsocket device_socket = server_connect_to(&server, serial); if (!device_socket) { server_stop(&server, serial); - return SDL_FALSE; + ret = SDL_FALSE; + goto finally_destroy_server; } char device_name[DEVICE_NAME_FIELD_LENGTH]; @@ -116,16 +119,16 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b // to init the window immediately if (!device_read_info(device_socket, device_name, &frame_size)) { server_stop(&server, serial); - return SDL_FALSE; + ret = SDL_FALSE; + goto finally_destroy_server; } if (!frames_init(&frames)) { server_stop(&server, serial); - return SDL_FALSE; + ret = SDL_FALSE; + goto finally_destroy_server; } - SDL_bool ret = SDL_TRUE; - decoder.frames = &frames; decoder.video_socket = device_socket; @@ -173,6 +176,8 @@ finally_stop_decoder: decoder_join(&decoder); finally_destroy_frames: frames_destroy(&frames); +finally_destroy_server: + server_destroy(&server); return ret; } diff --git a/app/src/server.c b/app/src/server.c index 246083ab..e3c4e997 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -115,13 +115,6 @@ TCPsocket server_connect_to(struct server *server, const char *serial) { void server_stop(struct server *server, const char *serial) { SDL_assert(server->process != PROCESS_NONE); - if (server->server_socket) { - SDLNet_TCP_Close(server->server_socket); - } - if (server->device_socket) { - SDLNet_TCP_Close(server->device_socket); - } - terminate_server(server->process); if (server->adb_reverse_enabled) { @@ -129,3 +122,12 @@ void server_stop(struct server *server, const char *serial) { disable_tunnel(serial); } } + +void server_destroy(struct server *server) { + if (server->server_socket) { + SDLNet_TCP_Close(server->server_socket); + } + if (server->device_socket) { + SDLNet_TCP_Close(server->device_socket); + } +} diff --git a/app/src/server.h b/app/src/server.h index 082ac14a..3ba1af0b 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -31,4 +31,7 @@ TCPsocket server_connect_to(struct server *server, const char *serial); // disconnect and kill the server process void server_stop(struct server *server, const char *serial); +// close and release sockets +void server_destroy(struct server *server); + #endif