diff options
Diffstat (limited to 'common.c')
-rw-r--r-- | common.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -95,6 +95,74 @@ xwrite (int fd, const char *data, size_t len, struct error **e) return true; } +// --- Simple network I/O ------------------------------------------------------ + +// TODO: move to liberty and remove from dwmstatus.c as well + +#define SOCKET_IO_OVERFLOW (8 << 20) ///< How large a read buffer can be + +enum socket_io_result +{ + SOCKET_IO_OK, ///< Completed successfully + SOCKET_IO_EOF, ///< Connection shut down by peer + SOCKET_IO_ERROR ///< Connection error +}; + +static enum socket_io_result +socket_io_try_read (int socket_fd, struct str *rb, struct error **e) +{ + // We allow buffering of a fair amount of data, however within reason, + // so that it's not so easy to flood us and cause an allocation failure + ssize_t n_read; + while (rb->len < SOCKET_IO_OVERFLOW) + { + str_ensure_space (rb, 4096); + n_read = recv (socket_fd, rb->str + rb->len, + rb->alloc - rb->len - 1 /* null byte */, 0); + + if (n_read > 0) + { + rb->str[rb->len += n_read] = '\0'; + continue; + } + if (n_read == 0) + return SOCKET_IO_EOF; + + if (errno == EAGAIN) + return SOCKET_IO_OK; + if (errno == EINTR) + continue; + + error_set (e, "%s", strerror (errno)); + return SOCKET_IO_ERROR; + } + return SOCKET_IO_OK; +} + +static enum socket_io_result +socket_io_try_write (int socket_fd, struct str *wb, struct error **e) +{ + ssize_t n_written; + while (wb->len) + { + n_written = send (socket_fd, wb->str, wb->len, 0); + if (n_written >= 0) + { + str_remove_slice (wb, 0, n_written); + continue; + } + + if (errno == EAGAIN) + return SOCKET_IO_OK; + if (errno == EINTR) + continue; + + error_set (e, "%s", strerror (errno)); + return SOCKET_IO_ERROR; + } + return SOCKET_IO_OK; +} + // --- Logging ----------------------------------------------------------------- static void |