diff --git a/app/src/controller.c b/app/src/controller.c index 24485bdd..2de0e86f 100644 --- a/app/src/controller.c +++ b/app/src/controller.c @@ -47,7 +47,7 @@ static SDL_bool process_event(struct controller *controller, const struct contro if (!length) { return SDL_FALSE; } - int w = net_send(controller->video_socket, serialized_event, length); + int w = net_send_all(controller->video_socket, serialized_event, length); return w == length; } diff --git a/app/src/device.c b/app/src/device.c index a166274f..1e5ea625 100644 --- a/app/src/device.c +++ b/app/src/device.c @@ -3,7 +3,7 @@ SDL_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(device_socket, buf, sizeof(buf)); + int r = net_recv_all(device_socket, buf, sizeof(buf)); if (r < DEVICE_NAME_FIELD_LENGTH + 4) { LOGE("Could not retrieve device information"); return SDL_FALSE; diff --git a/app/src/net.c b/app/src/net.c index 11a66e4c..bfb513f3 100644 --- a/app/src/net.c +++ b/app/src/net.c @@ -51,6 +51,23 @@ ssize_t net_recv(socket_t socket, void *buf, size_t len) { return recv(socket, buf, len, 0); } +ssize_t net_recv_all(socket_t socket, void *buf, size_t len) { + return recv(socket, buf, len, MSG_WAITALL); +} + ssize_t net_send(socket_t socket, void *buf, size_t len) { return send(socket, buf, len, 0); } + +ssize_t net_send_all(socket_t socket, void *buf, size_t len) { + ssize_t w; + while (len > 0) { + w = send(socket, buf, len, 0); + if (w == -1) { + return -1; + } + len -= w; + buf += w; + } + return w; +} diff --git a/app/src/net.h b/app/src/net.h index bf22605c..8d8994de 100644 --- a/app/src/net.h +++ b/app/src/net.h @@ -19,8 +19,12 @@ void net_cleanup(void); socket_t net_listen(Uint32 addr, Uint16 port, int backlog); socket_t net_accept(socket_t server_socket); + +// the _all versions wait/retry until len bytes have been written/read ssize_t net_recv(socket_t socket, void *buf, size_t len); +ssize_t net_recv_all(socket_t socket, void *buf, size_t len); ssize_t net_send(socket_t socket, void *buf, size_t len); +ssize_t net_send_all(socket_t socket, void *buf, size_t len); void net_close(socket_t socket); #endif