Skip to content

Commit

Permalink
Fix receiver on partial device messages
Browse files Browse the repository at this point in the history
If a single device message was read in several chunks from the control
socket, the communication was broken.
  • Loading branch information
rom1v committed Jun 4, 2020
1 parent 0025952 commit a4351ba
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/src/receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,24 @@ run_receiver(void *data) {

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

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

if (consumed) {
head -= consumed;
// shift the remaining data in the buffer
memmove(buf, &buf[consumed], r - consumed);
head = r - consumed;
memmove(buf, &buf[consumed], head);
}
}

Expand Down

0 comments on commit a4351ba

Please sign in to comment.