diff --git a/gdb_proto.c b/gdb_proto.c index fa497ae..a3715bb 100644 --- a/gdb_proto.c +++ b/gdb_proto.c @@ -50,10 +50,15 @@ void gdb_printf(struct gdb_data *data, const char *fmt, ...) data->outlen += len; } +/* Returns -1 for error, 0 for timeout, >0 if data received. */ static int gdb_read(struct gdb_data *data, int timeout_ms) { + int was_timeout; int len = sockets_recv(data->sock, data->xbuf, sizeof(data->xbuf), 0, - timeout_ms); + timeout_ms, &was_timeout); + + if (was_timeout) + return 0; if (len < 0) { data->error = 1; @@ -74,8 +79,8 @@ static int gdb_read(struct gdb_data *data, int timeout_ms) int gdb_peek(struct gdb_data *data, int timeout_ms) { - if (data->head == data->tail && gdb_read(data, timeout_ms) < 0) - return -1; + if (data->head == data->tail) + return gdb_read(data, timeout_ms); return data->head != data->tail; } @@ -85,7 +90,7 @@ int gdb_getc(struct gdb_data *data) int c; /* If the buffer is empty, receive some more data */ - if (data->head == data->tail && gdb_read(data, -1) < 0) + if (data->head == data->tail && gdb_read(data, -1) <= 0) return -1; c = data->xbuf[data->head]; diff --git a/sockets.c b/sockets.c index 98b9525..1fbffc8 100644 --- a/sockets.c +++ b/sockets.c @@ -120,7 +120,7 @@ ssize_t sockets_send(SOCKET s, const void *buf, size_t len, int flags) } ssize_t sockets_recv(SOCKET s, void *buf, size_t len, int flags, - int timeout_ms) + int timeout_ms, int *was_timeout) { int ret = -1; DWORD to_arg = (timeout_ms >= 0) ? timeout_ms : INFINITE; @@ -131,6 +131,9 @@ ssize_t sockets_recv(SOCKET s, void *buf, size_t len, int flags, ret = recv(s, buf, len, flags); } while (ret < 0 && !sockets_wait(to_arg)); + if (was_timeout) + *was_timeout = (ret < 0 && (error_save == WAIT_TIMEOUT)); + sockets_end(s); return ret; } @@ -151,7 +154,7 @@ ssize_t sockets_send(SOCKET s, const void *buf, size_t len, int flags) } ssize_t sockets_recv(SOCKET s, void *buf, size_t buf_len, int flags, - int timeout_ms) + int timeout_ms, int *was_timeout) { fd_set r; struct timeval to = { @@ -166,6 +169,9 @@ ssize_t sockets_recv(SOCKET s, void *buf, size_t buf_len, int flags, timeout_ms < 0 ? NULL : &to) < 0) return -1; + if (was_timeout) + *was_timeout = !FD_ISSET(s, &r); + if (!FD_ISSET(s, &r)) { errno = ETIMEDOUT; return 0; diff --git a/sockets.h b/sockets.h index 450cef2..208ec23 100644 --- a/sockets.h +++ b/sockets.h @@ -48,6 +48,6 @@ SOCKET sockets_accept(SOCKET s, struct sockaddr *addr, socklen_t *addrlen); int sockets_connect(SOCKET s, const struct sockaddr *addr, socklen_t addrlen); ssize_t sockets_send(SOCKET s, const void *buf, size_t len, int flags); ssize_t sockets_recv(SOCKET s, void *buf, size_t len, int flags, - int timeout_ms); + int timeout_ms, int *was_timeout); #endif