Send and recv all data when necessary

Expose net_recv_all() and net_send_all(), equivalent of net_recv() and
net_send(), but that waits/retries until the requested length has been
transferred.

Use these new functions where it was (wrongly) assumed that the
requested length had been transferred.
This commit is contained in:
Romain Vimont 2018-02-15 23:55:52 +01:00
parent d646f95a31
commit a791c272bc
4 changed files with 23 additions and 2 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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