From 8ad728c6a86a1d4088c753a3071cba2abe7fc19b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Mon, 4 Jan 1999 11:29:29 +0100 Subject: [PATCH] * io.c (write_raw): New function. (write_raw_with_poll): -"- Rev: src/io.c:1.40 Rev: src/io.h:1.26 --- src/io.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/io.h | 3 +++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/io.c b/src/io.c index 9734c9be..19820e8f 100644 --- a/src/io.c +++ b/src/io.c @@ -234,7 +234,7 @@ static int do_read(struct abstract_read **r, UINT32 length, UINT8 *buffer) case EWOULDBLOCK: /* aka EAGAIN */ return 0; case EPIPE: - werror("io.c: read() returned EPIPE! Treating it as EOF.\n"); + wwrite("io.c: read() returned EPIPE! Treating it as EOF.\n"); return A_EOF; default: werror("io.c: do_read: read() failed (errno %d), %s\n", @@ -358,7 +358,7 @@ static void listen_callback(struct lsh_fd *fd) res = FD_CALLBACK(self->callback, conn); if (LSH_ACTIONP(res)) { - werror("Strange: Accepted a connection, " + wwrite("Strange: Accepted a connection, " "but failed before writing anything.\n"); close_fd(fd, (LSH_FAILUREP(res) ? CLOSE_PROTOCOL_FAILURE @@ -375,7 +375,7 @@ static void connect_callback(struct lsh_fd *fd) if (LSH_ACTIONP(res)) { - werror("Strange: Connected, " + wwrite("Strange: Connected, " "but failed before writing anything.\n"); } else @@ -535,6 +535,70 @@ get_inaddr(struct sockaddr_in * addr, return 1; } +/* For fd:s in blocking mode. */ +int write_raw(int fd, UINT32 length, UINT8 *data) +{ + while(length) + { + int written = write(fd, data, length); + + if (written < 0) + switch(errno) + { + case EINTR: + case EAGAIN: + continue; + default: + return 0; + } + + length -= written; + data += written; + } + return 1; +} + +int write_raw_with_poll(int fd, UINT32 length, UINT8 *data) +{ + while(length) + { + struct pollfd pfd; + int res; + int written; + + pfd.fd = fd; + pfd.events = POLLOUT; + + res = poll(&pfd, 1, -1); + + if (res < 0) + switch(errno) + { + case EINTR: + case EAGAIN: + continue; + default: + return 0; + } + + written = write(fd, data, length); + + if (written < 0) + switch(errno) + { + case EINTR: + case EAGAIN: + continue; + default: + return 0; + } + + length -= written; + data += written; + } + return 1; +} + void io_set_nonblocking(int fd) { if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) diff --git a/src/io.h b/src/io.h index b739d24e..dcc3e66b 100644 --- a/src/io.h +++ b/src/io.h @@ -159,6 +159,9 @@ int get_inaddr(struct sockaddr_in * addr, const char * service, const char * protocol); +int write_raw(int fd, UINT32 length, UINT8 *data); +int write_raw_with_poll(int fd, UINT32 length, UINT8 *data); + void io_set_nonblocking(int fd); void io_set_close_on_exec(int fd); void io_init_fd(int fd); -- GitLab