Commit 2defe232 authored by Pieter Noordhuis's avatar Pieter Noordhuis
Browse files

Extract fd r/w to separate file

parent f7097a17
......@@ -33,6 +33,38 @@ int redis_fd_error(int fd) {
return err;
}
int redis_fd_read(int fildes, void *buf, size_t nbyte) {
int nread;
do {
nread = read(fildes, buf, nbyte);
} while (nread == -1 && errno == EINTR);
if (nread == -1) {
return REDIS_ESYS;
}
if (nread == 0) {
return REDIS_EEOF;
}
return nread;
}
int redis_fd_write(int fildes, const void *buf, size_t nbyte) {
int nwritten;
do {
nwritten = write(fildes, buf, nbyte);
} while (nwritten == -1 && errno == EINTR);
if (nwritten == -1) {
return REDIS_ESYS;
}
return nwritten;
}
static int redis__nonblock(int fd, int nonblock) {
int flags;
......
......@@ -5,6 +5,8 @@
#include "address.h"
int redis_fd_error(int fd);
int redis_fd_read(int fildes, void *buf, size_t nbyte);
int redis_fd_write(int fildes, const void *buf, size_t nbyte);
int redis_fd_connect_address(const redis_address addr);
int redis_fd_connect_gai(int family, const char *host, int port, redis_address *addr);
......
......@@ -206,16 +206,13 @@ int redis_handle_write_from_buffer(redis_handle *h, int *drained) {
}
if (sdslen(h->wbuf)) {
do {
nwritten = write(h->fd, h->wbuf, sdslen(h->wbuf));
} while (nwritten == -1 && errno == EINTR);
if (nwritten == -1) {
nwritten = redis_fd_write(h->fd, h->wbuf, sdslen(h->wbuf));
if (nwritten < 0) {
/* Let all errors bubble, including EAGAIN */
return REDIS_ESYS;
return nwritten;
}
if (nwritten > 0) {
if (nwritten) {
h->wbuf = sdsrange(h->wbuf, nwritten, -1);
}
}
......@@ -246,17 +243,9 @@ int redis_handle_read_to_buffer(redis_handle *h) {
return REDIS_ESYS;
}
do {
nread = read(h->fd, buf, sizeof(buf));
} while (nread == -1 && errno == EINTR);
if (nread == -1) {
/* Let all errors bubble, including EAGAIN */
return REDIS_ESYS;
}
if (nread == 0) {
return REDIS_EEOF;
nread = redis_fd_read(h->fd, buf, sizeof(buf));
if (nread < 0) {
return nread;
}
h->rbuf = sdscatlen(h->rbuf, buf, nread);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment