From 4ae7d1e9a9068f9effcf9af4baf39e8e4fc06d12 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 30 Oct 2021 19:07:35 +0200 Subject: [PATCH] server_info --- app/src/scrcpy.c | 15 +++++++-------- app/src/server.c | 21 +++++++++------------ app/src/server.h | 10 +++++++--- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 0c1289bc..43ed428e 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -332,10 +332,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; } @@ -362,7 +361,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; @@ -408,11 +407,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, @@ -435,8 +434,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 ed8bf092..44a0df35 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -503,8 +503,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) { @@ -513,19 +512,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) { @@ -570,7 +567,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 3132f4e7..43d61679 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_params { const char *serial; enum sc_log_level log_level; @@ -64,12 +70,10 @@ server_init(struct server *server, const struct server_params *params); bool server_start(struct server *server); -#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