diff options
Diffstat (limited to 'hiredis.c')
-rw-r--r-- | hiredis.c | 52 |
1 files changed, 9 insertions, 43 deletions
@@ -34,7 +34,6 @@ #include "fmacros.h" #include <string.h> #include <stdlib.h> -#include <unistd.h> #include <assert.h> #include <errno.h> #include <ctype.h> @@ -43,6 +42,7 @@ #include "net.h" #include "sds.h" #include "sslio.h" +#include "win32.h" static redisReply *createReplyObject(int type); static void *createStringObject(const redisReadTask *task, char *str, size_t len); @@ -605,8 +605,7 @@ static redisContext *redisContextInit(const redisOptions *options) { void redisFree(redisContext *c) { if (c == NULL) return; - if (c->fd > 0) - close(c->fd); + redisNetClose(c); sdsfree(c->obuf); redisReaderFree(c->reader); @@ -622,9 +621,9 @@ void redisFree(redisContext *c) { free(c); } -int redisFreeKeepFd(redisContext *c) { - int fd = c->fd; - c->fd = -1; +redisFD redisFreeKeepFd(redisContext *c) { + redisFD fd = c->fd; + c->fd = REDIS_INVALID_FD; redisFree(c); return fd; } @@ -633,9 +632,7 @@ int redisReconnect(redisContext *c) { c->err = 0; memset(c->errstr, '\0', strlen(c->errstr)); - if (c->fd > 0) { - close(c->fd); - } + redisNetClose(c); sdsfree(c->obuf); redisReaderFree(c->reader); @@ -750,7 +747,7 @@ redisContext *redisConnectUnixNonBlock(const char *path) { return redisConnectWithOptions(&options); } -redisContext *redisConnectFd(int fd) { +redisContext *redisConnectFd(redisFD fd) { redisOptions options = {0}; options.type = REDIS_CONN_USERFD; options.endpoint.fd = fd; @@ -776,24 +773,6 @@ int redisEnableKeepAlive(redisContext *c) { return REDIS_OK; } -static int rawRead(redisContext *c, char *buf, size_t bufcap) { - int nread = read(c->fd, buf, bufcap); - if (nread == -1) { - if ((errno == EAGAIN && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) { - /* Try again later */ - return 0; - } else { - __redisSetError(c, REDIS_ERR_IO, NULL); - return -1; - } - } else if (nread == 0) { - __redisSetError(c, REDIS_ERR_EOF, "Server closed the connection"); - return -1; - } else { - return nread; - } -} - /* Use this function to handle a read event on the descriptor. It will try * and read some bytes from the socket and feed them to the reply parser. * @@ -808,7 +787,7 @@ int redisBufferRead(redisContext *c) { return REDIS_ERR; nread = c->flags & REDIS_SSL ? - redisSslRead(c, buf, sizeof(buf)) : rawRead(c, buf, sizeof(buf)); + redisSslRead(c, buf, sizeof(buf)) : redisNetRead(c, buf, sizeof(buf)); if (nread > 0) { if (redisReaderFeed(c->reader, buf, nread) != REDIS_OK) { __redisSetError(c, c->reader->err, c->reader->errstr); @@ -821,19 +800,6 @@ int redisBufferRead(redisContext *c) { return REDIS_OK; } -static int rawWrite(redisContext *c) { - int nwritten = write(c->fd, c->obuf, sdslen(c->obuf)); - if (nwritten < 0) { - if ((errno == EAGAIN && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) { - /* Try again later */ - } else { - __redisSetError(c, REDIS_ERR_IO, NULL); - return -1; - } - } - return nwritten; -} - /* Write the output buffer to the socket. * * Returns REDIS_OK when the buffer is empty, or (a part of) the buffer was @@ -850,7 +816,7 @@ int redisBufferWrite(redisContext *c, int *done) { return REDIS_ERR; if (sdslen(c->obuf) > 0) { - int nwritten = (c->flags & REDIS_SSL) ? redisSslWrite(c) : rawWrite(c); + int nwritten = (c->flags & REDIS_SSL) ? redisSslWrite(c) : redisNetWrite(c); if (nwritten < 0) { return REDIS_ERR; } else if (nwritten > 0) { |