Fix receiver on partial device messages

If a single device message was read in several chunks from the control
socket, the communication was broken.
This commit is contained in:
Romain Vimont 2020-06-04 22:44:43 +02:00
parent 245999aec4
commit 00d292b2f5

View file

@ -65,23 +65,24 @@ run_receiver(void *data) {
for (;;) { for (;;) {
assert(head < DEVICE_MSG_MAX_SIZE); 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); DEVICE_MSG_MAX_SIZE - head);
if (r <= 0) { if (r <= 0) {
LOGD("Receiver stopped"); LOGD("Receiver stopped");
break; break;
} }
ssize_t consumed = process_msgs(buf, r); head += r;
ssize_t consumed = process_msgs(buf, head);
if (consumed == -1) { if (consumed == -1) {
// an error occurred // an error occurred
break; break;
} }
if (consumed) { if (consumed) {
head -= consumed;
// shift the remaining data in the buffer // shift the remaining data in the buffer
memmove(buf, &buf[consumed], r - consumed); memmove(buf, &buf[consumed], head);
head = r - consumed;
} }
} }