redis networking通信协议的源码分析

        networking的代码主要是针对client的命令进行处理,主要是实现三个功能:client连接的管理;解析client的请求;发送回复内容给client。

 一、client连接的管理

       进行连接的接收,并创建client结构体,完成内部属性的初始化,事件回调函数的设置。

void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    ……
    while(max--) {
        cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
        if (cfd == ANET_ERR) {
            ……
            return;
        }
        serverLog(LL_VERBOSE,"Accepted %s:%d", cip, cport);
        acceptCommonHandler(cfd,0,cip);
    }
}
static void acceptCommonHandler(int fd, int flags, char *ip) {
    client *c;//连接建立创建client结构体
    if ((c = createClient(fd)) == NULL) {
        close(fd); /* May be already closed, just ignore errors */
        return;
    }
    //client连接个数超过server.maxclients,将被拒绝,释放client
    if (listLength(server.clients) > server.maxclients) {
        ……
        server.stat_rejected_conn++;
        freeClient(c);
        return;
    }
    ……
    server.stat_numconnections++;
    c->flags |= flags;
}
client *createClient(int fd) {
    client *c = zmalloc(sizeof(client));
    //设置socket fd的选项和可读事件回调函数readQueryFromClient
    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)
        {
            ……
        }
    }
    selectDb(c,0);
    ……//初始化client内部属性
    listSetFreeMethod(c->pubsub_patterns,decrRefCountVoid);
    listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
    if (fd != -1) listAddNodeTail(server.clients,c);
    initClientMultiState(c);
    return c;
}

二、解析client的请求

        在统一的请求协议中, 所有发送至 Redis 服务器的参数都是二进制安全(binary safe)的。

协议的一般格式如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值