diff options
author | Mark Nunberg <mnunberg@haskalah.org> | 2018-12-05 20:28:13 -0500 |
---|---|---|
committer | Mark Nunberg <mnunberg@haskalah.org> | 2019-02-20 09:10:10 -0500 |
commit | e4a78006e73fada8084b15618b7ba269fdda9237 (patch) | |
tree | 119cfe22dd4959a3c08136464c2533de3be76006 /hiredis.h | |
parent | d329cc952a61fc937c2bfe6887ed6d64d763f4f3 (diff) |
Provide option-struct initialization
This reduces the boilerplate of all the redisConnectXXX functions, and
allows us to provide more connection options in the future.
Diffstat (limited to 'hiredis.h')
-rw-r--r-- | hiredis.h | 45 |
1 files changed, 44 insertions, 1 deletions
@@ -112,11 +112,53 @@ void redisFreeSdsCommand(sds cmd); enum redisConnectionType { REDIS_CONN_TCP, - REDIS_CONN_UNIX + REDIS_CONN_UNIX, + REDIS_CONN_USERFD }; struct redisSsl; +#define REDIS_OPT_NONBLOCK 0x01 +#define REDIS_OPT_REUSEADDR 0x02 + +typedef struct { + /* + * the type of connection to use. This also indicates which + * `endpoint` member field to use + */ + int type; + /* bit field of REDIS_OPT_xxx */ + int options; + /* timeout value. if NULL, no timeout is used */ + const struct timeval *timeout; + union { + /** use this field for tcp/ip connections */ + struct { + const char *source_addr; + const char *ip; + int port; + } tcp; + /** use this field for unix domain sockets */ + const char *unix_socket; + /** + * use this field to have hiredis operate an already-open + * file descriptor */ + int fd; + } endpoint; +} redisOptions; + +/** + * Helper macros to initialize options to their specified fields. + */ +#define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) \ + (opts)->type = REDIS_CONN_TCP; \ + (opts)->endpoint.tcp.ip = ip_; \ + (opts)->endpoint.tcp.port = port; + +#define REDIS_OPTIONS_SET_UNIX(opts, path) \ + (opts)->type = REDIS_CONN_UNIX; \ + (opts)->endpoint.unix_socket = path; + /* Context for a connection to Redis */ typedef struct redisContext { int err; /* Error flags, 0 when there is no error */ @@ -147,6 +189,7 @@ typedef struct redisContext { } redisContext; +redisContext *redisConnectWithOptions(const redisOptions *options); redisContext *redisConnect(const char *ip, int port); redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv); redisContext *redisConnectNonBlock(const char *ip, int port); |