events::events(const string& redis_host, unsigned short redis_port) { this->redis = redisAsyncConnect(redis_host.c_str(), redis_port); this->redis_pubsub = redisAsyncConnect(redis_host.c_str(), redis_port); if (this->redis->err || this->redis_pubsub->err) { throw string("Redis Async cannot connect."); } this->loop = ev_loop_new(EVBACKEND_POLL | EVBACKEND_SELECT); redisLibevAttach(this->loop, this->redis); redisLibevAttach(this->loop, this->redis_pubsub); }
void uq_redis_connect(EV_P) { pubContext = redisAsyncConnect("127.0.0.1", 6379); if (pubContext->err) { fprintf(stderr, "Redis Error %s\n", pubContext->errstr); exit(1); } redisLibevAttach(EV_A_ pubContext); redisAsyncSetConnectCallback(pubContext, connectCallback); }
bool Redox::initHiredis() { ctx_->data = (void *)this; // Back-reference if (ctx_->err) { logger_.fatal() << "Could not create a hiredis context: " << ctx_->errstr; { unique_lock<mutex> lk(connect_lock_); connect_state_ = INIT_ERROR; } connect_waiter_.notify_all(); return false; } // Attach event loop to hiredis if (redisLibevAttach(evloop_, ctx_) != REDIS_OK) { logger_.fatal() << "Could not attach libev event loop to hiredis."; { unique_lock<mutex> lk(connect_lock_); connect_state_ = INIT_ERROR; } connect_waiter_.notify_all(); return false; } // Set the callbacks to be invoked on server connection/disconnection if (redisAsyncSetConnectCallback(ctx_, Redox::connectedCallback) != REDIS_OK) { logger_.fatal() << "Could not attach connect callback to hiredis."; { unique_lock<mutex> lk(connect_lock_); connect_state_ = INIT_ERROR; } connect_waiter_.notify_all(); return false; } if (redisAsyncSetDisconnectCallback(ctx_, Redox::disconnectedCallback) != REDIS_OK) { logger_.fatal() << "Could not attach disconnect callback to hiredis."; { unique_lock<mutex> lk(connect_lock_); connect_state_ = INIT_ERROR; } connect_waiter_.notify_all(); return false; } return true; }
static int new_connect( struct cnt_pool *pool, uintptr_t **cite, va_list *ap ) { char *host = va_arg(*ap, char *); int port = va_arg(*ap, int); redisAsyncContext *c = redisAsyncConnect (host, port); if (!c || c->err) { /* Let *c leak for now... */ x_printf (E, "Error: %s\n", c->errstr); free (c); *cite = NULL; return -1; } redisLibevAttach(NULL, c); redisAsyncSetConnectCallback(c, _connectCallback); redisAsyncSetDisconnectCallback(c, _disconnectCallback); *cite = (uintptr_t *)c; return 0; }
int main (int argc, char **argv) { signal(SIGPIPE, SIG_IGN); redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); if (c->err) { /* Let *c leak for now... */ printf("Error: %s\n", c->errstr); return 1; } redisLibevAttach(EV_DEFAULT_ c); redisAsyncSetConnectCallback(c,connectCallback); redisAsyncSetDisconnectCallback(c,disconnectCallback); redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); ev_loop(EV_DEFAULT_ 0); return 0; }
int main() { signal(SIGPIPE, SIG_IGN); redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); if (c->err) { /* Let *c leak for now... */ printf("Error: %s\n", c->errstr); return 1; } redisLibevAttach(EV_DEFAULT_ c); redisAsyncSetConnectCallback(c,connectCallback); redisAsyncSetDisconnectCallback(c,disconnectCallback); // redisAsyncCommand(c, getCallback, (char*)"sub", "SUBSCRIBE name"); new ChatRoom(c, {"79153060652", "79030072726", "79853672651"}); ev_loop(EV_DEFAULT_ 0); return 0; }
void build_async_conn() { if (actx) { redisAsyncDisconnect(actx); } actx = redisAsyncConnect(REDIS_SERVER_IP, REDIS_SERVER_PORT); if (actx == NULL || actx->err) { if (actx) { log_error("redisAsyncConnect fails: %s", actx->errstr); //redisFree(actx); } else { log_error("redisAsyncConnect fails: can't allocate redis context"); } return; } redisLibevAttach(EV_DEFAULT_ actx); redisAsyncSetConnectCallback(actx, on_connect); redisAsyncSetDisconnectCallback(actx, on_disconnect); redisAsyncCommand(actx, on_expire, NULL, "SUBSCRIBE __keyevent@0__:expired"); }
static int lua_create_client(lua_State *L) { lua_redis_client_t *lua_redis_client = NULL; redisAsyncContext *redis_async_context = NULL; luv_ref_t *ref; const char *host = luaL_checkstring(L, 1); int port = luaL_checkint(L, 2); redis_async_context = redisAsyncConnect(host, port); if(redis_async_context->err) { redisAsyncFree(redis_async_context); return luaL_error(L, redis_async_context->errstr); } redisLibevAttach(EV_DEFAULT_ redis_async_context); lua_redis_client = (lua_redis_client_t *)lua_newuserdata( L, sizeof(lua_redis_client_t)); lua_redis_client->redis_async_context = redis_async_context; luaL_getmetatable(L, LUA_REDIS_CLIENT_MT); lua_setmetatable(L, -2); lua_newtable(L); lua_setfenv(L, -2); ref = ref_alloc(); ref->L = L; lua_pushvalue(L, -1); ref->r = luaL_ref(L, LUA_REGISTRYINDEX); redis_async_context->data = ref; return 1; }
/* Internal host connect - Sync or Async */ static int _host_connect( host_t *h, eredis_reader_t *r ) { redisContext *c; if (r) { /* Sync - not in EV context */ c = (h->port) ? redisConnect( h->target, h->port ) : redisConnectUnix( h->target ); if (! c) { fprintf(stderr, "eredis: error: connect sync %s NULL\n", h->target); return 0; } if (c->err) { #if EREDIS_VERBOSE>0 printf( "eredis: error: connect sync %s %d\n", h->target, c->err); #endif redisFree( c ); return 0; } r->ctx = c; r->host = h; } else { redisAsyncContext *ac; /* ASync - in EV context */ ac = (h->port) ? redisAsyncConnect( h->target, h->port ) : redisAsyncConnectUnix( h->target ); if (! ac) { printf( "eredis: error: connect async %s undef\n", h->target); return 0; } if (ac->err) { #if EREDIS_VERBOSE>0 printf( "eredis: error: connect async %s %d\n", h->target, ac->err); #endif redisAsyncFree( ac ); return 0; } h->async_ctx = ac; /* data for _redis_*_cb */ ac->data = h; /* Order is important here */ /* attach */ redisLibevAttach( h->e->loop, ac ); /* set callbacks */ redisAsyncSetDisconnectCallback( ac, _redis_disconnect_cb ); redisAsyncSetConnectCallback( ac, _redis_connect_cb ); c = (redisContext*) ac; } /* Apply keep-alive */ #ifdef HOST_TCP_KEEPALIVE if (h->port) { redisEnableKeepAlive( c ); if (r && (h->e->sync_to.tv_sec||h->e->sync_to.tv_usec)) { redisSetTimeout( c, h->e->sync_to ); } } #endif /* Override the maxbuf */ c->reader->maxbuf = EREDIS_READER_MAX_BUF; return 1; }