From 6adc97198bd6fc9122c9cd9cef319420219a384d Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 9 May 2021 16:52:22 +0200 Subject: [PATCH] Provide device info directly on server connection This avoids to retrieve them in a separate step. --- app/meson.build | 1 - app/src/device.c | 23 ----------------------- app/src/device.h | 17 ----------------- app/src/scrcpy.c | 10 +--------- app/src/server.c | 25 +++++++++++++++++++++++-- app/src/server.h | 5 ++++- 6 files changed, 28 insertions(+), 53 deletions(-) delete mode 100644 app/src/device.c delete mode 100644 app/src/device.h diff --git a/app/meson.build b/app/meson.build index c1dbf489..6b6991aa 100644 --- a/app/meson.build +++ b/app/meson.build @@ -6,7 +6,6 @@ src = [ 'src/control_msg.c', 'src/controller.c', 'src/decoder.c', - 'src/device.c', 'src/device_msg.c', 'src/event_converter.c', 'src/file_handler.c', diff --git a/app/src/device.c b/app/src/device.c deleted file mode 100644 index 094dc616..00000000 --- a/app/src/device.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "device.h" - -#include "util/log.h" - -bool -device_read_info(socket_t device_socket, char *device_name, struct size *size) { - unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4]; - int r = net_recv_all(device_socket, buf, sizeof(buf)); - if (r < DEVICE_NAME_FIELD_LENGTH + 4) { - LOGE("Could not retrieve device information"); - return false; - } - // 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]; - return true; -} diff --git a/app/src/device.h b/app/src/device.h deleted file mode 100644 index 376e3d4b..00000000 --- a/app/src/device.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef DEVICE_H -#define DEVICE_H - -#include "common.h" - -#include - -#include "coords.h" -#include "util/net.h" - -#define DEVICE_NAME_FIELD_LENGTH 64 - -// name must be at least DEVICE_NAME_FIELD_LENGTH bytes -bool -device_read_info(socket_t device_socket, char *device_name, struct size *size); - -#endif diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 8d14a21b..8418353d 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -15,7 +15,6 @@ #include "controller.h" #include "decoder.h" -#include "device.h" #include "events.h" #include "file_handler.h" #include "input_manager.h" @@ -284,17 +283,10 @@ scrcpy(const struct scrcpy_options *options) { goto end; } - if (!server_connect_to(&server)) { - goto end; - } - char device_name[DEVICE_NAME_FIELD_LENGTH]; struct size frame_size; - // screenrecord does not send frames when the screen content does not - // change therefore, we transmit the screen size before the video stream, - // to be able to init the window immediately - if (!device_read_info(server.video_socket, device_name, &frame_size)) { + if (!server_connect_to(&server, device_name, &frame_size)) { goto end; } diff --git a/app/src/server.c b/app/src/server.c index 4448ab95..646006d6 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -464,8 +464,28 @@ error: return false; } +static bool +device_read_info(socket_t device_socket, char *device_name, struct size *size) { + unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4]; + int r = net_recv_all(device_socket, buf, sizeof(buf)); + if (r < DEVICE_NAME_FIELD_LENGTH + 4) { + LOGE("Could not retrieve device information"); + return false; + } + // 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]; + return true; +} + bool -server_connect_to(struct server *server) { +server_connect_to(struct server *server, char *device_name, struct size *size) { if (!server->tunnel_forward) { server->video_socket = net_accept(server->server_socket); if (server->video_socket == INVALID_SOCKET) { @@ -505,7 +525,8 @@ server_connect_to(struct server *server) { disable_tunnel(server); // ignore failure server->tunnel_enabled = false; - return true; + // The sockets will be closed on stop if device_read_info() fails + return device_read_info(server->video_socket, device_name, size); } void diff --git a/app/src/server.h b/app/src/server.h index 15306e4f..ad49a982 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -8,6 +8,7 @@ #include #include "adb.h" +#include "coords.h" #include "scrcpy.h" #include "util/log.h" #include "util/net.h" @@ -58,9 +59,11 @@ bool server_start(struct server *server, const char *serial, 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); +server_connect_to(struct server *server, char *device_name, struct size *size); // disconnect and kill the server process void