Beispiel #1
0
proxy_client*AllocClient(int fd)
{
    proxy_client*c=malloc(sizeof(proxy_client));
    if(c==NULL) return NULL;
    c->flags=0;

    anetNonBlock(NULL,fd);
    anetTcpNoDelay(NULL,fd);

    c->fd=fd;
    c->blist=AllocBufferList(3);
    c->remote=AllocRemote(c);
    c->OnError=FreeClient;
    c->OnRemoteDown=CloseAfterSent;

    if(c->remote==NULL){
	//bug:c->blist should free
	close(fd);
	free(c);
	return NULL;
    }

    LogDebug("New client fd:%d remotefd:%d",c->fd,c->remote->fd);
    return c;
}
 client::client(const string_type & host, unsigned int port)
 {
   char err[ANET_ERR_LEN];
   socket_ = anetTcpConnect(err, const_cast<char*>(host.c_str()), port);
   if (socket_ == ANET_ERR) 
     throw connection_error(err);
   anetTcpNoDelay(NULL, socket_);
 }
Beispiel #3
0
/* Connect to a Redis instance. On success NULL is returned and *fd is set
 * to the socket file descriptor. On error a redisReply object is returned
 * with reply->type set to REDIS_REPLY_ERROR and reply->string containing
 * the error message. This replyObject must be freed with redisFreeReply(). */
