Copy server params

This is a preliminary step necessary to move the server to a separate
thread.
This commit is contained in:
Romain Vimont 2021-10-27 23:40:52 +02:00
parent 9fa4d1cd4a
commit 882e4cff5f
3 changed files with 72 additions and 26 deletions

View file

@ -275,10 +275,6 @@ scrcpy(struct scrcpy_options *options) {
atexit(SDL_Quit); atexit(SDL_Quit);
if (!server_init(&s->server)) {
return false;
}
bool ret = false; bool ret = false;
bool server_started = false; bool server_started = false;
@ -313,7 +309,12 @@ scrcpy(struct scrcpy_options *options) {
.force_adb_forward = options->force_adb_forward, .force_adb_forward = options->force_adb_forward,
.power_off_on_close = options->power_off_on_close, .power_off_on_close = options->power_off_on_close,
}; };
if (!server_start(&s->server, &params)) {
if (!server_init(&s->server, &params)) {
return false;
}
if (!server_start(&s->server)) {
goto end; goto end;
} }
@ -338,7 +339,7 @@ scrcpy(struct scrcpy_options *options) {
} }
if (options->display && options->control) { if (options->display && options->control) {
if (!file_handler_init(&s->file_handler, s->server.serial, if (!file_handler_init(&s->file_handler, options->serial,
options->push_target)) { options->push_target)) {
goto end; goto end;
} }

View file

@ -62,6 +62,44 @@ get_server_path(void) {
return server_path; return server_path;
} }
static void
server_params_destroy(struct server_params *params) {
// The server stores a copy of the params provided by the user
free((char *) params->serial);
free((char *) params->crop);
free((char *) params->codec_options);
free((char *) params->encoder_name);
}
static bool
server_params_copy(struct server_params *dst, const struct server_params *src) {
*dst = *src;
// The params reference user-allocated memory, so we must copy them to
// handle them from another thread
#define COPY(FIELD) \
dst->FIELD = NULL; \
if (src->FIELD) { \
dst->FIELD = strdup(src->FIELD); \
if (!dst->FIELD) { \
goto error; \
} \
}
COPY(serial);
COPY(crop);
COPY(codec_options);
COPY(encoder_name);
#undef COPY
return true;
error:
server_params_destroy(dst);
return false;
}
static bool static bool
push_server(const char *serial) { push_server(const char *serial) {
char *server_path = get_server_path(); char *server_path = get_server_path();
@ -106,9 +144,10 @@ static bool
disable_tunnel(struct server *server) { disable_tunnel(struct server *server) {
assert(server->tunnel_enabled); assert(server->tunnel_enabled);
const char *serial = server->params.serial;
bool ok = server->tunnel_forward bool ok = server->tunnel_forward
? disable_tunnel_forward(server->serial, server->local_port) ? disable_tunnel_forward(serial, server->local_port)
: disable_tunnel_reverse(server->serial); : disable_tunnel_reverse(serial);
// Consider tunnel disabled even if the command failed // Consider tunnel disabled even if the command failed
server->tunnel_enabled = false; server->tunnel_enabled = false;
@ -125,9 +164,10 @@ listen_on_port(sc_socket socket, uint16_t port) {
static bool static bool
enable_tunnel_reverse_any_port(struct server *server, enable_tunnel_reverse_any_port(struct server *server,
struct sc_port_range port_range) { struct sc_port_range port_range) {
const char *serial = server->params.serial;
uint16_t port = port_range.first; uint16_t port = port_range.first;
for (;;) { for (;;) {
if (!enable_tunnel_reverse(server->serial, port)) { if (!enable_tunnel_reverse(serial, port)) {
// the command itself failed, it will fail on any port // the command itself failed, it will fail on any port
return false; return false;
} }
@ -153,7 +193,7 @@ enable_tunnel_reverse_any_port(struct server *server,
} }
// failure, disable tunnel and try another port // failure, disable tunnel and try another port
if (!disable_tunnel_reverse(server->serial)) { if (!disable_tunnel_reverse(serial)) {
LOGW("Could not remove reverse tunnel on port %" PRIu16, port); LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
} }
@ -179,9 +219,11 @@ static bool
enable_tunnel_forward_any_port(struct server *server, enable_tunnel_forward_any_port(struct server *server,
struct sc_port_range port_range) { struct sc_port_range port_range) {
server->tunnel_forward = true; server->tunnel_forward = true;
const char *serial = server->params.serial;
uint16_t port = port_range.first; uint16_t port = port_range.first;
for (;;) { for (;;) {
if (enable_tunnel_forward(server->serial, port)) { if (enable_tunnel_forward(serial, port)) {
// success // success
server->local_port = port; server->local_port = port;
server->tunnel_enabled = true; server->tunnel_enabled = true;
@ -244,6 +286,8 @@ log_level_to_server_string(enum sc_log_level level) {
static sc_pid static sc_pid
execute_server(struct server *server, const struct server_params *params) { execute_server(struct server *server, const struct server_params *params) {
const char *serial = server->params.serial;
char max_size_string[6]; char max_size_string[6];
char bit_rate_string[11]; char bit_rate_string[11];
char max_fps_string[6]; char max_fps_string[6];
@ -301,7 +345,7 @@ execute_server(struct server *server, const struct server_params *params) {
// Port: 5005 // Port: 5005
// Then click on "Debug" // Then click on "Debug"
#endif #endif
return adb_execute(server->serial, cmd, ARRAY_LEN(cmd)); return adb_execute(serial, cmd, ARRAY_LEN(cmd));
} }
static bool static bool
@ -344,8 +388,13 @@ connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
} }
bool bool
server_init(struct server *server) { server_init(struct server *server, const struct server_params *params) {
server->serial = NULL; bool ok = server_params_copy(&server->params, params);
if (!ok) {
LOGE("Could not copy server params");
return false;
}
server->process = SC_PROCESS_NONE; server->process = SC_PROCESS_NONE;
server->server_socket = SC_INVALID_SOCKET; server->server_socket = SC_INVALID_SOCKET;
@ -377,13 +426,8 @@ server_on_terminated(void *userdata) {
} }
bool bool
server_start(struct server *server, const struct server_params *params) { server_start(struct server *server) {
if (params->serial) { const struct server_params *params = &server->params;
server->serial = strdup(params->serial);
if (!server->serial) {
return false;
}
}
if (!push_server(params->serial)) { if (!push_server(params->serial)) {
/* server->serial will be freed on server_destroy() */ /* server->serial will be freed on server_destroy() */
@ -582,5 +626,5 @@ server_destroy(struct server *server) {
LOGW("Could not close control socket"); LOGW("Could not close control socket");
} }
} }
free(server->serial); server_params_destroy(&server->params);
} }

View file

@ -40,7 +40,8 @@ struct server_params {
}; };
struct server { struct server {
char *serial; // The internal allocated strings are copies owned by the server
struct server_params params;
sc_pid process; sc_pid process;
// alive only between start() and stop() // alive only between start() and stop()
@ -54,13 +55,13 @@ struct server {
bool tunnel_forward; // use "adb forward" instead of "adb reverse" bool tunnel_forward; // use "adb forward" instead of "adb reverse"
}; };
// init default values // init the server with the given params
bool bool
server_init(struct server *server); server_init(struct server *server, const struct server_params *params);
// push, enable tunnel et start the server // push, enable tunnel et start the server
bool bool
server_start(struct server *server, const struct server_params *params); server_start(struct server *server);
// block until the communication with the server is established // block until the communication with the server is established
bool bool