Skip to content

Commit

Permalink
Provide device info directly on server connection
Browse files Browse the repository at this point in the history
This avoids to retrieve them in a separate step.
  • Loading branch information
rom1v committed May 17, 2021
1 parent 6a2cd08 commit 6adc971
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 53 deletions.
1 change: 0 additions & 1 deletion app/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
23 changes: 0 additions & 23 deletions app/src/device.c

This file was deleted.

17 changes: 0 additions & 17 deletions app/src/device.h

This file was deleted.

10 changes: 1 addition & 9 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include "controller.h"
#include "decoder.h"
#include "device.h"
#include "events.h"
#include "file_handler.h"
#include "input_manager.h"
Expand Down Expand Up @@ -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;
}

Expand Down
25 changes: 23 additions & 2 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,28 @@ server_start(struct server *server, const char *serial,
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) {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdint.h>

#include "adb.h"
#include "coords.h"
#include "scrcpy.h"
#include "util/log.h"
#include "util/net.h"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6adc971

Please sign in to comment.