From 676fa73d2c19d4828921640588e42d1911ad2147 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 30 Oct 2021 19:07:35 +0200 Subject: [PATCH] Wrap device name and size in a struct As a benefit, this avoids to take care of the device name length on the caller side. --- app/src/scrcpy.c | 15 +++++++-------- app/src/server.c | 21 +++++++++------------ app/src/server.h | 11 +++++++---- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index c7033a26..c8adb5a1 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -331,10 +331,9 @@ scrcpy(struct scrcpy_options *options) { sdl_configure(options->display, options->disable_screensaver); - char device_name[DEVICE_NAME_FIELD_LENGTH]; - struct sc_size frame_size; + struct server_info info; - if (!server_connect_to(&s->server, device_name, &frame_size)) { + if (!server_connect_to(&s->server, &info)) { goto end; } @@ -361,7 +360,7 @@ scrcpy(struct scrcpy_options *options) { if (!recorder_init(&s->recorder, options->record_filename, options->record_format, - frame_size)) { + info.frame_size)) { goto end; } rec = &s->recorder; @@ -407,11 +406,11 @@ scrcpy(struct scrcpy_options *options) { if (options->display) { const char *window_title = - options->window_title ? options->window_title : device_name; + options->window_title ? options->window_title : info.device_name; struct screen_params screen_params = { .window_title = window_title, - .frame_size = frame_size, + .frame_size = info.frame_size, .always_on_top = options->always_on_top, .window_x = options->window_x, .window_y = options->window_y, @@ -434,8 +433,8 @@ scrcpy(struct scrcpy_options *options) { #ifdef HAVE_V4L2 if (options->v4l2_device) { - if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, frame_size, - options->v4l2_buffer)) { + if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, + info.frame_size, options->v4l2_buffer)) { goto end; } diff --git a/app/src/server.c b/app/src/server.c index 50d7f836..4a4d3ec4 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -426,8 +426,7 @@ error: } static bool -device_read_info(sc_socket device_socket, char *device_name, - struct sc_size *size) { +device_read_info(sc_socket device_socket, struct server_info *info) { unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4]; ssize_t r = net_recv_all(device_socket, buf, sizeof(buf)); if (r < DEVICE_NAME_FIELD_LENGTH + 4) { @@ -436,19 +435,17 @@ device_read_info(sc_socket device_socket, char *device_name, } // in case the client sends garbage buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0'; - // strcpy is safe here, since name contains at least - // DEVICE_NAME_FIELD_LENGTH bytes and strlen(buf) < DEVICE_NAME_FIELD_LENGTH - strcpy(device_name, (char *) buf); - size->width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8) - | buf[DEVICE_NAME_FIELD_LENGTH + 1]; - size->height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8) - | buf[DEVICE_NAME_FIELD_LENGTH + 3]; + memcpy(info->device_name, (char *) buf, sizeof(info->device_name)); + + info->frame_size.width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8) + | buf[DEVICE_NAME_FIELD_LENGTH + 1]; + info->frame_size.height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8) + | buf[DEVICE_NAME_FIELD_LENGTH + 3]; return true; } bool -server_connect_to(struct server *server, char *device_name, - struct sc_size *size) { +server_connect_to(struct server *server, struct server_info *info) { if (!server->tunnel_forward) { server->video_socket = net_accept(server->server_socket); if (server->video_socket == SC_INVALID_SOCKET) { @@ -489,7 +486,7 @@ server_connect_to(struct server *server, char *device_name, server->tunnel_enabled = false; // The sockets will be closed on stop if device_read_info() fails - return device_read_info(server->video_socket, device_name, size); + return device_read_info(server->video_socket, info); } void diff --git a/app/src/server.h b/app/src/server.h index b291c0a5..242b0525 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -14,6 +14,12 @@ #include "util/net.h" #include "util/thread.h" +#define DEVICE_NAME_FIELD_LENGTH 64 +struct server_info { + char device_name[DEVICE_NAME_FIELD_LENGTH]; + struct sc_size frame_size; +}; + struct server { char *serial; process_t process; @@ -58,12 +64,9 @@ server_init(struct server *server); bool server_start(struct server *server, const struct server_params *params); -#define DEVICE_NAME_FIELD_LENGTH 64 // block until the communication with the server is established -// device_name must point to a buffer of at least DEVICE_NAME_FIELD_LENGTH bytes bool -server_connect_to(struct server *server, char *device_name, - struct sc_size *size); +server_connect_to(struct server *server, struct server_info *info); // disconnect and kill the server process void