示例#1
0
client *createClient(int fd) {
    client *c = malloc(sizeof(client));

    anetNonBlock(NULL,fd);
    anetEnableTcpNoDelay(NULL,fd);
    if (server.tcpkeepalive)
        anetKeepAlive(NULL,fd,server.tcpkeepalive);
    if (aeCreateFileEvent(server.el,fd,AE_READABLE,
        readQueryFromClient, c) == AE_ERR)
    {
        close(fd);
        free(c);
        return NULL;
    }

    c->id = server.next_client_id++;
    c->fd = fd;
    c->name = NULL;
    c->bufpos = 0;
    c->querybuf = sdsempty();
    c->querybuf_peak = 0;
    c->reqtype = 0;
    c->argc = 0;
    c->argv = NULL;
    c->lastcmd = NULL;
    c->multibulklen = 0;
    c->bulklen = -1;
    c->sentlen = 0;
    c->flags = 0;
    c->ctime = c->lastinteraction = server.unixtime;
    if (fd != -1) listAddNodeTail(server.clients,c);
    return c;
}
示例#2
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);
        anetEnableTcpNoDelay(NULL,fd);
        if (server.tcpkeepalive)
            anetKeepAlive(NULL,fd,server.tcpkeepalive);
        if (aeCreateFileEvent(server.el,fd,AE_READABLE,
            readQueryFromClient, c) == AE_ERR)
        {
            close(fd);
            zfree(c);
            return NULL;
        }
    }

    selectDb(c,0);
    c->fd = fd;
    c->name = NULL;
    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 = dictCreate(&setDictType,NULL);
    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;
}
示例#3
0
文件: client.c 项目: cinience/saker
ugClient *createClient(int fd) {
    ugClient *c = zmalloc(sizeof(ugClient));
    memset(c,0,sizeof(ugClient));
    if (fd != -1) {
        anetNonBlock(NULL,fd);
        anetEnableTcpNoDelay(NULL,fd);
        if (server.tcpkeepalive)
            anetKeepAlive(NULL, fd, server.tcpkeepalive);
        if (aeCreateFileEvent(server.el, fd, AE_READABLE,
                              readQueryFromClient, c) == AE_ERR) {
#ifdef _WIN32
            aeWinCloseSocket(fd);
#else
            close(fd);
#endif
            zfree(c);
            return NULL;
        }
    }

    if (server.config->password == NULL) {
        c->authenticated = 1;
    }
    c->querybuf = sdsempty();
    c->fd = fd;
    c->ctime = c->lastinteraction = time(NULL);
    c->multibulklen = 0;
    c->bulklen = -1;
    c->reply = listCreate();

    c->reply_bytes = 0;
    c->bufpos = 0;
    c->sentlen = 0;
    /* listSetDupMethod(c->reply, listDupReplyObjects); */
    listSetFreeMethod(c->reply, listFreeReplyObjects);

    c->pubsub_channels = dictCreate(&callbackDict, NULL);
    c->pubsub_patterns = listCreate();
    listSetMatchMethod(c->pubsub_patterns, listMatchPubsubPattern);
    listSetFreeMethod(c->pubsub_patterns, listFreePubsubPattern);
    /* c->pubsub_patterns = listCreate(); */
    if (!server.clients) {
        server.clients = createClientlist();
    }

    listAddNodeTail(server.clients, c);

    return c;
}
示例#4
0
文件: test.c 项目: hiepnm/redislib
testclient* createClient(int fd) {
	testclient* c = zmalloc(sizeof(testclient));
	if (fd != -1) {
		anetNonBlock(NULL, fd);
		anetEnableTcpNoDelay(NULL, fd);
		if (server.tcpkeepalive)
			anetKeepAlive(NULL, fd, server.tcpkeepalive);
		if (aeCreateFileEvent(server.el, fd, AE_READABLE, readRequestFromClient, c) == AE_ERR) {
			close(fd);
			zfree(c);
			return NULL;
		}
	}
	c->cfd = fd;
	c->requestbuf = sdsempty();
	if (fd != -1)
		listAddNodeTail(server.clients, c);

	return c;
}
void tAcceptProc(struct aeEventLoop* eventLoop, int fd, void* clientData, int mask) {
    int cport;
    char cip[SHTTPSVR_IP_STR_LEN];

    int cfd = anetTcpAccept(NULL, fd, cip, sizeof(cip), &cport);

    if (cfd != -1) {
        anetNonBlock(NULL, cfd);
        anetEnableTcpNoDelay(NULL, cfd);
        if (server.tcpkeepalive) {
            anetKeepAlive(NULL, cfd, server.tcpkeepalive);
        }

        client_data_t* c = create_client(cfd);
        if (aeCreateFileEvent(eventLoop, cfd, AE_READABLE,
                              tReadProc, c) == AE_ERR) {
            DBGLOG("aeCreateFileEvent error.", cip, cport);
            free_client(c);
            return;
        }
    }
    DBGLOG("Accepted %s:%d", cip, cport);
    return;
}
示例#6
0
static redis_test_client *redis_test_create_client(int fd)
{
	redis_test_client *c = zmalloc(sizeof(redis_test_client));

    /* passing -1 as fd it is possible to create a non connected client.
     * This is useful since all the 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);
        anetEnableTcpNoDelay(NULL,fd);
        if (server.tcpkeepalive)
            anetKeepAlive(NULL,fd,server.tcpkeepalive);
        if (aeCreateFileEvent(server.el,fd,AE_READABLE,
            redis_test_read_handler, c) == AE_ERR)
        {
            close(fd);
            zfree(c);
            return NULL;
        }
    }

    c->fd = fd;
    c->bufpos = 0;
    c->querybuf = sdsempty();
    c->sentlen = 0;
    c->flags = 0;
    c->ctime = c->lastinteraction = server.unixtime;
    c->reply = listCreate();
    c->reply_bytes = 0;
    c->obuf_soft_limit_reached_time = 0;

	printf("%s %d: fd = %d\n", __FUNCTION__, __LINE__, fd);	
	
    return c;
}
示例#7
0
/* Connect to the server. If force is not zero the connection is performed
 * even if there is already a connected socket. */
static int cliConnect(int force) {
    if (context == NULL || force) {
        if (context != NULL)
            redisFree(context);

        if (config.hostsocket == NULL) {
            context = redisConnect(config.hostip,config.hostport);
        } else {
            context = redisConnectUnix(config.hostsocket);
        }

        if (context->err) {
            fprintf(stderr,"Could not connect to Redis at ");
            if (config.hostsocket == NULL)
                fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
            else
                fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
            redisFree(context);
            context = NULL;
            return REDIS_ERR;
        }

        /* Set aggressive KEEP_ALIVE socket option in the Redis context socket
         * in order to prevent timeouts caused by the execution of long
         * commands. At the same time this improves the detection of real
         * errors. */
        anetKeepAlive(NULL, context->fd, REDIS_CLI_KEEPALIVE_INTERVAL);

        /* Do AUTH and select the right DB. */
        if (cliAuth() != REDIS_OK)
            return REDIS_ERR;
        if (cliSelect() != REDIS_OK)
            return REDIS_ERR;
    }
    return REDIS_OK;
}