mirror of https://github.com/sipwise/rtpengine.git
parent
2f183d8ce3
commit
cffb385bcf
@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <hiredis.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "redis.h"
|
||||
#include "aux.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct redis *redis_new(u_int32_t ip, u_int16_t port, char *key) {
|
||||
struct redis *r;
|
||||
struct timeval tv;
|
||||
redisReply *rp;
|
||||
|
||||
r = malloc(sizeof(*r));
|
||||
ZERO(*r);
|
||||
|
||||
sprintf(r->host, IPF, IPP(ip));
|
||||
r->port = port;
|
||||
r->key = key;
|
||||
|
||||
tv.tv_sec = 1;
|
||||
tv.tv_usec = 0;
|
||||
r->ctx = redisConnectWithTimeout(r->host, r->port, tv);
|
||||
|
||||
if (!r->ctx)
|
||||
goto err;
|
||||
if (r->ctx->err)
|
||||
goto err;
|
||||
|
||||
rp = redisCommand(r->ctx, "PING");
|
||||
if (!rp)
|
||||
goto err;
|
||||
freeReplyObject(rp);
|
||||
|
||||
return r;
|
||||
|
||||
err:
|
||||
if (r->ctx && r->ctx->errstr)
|
||||
mylog(LOG_CRIT, "Redis error: %s\n", r->ctx->errstr);
|
||||
if (r->ctx)
|
||||
redisFree(r->ctx);
|
||||
free(r);
|
||||
return NULL;
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
#ifndef __REDIS_H__
|
||||
#define __REDIS_H__
|
||||
|
||||
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <hiredis.h>
|
||||
|
||||
|
||||
|
||||
|
||||
struct redis {
|
||||
char host[32];
|
||||
int port;
|
||||
|
||||
redisContext *ctx;
|
||||
char *key;
|
||||
int active:1;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
struct redis *redis_new(u_int32_t, u_int16_t, char *);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Loading…
Reference in new issue