diff options
Diffstat (limited to 'src/io-unix.c')
-rw-r--r-- | src/io-unix.c | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/src/io-unix.c b/src/io-unix.c index cc1bd1b..8ed8e42 100644 --- a/src/io-unix.c +++ b/src/io-unix.c @@ -90,7 +90,8 @@ int tf_close(struct tf_fd *fd) return 0; } -int tf_read(struct tf_fd *fd, void *buf, size_t count, tf_mtime_diff_t timeout) +int tf_read_fully(struct tf_fd *fd, void *buf, size_t count, + tf_mtime_diff_t timeout) { ssize_t n; int r; @@ -127,8 +128,8 @@ int tf_read(struct tf_fd *fd, void *buf, size_t count, tf_mtime_diff_t timeout) return -r; } -int tf_write(struct tf_fd *fd, const void *buf, size_t count, - tf_mtime_diff_t timeout) +int tf_write_fully(struct tf_fd *fd, const void *buf, size_t count, + tf_mtime_diff_t timeout) { ssize_t n; int r; @@ -162,6 +163,53 @@ int tf_write(struct tf_fd *fd, const void *buf, size_t count, return -r; } +ssize_t tf_read(struct tf_fd *fd, void *buf, size_t count, tf_mtime_diff_t timeout) +{ + ssize_t n; + + tf_fd_monitor(fd, EPOLLIN); + do { + n = read(fd->fd, buf, count); + if (n >= 0) + break; + if (errno == EINTR) + continue; + if (errno != EAGAIN) { + n = -errno; + break; + } + n = tf_schedule(timeout); + timeout = TF_NO_TIMEOUT_CHANGE; + } while (n == TF_WAKEUP_FD); + tf_fd_unmonitor(fd); + + return n; +} + +ssize_t tf_write(struct tf_fd *fd, const void *buf, size_t count, + tf_mtime_diff_t timeout) +{ + ssize_t n; + + tf_fd_monitor(fd, EPOLLOUT); + do { + n = write(fd->fd, buf, count); + if (n >= 0) + break; + if (errno == EINTR) + continue; + if (errno != EAGAIN) { + n = -errno; + break; + } + n = tf_schedule(timeout); + timeout = TF_NO_TIMEOUT_CHANGE; + } while (n == TF_WAKEUP_FD); + tf_fd_unmonitor(fd); + + return n; +} + int tf_socket(struct tf_fd *fd, int domain, int type, int protocol) { int kfd, tfdf, on = 1; |