server_params_copy

This commit is contained in:
Romain Vimont 2021-05-02 15:01:18 +02:00
commit 52f5c6d4c1
3 changed files with 72 additions and 12 deletions

View file

@ -243,10 +243,6 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
bool bool
scrcpy(const struct scrcpy_options *options) { scrcpy(const struct scrcpy_options *options) {
if (!server_init(&server)) {
return false;
}
bool ret = false; bool ret = false;
bool server_started = false; bool server_started = false;
@ -279,7 +275,12 @@ scrcpy(const 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(&server, options->serial, &params)) {
if (!server_init(&server, &params)) {
return false;
}
if (!server_start(&server, options->serial)) {
goto end; goto end;
} }

View file

@ -352,8 +352,61 @@ close_socket(socket_t socket) {
} }
} }
static void
server_params_destroy(struct server_params *params) {
// The server stores a copy of the params provided by the user
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) {
// params reference user-allocated memory, so we must copy them to handle
// them from a separate thread
*dst = *src;
dst->crop = NULL;
dst->codec_options = NULL;
dst->encoder_name = NULL;
if (src->crop) {
dst->crop = strdup(src->crop);
if (!dst->crop) {
goto error;
}
}
if (src->codec_options) {
dst->codec_options = strdup(src->codec_options);
if (!dst->codec_options) {
goto error;
}
}
if (src->encoder_name) {
dst->encoder_name = strdup(src->encoder_name);
if (!dst->encoder_name) {
goto error;
}
}
return true;
error:
server_params_destroy(dst);
return false;
};
bool bool
server_init(struct server *server) { server_init(struct server *server, const struct server_params *params) {
if (!server_params_copy(&server->params, params)) {
LOGE("Could not copy server params");
return false;
}
server->serial = NULL; server->serial = NULL;
server->process = PROCESS_NONE; server->process = PROCESS_NONE;
atomic_flag_clear_explicit(&server->server_socket_closed, atomic_flag_clear_explicit(&server->server_socket_closed,
@ -361,12 +414,14 @@ server_init(struct server *server) {
bool ok = sc_mutex_init(&server->mutex); bool ok = sc_mutex_init(&server->mutex);
if (!ok) { if (!ok) {
server_params_destroy(&server->params);
return false; return false;
} }
ok = sc_cond_init(&server->process_terminated_cond); ok = sc_cond_init(&server->process_terminated_cond);
if (!ok) { if (!ok) {
sc_mutex_destroy(&server->mutex); sc_mutex_destroy(&server->mutex);
server_params_destroy(&server->params);
return false; return false;
} }
@ -407,8 +462,9 @@ run_wait_server(void *data) {
} }
bool bool
server_start(struct server *server, const char *serial, server_start(struct server *server, const char *serial) {
const struct server_params *params) { const struct server_params *params = &server->params;
if (serial) { if (serial) {
server->serial = strdup(serial); server->serial = strdup(serial);
if (!server->serial) { if (!server->serial) {
@ -558,4 +614,5 @@ server_destroy(struct server *server) {
free(server->serial); free(server->serial);
sc_cond_destroy(&server->process_terminated_cond); sc_cond_destroy(&server->process_terminated_cond);
sc_mutex_destroy(&server->mutex); sc_mutex_destroy(&server->mutex);
server_params_destroy(&server->params);
} }

View file

@ -47,16 +47,18 @@ struct server {
uint16_t local_port; // selected from port_range uint16_t local_port; // selected from port_range
bool tunnel_enabled; bool tunnel_enabled;
bool tunnel_forward; // use "adb forward" instead of "adb reverse" bool tunnel_forward; // use "adb forward" instead of "adb reverse"
// The internal allocated strings are copies owned by the server
struct server_params params;
}; };
// init default values // init server fields
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 char *serial, server_start(struct server *server, const char *serial);
const struct server_params *params);
// block until the communication with the server is established // block until the communication with the server is established
bool bool