示例#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
static void check_connect(aeEventLoop *el, int fd, void *data, int mask)
{
    int err = 0;
    socklen_t optlen;
    char errstr[ANET_ERR_LEN];

    aeDeleteFileEvent(el, fd, AE_WRITABLE);

    optlen = sizeof(err);
    if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &optlen) == -1) {
        Log(WARNING, "getsockopt: %s", strerror(errno));
        close(fd);
        retry_connect();
        return;
    }

    if (err != 0) {
        Log(WARNING, "data server disconnected: %s", strerror(err));
        close(fd);
        retry_connect();
        return;
    }

    if (anetEnableTcpNoDelay(errstr, fd) == ANET_ERR) {
        Log(WARNING, "anetEnableTcpNoDelay: %s", errstr);
    }

    data_stream = sock_init(fd, BUFSIZE, 0);

    Log(NOTICE, "connected to data stream");
}
示例#3
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;
}
示例#4
0
void acceptCommonHandler( aeServer* serv ,int fd,char* client_ip,int client_port, int flags)
{
	if( serv->connectNum >= serv->maxConnect )
	{
		printf( "connect num over limit \n");
		close( fd );
		return;
	}
	
	if( fd <= 0 )
	{
		printf( "error fd is null\n");
		close(fd );
		return;
	}
	
	serv->connlist[fd].client_ip = client_ip;
	serv->connlist[fd].client_port = client_port;
	serv->connlist[fd].flags |= flags;
	serv->connlist[fd].fd = fd;
	serv->connlist[fd].disable = 0;
	serv->connlist[fd].send_buffer = sdsempty();
	
	int reactor_id = fd % serv->reactorNum;
	int worker_id  = fd % serv->workerNum;

    if (fd != -1)
	{
        anetNonBlock(NULL,fd);
        anetEnableTcpNoDelay(NULL,fd);

		aeEventLoop* el = serv->reactorThreads[reactor_id].reactor.eventLoop;
        if (aeCreateFileEvent( el ,fd,AE_READABLE, 
				onClientReadable, &fd ) == AE_ERR )
        {
            printf( "CreateFileEvent read error fd =%d,errno=%d,errstr=%s  \n" ,fd  , errno, strerror( errno )  );
            close(fd);
        }
	
    	aePipeData  data = {0};
    	data.type = PIPE_EVENT_CONNECT;
    	data.connfd = fd;
    	data.len = 0;
		serv->connectNum += 1;	
		setPipeWritable( el , NULL , worker_id );

		int sendlen = PIPE_DATA_HEADER_LENG;	
		pthread_mutex_lock( &servG->workers[worker_id].w_mutex );
    	servG->workers[worker_id].send_buffer = sdscatlen( servG->workers[worker_id].send_buffer , &data, sendlen );
    	pthread_mutex_unlock( &servG->workers[worker_id].w_mutex );
	}	
}
示例#5
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;
}
示例#6
0
文件: client.c 项目: ChellsChen/wind
vuiClient * createClient(int fd)
{
    if (server.client != NULL)
    {
        close(fd);
        return NULL;
    }
    LogInfo("creat client fd:%d", fd);
    vuiClient *c = zmalloc(sizeof(vuiClient));
    memset(c, 0, sizeof(vuiClient));

    c->querybuf = sdsempty();
    c->querymsg = sdsempty();

    c->prot.method = NULL;
    c->prot.version = NULL;
    c->prot.body = NULL;
    c->prot.lenght = 0;
    c->prot.waiting = 0;

    c->res.version = "VPC/1.0";
    c->res.code = 200;
    c->res.reason = sdsnew("OK");
    c->res.body = sdsempty();
    c->res.buf = sdsempty();

    c->jsons = listCreate();

    c->fd = fd;


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

}
示例#7
0
//接受新连接
void AcceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask){
	int cfd, cport;
	char ip_addr[128] = { 0 };
	cfd = anetTcpAccept(g_err_string, fd, ip_addr, strlen(ip_addr), &cport);
	printf("Connected from %s:%d\n", ip_addr, cport);

	if(anetNonBlock(g_err_string, cfd) == ANET_ERR){
		fprintf(stderr, "set nonblock error: %d\n", fd);
	}
	if(anetEnableTcpNoDelay(g_err_string,fd) == ANET_ERR){
		fprintf(stderr, "set nodelay error: %d\n", fd);
	}
	
	if( aeCreateFileEvent(el, cfd, AE_READABLE, 
	ReadFromClient, NULL) == AE_ERR ){
		fprintf(stderr, "client connect fail: %d\n", fd);
		close(fd);
	}
}
示例#8
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;
}
示例#9
0
文件: slots.c 项目: 107192468/codis
static int
slotsmgrt_get_socket(redisClient *c, sds host, sds port, int timeout) {
    sds name = sdsempty();
    name = sdscatlen(name, host, sdslen(host));
    name = sdscatlen(name, ":", 1);
    name = sdscatlen(name, port, sdslen(port));

    slotsmgrt_sockfd *pfd = dictFetchValue(server.slotsmgrt_cached_sockfds, name);
    if (pfd != NULL) {
        sdsfree(name);
        pfd->lasttime = server.unixtime;
        return pfd->fd;
    }
    
    int fd = anetTcpNonBlockConnect(server.neterr, host, atoi(port));
    if (fd == -1) {
        redisLog(REDIS_WARNING, "slotsmgrt: connect to target %s:%s, error = '%s'",
                host, port, server.neterr);
        sdsfree(name);
        addReplyErrorFormat(c,"Can't connect to target node: %s", server.neterr);
        return -1;
    }
    anetEnableTcpNoDelay(server.neterr, fd);
    if ((aeWait(fd, AE_WRITABLE, timeout) & AE_WRITABLE) == 0) {
        redisLog(REDIS_WARNING, "slotsmgrt: connect to target %s:%s, aewait error = '%s'",
                host, port, server.neterr);
        sdsfree(name);
        close(fd);
        addReplySds(c, sdsnew("-IOERR error or timeout connecting to the client\r\n"));
        return -1;
    }
    redisLog(REDIS_WARNING, "slotsmgrt: connect to target %s:%s", host, port);

    pfd = zmalloc(sizeof(*pfd));
    pfd->fd = fd;
    pfd->lasttime = server.unixtime;
    dictAdd(server.slotsmgrt_cached_sockfds, name, pfd);
    return fd;
}
示例#10
0
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;
}
示例#11
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;
}
示例#12
0
void acceptCommonHandler( appnetServer *serv , int connfd , char *client_ip ,
		int client_port )
{
	
	if (serv->connect_num >= serv->connect_max)
	{
		printf( "connect num over limit \n" );
		close( connfd );
		return;
	}
	
	if (connfd <= 0)
	{
		printf( "error fd is null\n" );
		close( connfd );
		return;
	}
	
	int reactor_id = connfd % serv->reactor_num;
	int worker_id = connfd % serv->worker_num;
	
	serv->connlist[connfd].client_ip = client_ip;
	serv->connlist[connfd].client_port = client_port;
	serv->connlist[connfd].connfd = connfd;
	serv->connlist[connfd].disable = 0;
	serv->connlist[connfd].send_buffer = sdsempty();
	serv->connlist[connfd].recv_buffer = sdsempty();
	serv->connlist[connfd].proto_type = 0;
	serv->connlist[connfd].worker_id = worker_id;
	serv->connlist[connfd].reactor_id = reactor_id;
	
	if (connfd != -1)
	{
		anetNonBlock( NULL , connfd );
		anetEnableTcpNoDelay( NULL , connfd );
		
		aeEventLoop *el = serv->reactor_threads[reactor_id].reactor.event_loop;
		if (aeCreateFileEvent( el , connfd , AE_READABLE , onClientReadable , &connfd ) ==
				AE_ERR)
		{
			
			printf( "CreateFileEvent read error fd =%d,errno=%d,errstr=%s  \n" , connfd ,
					errno , strerror( errno ) );
			
			close( connfd );
		}
		
		appnetPipeData data =
				{0};
		data.type = PIPE_EVENT_CONNECT;
		data.connfd = connfd;
		data.len = 0;
		serv->connect_num += 1;
		
		int index = worker_id * servG->reactor_num + reactor_id;
		
		int ret = aeCreateFileEvent( el , servG->worker_pipes[index].pipefd[0] ,
				AE_WRITABLE , onMasterPipeWritable , worker_id );
		
		if (ret == AE_ERR)
		printf( "Accept setPipeWritable Error \n" );
		
		int sendlen = PIPE_DATA_HEADER_LENG;
		
		pthread_mutex_lock( &servG->workers[worker_id].w_mutex );
		
		/* append connect event message */
		servG->workers[worker_id].send_buffer =
				sdscatlen( servG->workers[worker_id].send_buffer , &data , sendlen );
		
		pthread_mutex_unlock( &servG->workers[worker_id].w_mutex );
	}
}