Skip to content

Commit

Permalink
Do not report error on device disconnected
Browse files Browse the repository at this point in the history
A device disconnection (when the adb connection is closed) makes the
read() on the "receiver" socket fail.

Since commit 063a833, this is reported
as an error. As a consequence, scrcpy fails with:

    ERROR: Controller error

instead of:

    WARN: Device disconnected

To fix the issue, report a device disconnection in that case.

PR #5044 <#5044>
  • Loading branch information
rom1v committed Jul 5, 2024
1 parent a4f8c02 commit b419eef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
40 changes: 26 additions & 14 deletions app/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
#define SC_CONTROL_MSG_QUEUE_MAX 64

static void
sc_controller_receiver_on_error(struct sc_receiver *receiver, void *userdata) {
sc_controller_receiver_on_ended(struct sc_receiver *receiver, bool error,
void *userdata) {
(void) receiver;

struct sc_controller *controller = userdata;
// Forward the event to the controller listener
controller->cbs->on_error(controller, controller->cbs_userdata);
controller->cbs->on_ended(controller, error, controller->cbs_userdata);
}

bool
Expand All @@ -27,7 +28,7 @@ sc_controller_init(struct sc_controller *controller, sc_socket control_socket,
}

static const struct sc_receiver_callbacks receiver_cbs = {
.on_error = sc_controller_receiver_on_error,
.on_ended = sc_controller_receiver_on_ended,
};

ok = sc_receiver_init(&controller->receiver, control_socket, &receiver_cbs,
Expand Down Expand Up @@ -55,7 +56,7 @@ sc_controller_init(struct sc_controller *controller, sc_socket control_socket,
controller->control_socket = control_socket;
controller->stopped = false;

assert(cbs && cbs->on_error);
assert(cbs && cbs->on_ended);
controller->cbs = cbs;
controller->cbs_userdata = cbs_userdata;

Expand Down Expand Up @@ -110,21 +111,30 @@ sc_controller_push_msg(struct sc_controller *controller,

static bool
process_msg(struct sc_controller *controller,
const struct sc_control_msg *msg) {
const struct sc_control_msg *msg, bool *eos) {
static uint8_t serialized_msg[SC_CONTROL_MSG_MAX_SIZE];
size_t length = sc_control_msg_serialize(msg, serialized_msg);
if (!length) {
*eos = false;
return false;
}

ssize_t w =
net_send_all(controller->control_socket, serialized_msg, length);
return (size_t) w == length;
if ((size_t) w != length) {
*eos = true;
return false;
}

return true;
}

static int
run_controller(void *data) {
struct sc_controller *controller = data;

bool error = false;

for (;;) {
sc_mutex_lock(&controller->mutex);
while (!controller->stopped
Expand All @@ -134,27 +144,29 @@ run_controller(void *data) {
if (controller->stopped) {
// stop immediately, do not process further msgs
sc_mutex_unlock(&controller->mutex);
LOGD("Controller stopped");
break;
}

assert(!sc_vecdeque_is_empty(&controller->queue));
struct sc_control_msg msg = sc_vecdeque_pop(&controller->queue);
sc_mutex_unlock(&controller->mutex);

bool ok = process_msg(controller, &msg);
bool eos;
bool ok = process_msg(controller, &msg, &eos);
sc_control_msg_destroy(&msg);
if (!ok) {
LOGD("Could not write msg to socket");
goto error;
if (eos) {
LOGD("Controller stopped (socket closed)");
} // else error already logged
error = !eos;
break;
}
}

return 0;
controller->cbs->on_ended(controller, error, controller->cbs_userdata);

error:
controller->cbs->on_error(controller, controller->cbs_userdata);

return 1; // ignored
return 0;
}

bool
Expand Down
3 changes: 2 additions & 1 deletion app/src/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct sc_controller {
};

struct sc_controller_callbacks {
void (*on_error)(struct sc_controller *controller, void *userdata);
void (*on_ended)(struct sc_controller *controller, bool error,
void *userdata);
};

bool
Expand Down
8 changes: 6 additions & 2 deletions app/src/receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sc_receiver_init(struct sc_receiver *receiver, sc_socket control_socket,
receiver->acksync = NULL;
receiver->uhid_devices = NULL;

assert(cbs && cbs->on_error);
assert(cbs && cbs->on_ended);
receiver->cbs = cbs;
receiver->cbs_userdata = cbs_userdata;

Expand Down Expand Up @@ -134,19 +134,23 @@ run_receiver(void *data) {
static uint8_t buf[DEVICE_MSG_MAX_SIZE];
size_t head = 0;

bool error = false;

for (;;) {
assert(head < DEVICE_MSG_MAX_SIZE);
ssize_t r = net_recv(receiver->control_socket, buf + head,
DEVICE_MSG_MAX_SIZE - head);
if (r <= 0) {
LOGD("Receiver stopped");
// device disconnected: keep error=false
break;
}

head += r;
ssize_t consumed = process_msgs(receiver, buf, head);
if (consumed == -1) {
// an error occurred
error = true;
break;
}

Expand All @@ -157,7 +161,7 @@ run_receiver(void *data) {
}
}

receiver->cbs->on_error(receiver, receiver->cbs_userdata);
receiver->cbs->on_ended(receiver, error, receiver->cbs_userdata);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct sc_receiver {
};

struct sc_receiver_callbacks {
void (*on_error)(struct sc_receiver *receiver, void *userdata);
void (*on_ended)(struct sc_receiver *receiver, bool error, void *userdata);
};

bool
Expand Down
11 changes: 8 additions & 3 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,18 @@ sc_audio_demuxer_on_ended(struct sc_demuxer *demuxer,
}

static void
sc_controller_on_error(struct sc_controller *controller, void *userdata) {
sc_controller_on_ended(struct sc_controller *controller, bool error,
void *userdata) {
// Note: this function may be called twice, once from the controller thread
// and once from the receiver thread
(void) controller;
(void) userdata;

PUSH_EVENT(SC_EVENT_CONTROLLER_ERROR);
if (error) {
PUSH_EVENT(SC_EVENT_CONTROLLER_ERROR);
} else {
PUSH_EVENT(SC_EVENT_DEVICE_DISCONNECTED);
}
}

static void
Expand Down Expand Up @@ -567,7 +572,7 @@ scrcpy(struct scrcpy_options *options) {

if (options->control) {
static const struct sc_controller_callbacks controller_cbs = {
.on_error = sc_controller_on_error,
.on_ended = sc_controller_on_ended,
};

if (!sc_controller_init(&s->controller, s->server.control_socket,
Expand Down

0 comments on commit b419eef

Please sign in to comment.