Skip to content

Commit

Permalink
Windows compatability, partially based on desb42's patch, but with cl…
Browse files Browse the repository at this point in the history
…eanup and additional fixes.
  • Loading branch information
koenvandesande committed Jun 30, 2011
1 parent 3cc6a7f commit a45d29f
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 10 deletions.
8 changes: 7 additions & 1 deletion async.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "fmacros.h"
#include <string.h>
#ifndef HIREDIS_WIN
#include <strings.h>
#endif
#include <assert.h>
#include <ctype.h>
#include "async.h"
Expand All @@ -47,8 +50,9 @@ static unsigned int callbackHash(const void *key) {
}

static void *callbackValDup(void *privdata, const void *src) {
redisCallback *dup;
((void) privdata);
redisCallback *dup = malloc(sizeof(*dup));
dup = malloc(sizeof(*dup));
memcpy(dup,src,sizeof(*dup));
return dup;
}
Expand Down Expand Up @@ -129,12 +133,14 @@ redisAsyncContext *redisAsyncConnect(const char *ip, int port) {
return ac;
}

#ifndef HIREDIS_WIN
redisAsyncContext *redisAsyncConnectUnix(const char *path) {
redisContext *c = redisConnectUnixNonBlock(path);
redisAsyncContext *ac = redisAsyncInitialize(c);
__redisAsyncCopyError(ac);
return ac;
}
#endif

int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) {
if (ac->onConnect == NULL) {
Expand Down
28 changes: 28 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Special config for MSVC build of hiredis
Does absolutely nothing for Unix!
*/
#ifndef __CONFIG_H
#define __CONFIG_H

#include "hiredis.h"
#ifdef HIREDIS_WIN

#ifndef va_copy
/* WARNING - DANGER - ASSUMES TYPICAL STACK MACHINE */
#define va_copy(dst, src) ((void)((dst) = (src)))
#endif

#if defined( _MSC_VER ) && !defined( __cplusplus )
#define inline __inline
#endif

#define snprintf sprintf_s
#define strcasecmp strcmp
#define strncasecmp _strnicmp
#define strerror_r(errorno, buf, len) strerror_s(buf, len, errorno)

#endif

#endif
4 changes: 4 additions & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

#include "hiredis.h"

#ifdef HIREDIS_WIN
#define snprintf sprintf_s
#endif

int main(void) {
unsigned int j;
redisContext *c;
Expand Down
39 changes: 38 additions & 1 deletion hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "fmacros.h"
#include <string.h>
#include <stdlib.h>
#ifndef HIREDIS_WIN
#include <unistd.h>
#endif
#include <assert.h>
#include <errno.h>
#include <ctype.h>
Expand Down Expand Up @@ -796,7 +799,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
}

/* Consume and discard vararg */
va_arg(ap,void);
va_arg(ap,char*);
}
}

Expand Down Expand Up @@ -832,7 +835,12 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {

pos = sprintf(cmd,"*%d\r\n",argc);
for (j = 0; j < argc; j++) {
#ifdef HIREDIS_WIN
/* %zu not understood by VS2008 */
pos += sprintf(cmd+pos,"$%lu\r\n",sdslen(curargv[j]));
#else
pos += sprintf(cmd+pos,"$%zu\r\n",sdslen(curargv[j]));
#endif
memcpy(cmd+pos,curargv[j],sdslen(curargv[j]));
pos += sdslen(curargv[j]);
sdsfree(curargv[j]);
Expand Down Expand Up @@ -941,6 +949,19 @@ void __redisSetError(redisContext *c, int type, const char *str) {
static redisContext *redisContextInit(void) {
redisContext *c;

#ifdef HIREDIS_WIN
/* fireup Windows socket stuff */
WORD wVersionRequested = MAKEWORD(2, 0);
WSADATA wsaData;
int ret;

ret = WSAStartup(wVersionRequested, &wsaData);
if (ret != 0) {
fprintf(stderr, "error: WSAStartup() failed: %d\n", ret);
return NULL;
}
#endif

c = calloc(1,sizeof(redisContext));
if (c == NULL)
return NULL;
Expand All @@ -954,7 +975,11 @@ static redisContext *redisContextInit(void) {

void redisFree(redisContext *c) {
if (c->fd > 0)
#ifdef HIREDIS_WIN
closesocket(c->fd);
#else
close(c->fd);
#endif
if (c->obuf != NULL)
sdsfree(c->obuf);
if (c->reader != NULL)
Expand Down Expand Up @@ -986,6 +1011,8 @@ redisContext *redisConnectNonBlock(const char *ip, int port) {
return c;
}

#ifndef HIREDIS_WIN
/* no Unix Domaind stuff */
redisContext *redisConnectUnix(const char *path) {
redisContext *c = redisContextInit();
c->flags |= REDIS_BLOCK;
Expand All @@ -1006,6 +1033,7 @@ redisContext *redisConnectUnixNonBlock(const char *path) {
redisContextConnectUnix(c,path,NULL);
return c;
}
#endif

/* Set read/write timeout on a blocking socket. */
int redisSetTimeout(redisContext *c, struct timeval tv) {
Expand All @@ -1027,7 +1055,11 @@ int redisBufferRead(redisContext *c) {
if (c->err)
return REDIS_ERR;

#ifdef HIREDIS_WIN
nread = recv(c->fd,buf,sizeof(buf),0);
#else
nread = read(c->fd,buf,sizeof(buf));
#endif
if (nread == -1) {
if (errno == EAGAIN && !(c->flags & REDIS_BLOCK)) {
/* Try again later */
Expand Down Expand Up @@ -1064,7 +1096,12 @@ int redisBufferWrite(redisContext *c, int *done) {
return REDIS_ERR;

if (sdslen(c->obuf) > 0) {
#ifdef HIREDIS_WIN
/* not treated as a file */
nwritten = send(c->fd,c->obuf,sdslen(c->obuf),0);
#else
nwritten = write(c->fd,c->obuf,sdslen(c->obuf));
#endif
if (nwritten == -1) {
if (errno == EAGAIN && !(c->flags & REDIS_BLOCK)) {
/* Try again later */
Expand Down
11 changes: 11 additions & 0 deletions hiredis.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,20 @@

#ifndef __HIREDIS_H
#define __HIREDIS_H

#if defined(_MSC_VER)
#define HIREDIS_WIN 1
#endif

#include <stdio.h> /* for size_t */
#include <stdarg.h> /* for va_list */

#ifndef HIREDIS_WIN
#include <sys/time.h> /* for struct timeval */
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#define HIREDIS_MAJOR 0
#define HIREDIS_MINOR 10
Expand Down
Loading

0 comments on commit a45d29f

Please sign in to comment.