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) { ...@@ -33,6 +33,38 @@ int redis_fd_error(int fd) {
return err; 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) { static int redis__nonblock(int fd, int nonblock) {
int flags; int flags;
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "address.h" #include "address.h"
int redis_fd_error(int fd); 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_address(const redis_address addr);
int redis_fd_connect_gai(int family, const char *host, int port, 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) { ...@@ -206,16 +206,13 @@ int redis_handle_write_from_buffer(redis_handle *h, int *drained) {
} }
if (sdslen(h->wbuf)) { if (sdslen(h->wbuf)) {
do { nwritten = redis_fd_write(h->fd, h->wbuf, sdslen(h->wbuf));
nwritten = write(h->fd, h->wbuf, sdslen(h->wbuf)); if (nwritten < 0) {
} while (nwritten == -1 && errno == EINTR);
if (nwritten == -1) {
/* Let all errors bubble, including EAGAIN */ /* Let all errors bubble, including EAGAIN */
return REDIS_ESYS; return nwritten;
} }
if (nwritten > 0) { if (nwritten) {
h->wbuf = sdsrange(h->wbuf, nwritten, -1); h->wbuf = sdsrange(h->wbuf, nwritten, -1);
} }
} }
...@@ -246,17 +243,9 @@ int redis_handle_read_to_buffer(redis_handle *h) { ...@@ -246,17 +243,9 @@ int redis_handle_read_to_buffer(redis_handle *h) {
return REDIS_ESYS; return REDIS_ESYS;
} }
do { nread = redis_fd_read(h->fd, buf, sizeof(buf));
nread = read(h->fd, buf, sizeof(buf)); if (nread < 0) {
} while (nread == -1 && errno == EINTR); return nread;
if (nread == -1) {
/* Let all errors bubble, including EAGAIN */
return REDIS_ESYS;
}
if (nread == 0) {
return REDIS_EEOF;
} }
h->rbuf = sdscatlen(h->rbuf, buf, 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