redisReply *redisConnect(int *fd, const char *ip, int port) {
    char err[ANET_ERR_LEN];

    *fd = anetTcpConnect(err,ip,port);
    if (*fd == ANET_ERR)
        return createReplyObject(REDIS_REPLY_ERROR,sdsnew(err));
    anetTcpNoDelay(NULL,*fd);
    return NULL;
}
Beispiel #4
0
redisClient *createClient(int fd) {
    redisClient *c = zmalloc(sizeof(redisClient));

    /* passing -1 as fd it is possible to create a non connected client.
     * This is useful since all the Redis commands needs to be executed
     * in the context of a client. When commands are executed in other
     * contexts (for instance a Lua script) we need a non connected client. */
    if (fd != -1) {
        anetNonBlock(NULL,fd);
        anetTcpNoDelay(NULL,fd);
        if (aeCreateFileEvent(server.el,fd,AE_READABLE,
            readQueryFromClient, c) == AE_ERR)
        {
            close(fd);
            zfree(c);
            return NULL;
        }
    }

    selectDb(c,0);
    c->fd = fd;
    c->bufpos = 0;
    c->querybuf = sdsempty();
    c->querybuf_peak = 0;
    c->reqtype = 0;
    c->argc = 0;
    c->argv = NULL;
    c->cmd = c->lastcmd = NULL;
    c->multibulklen = 0;
    c->bulklen = -1;
    c->sentlen = 0;
    c->flags = 0;
    c->ctime = c->lastinteraction = server.unixtime;
    c->authenticated = 0;
    c->replstate = REDIS_REPL_NONE;
    c->slave_listening_port = 0;
    c->reply = listCreate();
    c->reply_bytes = 0;
    c->obuf_soft_limit_reached_time = 0;
    listSetFreeMethod(c->reply,decrRefCount);
    listSetDupMethod(c->reply,dupClientReplyValue);
    c->bpop.keys = NULL;
    c->bpop.count = 0;
    c->bpop.timeout = 0;
    c->bpop.target = NULL;
    c->io_keys = listCreate();
    c->watched_keys = listCreate();
    listSetFreeMethod(c->io_keys,decrRefCount);
    c->pubsub_channels = dictCreate(&setDictType,NULL);
    c->pubsub_patterns = listCreate();
    listSetFreeMethod(c->pubsub_patterns,decrRefCount);
    listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
    if (fd != -1) listAddNodeTail(server.clients,c);
    initClientMultiState(c);
    return c;
}
Beispiel #5
0
static int cliConnect(void) {
    char err[ANET_ERR_LEN];
    int fd;

    fd = anetTcpConnect(err,config.hostip,config.hostport);
    if (fd == ANET_ERR) {
        fprintf(stderr,"Connect: %s\n",err);
        return -1;
    }
    anetTcpNoDelay(NULL,fd);
    return fd;
}
Beispiel #6
0
static int cliConnect(void) {
    char err[ANET_ERR_LEN];
    static int fd = ANET_ERR;

    if (fd == ANET_ERR) {
        fd = anetTcpConnect(err,config.hostip,config.hostport);
        if (fd == ANET_ERR) {
            fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err);
            return -1;
        }
        anetTcpNoDelay(NULL,fd);
    }
    return fd;
}
Beispiel #7
0
redisClient *createClient(int fd) {
    redisClient *c = zmalloc(sizeof(redisClient));
    c->bufpos = 0;

    anetNonBlock(NULL,fd);
    anetTcpNoDelay(NULL,fd);
    if (!c) return NULL;
    if (aeCreateFileEvent(server.el,fd,AE_READABLE,
                          readQueryFromClient, c) == AE_ERR)
    {
#ifdef _WIN32
        closesocket(fd);
#else
        close(fd);
#endif
        zfree(c);
        return NULL;
    }

    selectDb(c,0);
    c->fd = fd;
    c->querybuf = sdsempty();
    c->reqtype = 0;
    c->argc = 0;
    c->argv = NULL;
    c->multibulklen = 0;
    c->bulklen = -1;
    c->sentlen = 0;
    c->flags = 0;
    c->lastinteraction = time(NULL);
    c->authenticated = 0;
    c->replstate = REDIS_REPL_NONE;
    c->reply = listCreate();
    listSetFreeMethod(c->reply,decrRefCount);
    listSetDupMethod(c->reply,dupClientReplyValue);
    c->bpop.keys = NULL;
    c->bpop.count = 0;
    c->bpop.timeout = 0;
    c->bpop.target = NULL;
    c->io_keys = listCreate();
    c->watched_keys = listCreate();
    listSetFreeMethod(c->io_keys,decrRefCount);
    c->pubsub_channels = dictCreate(&setDictType,NULL);
    c->pubsub_patterns = listCreate();
    listSetFreeMethod(c->pubsub_patterns,decrRefCount);
    listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
    listAddNodeTail(server.clients,c);
    initClientMultiState(c);
    return c;
}
Beispiel #8
0
proxy_client*AllocRemote(proxy_client*c)
{
    proxy_client*r=malloc(sizeof(proxy_client));
    r->flags=0;
    int fd=anetTcpNonBlockConnect(anet_error,policy->hosts[0].addr,policy->hosts[0].port);
    if(r==NULL||fd==-1) return NULL;
    LogDebug("connect remote fd:%d",fd);
    anetNonBlock(NULL,fd);
    anetTcpNoDelay(NULL,fd);
    r->fd=fd;
    r->remote=c;
    r->OnError=RemoteDown;
    r->blist=AllocBufferList(3);
    if(aeCreateFileEvent(el,r->fd,AE_READABLE,ReadIncome,r)==AE_ERR){
	//bug:free remote
	close(fd);
	return NULL;
    }
    LogDebug("new peer:%d---%d",r->fd,c->fd);
    return r;
}
Beispiel #9
0
client*tc_create_client(int fd)
{
    client*c=malloc(sizeof(client));
    c->flags=0;
    if(!c) return NULL;
    anetNonBlock(NULL,fd);
    anetTcpNoDelay(NULL,fd);

    c->fd=fd;
    c->blist=tc_create_blist(3);
    c->remote=tc_create_remote(c);
    c->on_local_error=tc_free_client;
    c->on_peer_error=tc_close_after_sent;

    if(c->remote==NULL){
	close(fd);
	free(c);
	return NULL;
    }

    return c;
}
Beispiel #10
0
client*tc_create_remote(int c)
{
    client*r=malloc(sizeof(*r));
    r->flags=0;
    int fd=anetTcpNonBlockConnect(error_msg,peer_addr,peer_port);
    if(r==NULL||fd==-1) return NULL;

    anetNonBlock(NULL,fd);
    anetTcpNoDelay(NULL,fd);

    r->fd=fd;
    r->blist=tc_create_blist(3);
    r->remote=c;
    r->on_local_error=tc_remote_donw;
    r->on_peer_error=NULL;

    if(aeCreateFileEvent(el,r->fd,AE_READABLE,tc_readincome,r)==AE_ERR){
	tc_free_client(r);
	return NULL;
    }

    return r;

}
Beispiel #11
0
httpClient *createClient(aeEventLoop *el, int fd, const char* ip, int port) {
    httpClient *c = malloc(sizeof(*c));
    /* Set the socket to nonblock as the default state set by the kernel is blocking or waiting */
    anetNonBlock(NULL,fd);
    anetTcpNoDelay(NULL,fd);
    c->fd = fd;
    c->rep = replyCreate();
    c->bufpos = 0;
    c->req = requestCreate();
    c->lastinteraction = time(NULL);
    c->ip = strdup(ip);
    c->port = port;
    c->elNode = NULL;
    c->blocked = 0;
    c->el = el;
    c->elNode = listAddNodeTailGetNode(el->clients,c);
    if (aeCreateFileEvent(el, fd, AE_READABLE, c) == AE_ERR)
    {
        printf("create client fail\n");
        freeClient(c);
        return NULL;
    }
    return c;
}