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.
This commit is contained in:
Romain Vimont 2018-03-12 10:19:12 +01:00
parent 9e328ef98b
commit 2b3ed5bcdb
3 changed files with 21 additions and 14 deletions

View file

@ -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);

View file

@ -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);
}

View file

@ -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);