Redis源码分析系列十二:readQueryFromClient

下面我们紧接着研究客户与服务器交互的readQueryFromClient函数。

    redisClient *c = (redisClient*) privdata;
 //指向之前设置的对象指针
 
    int nread;
 int readlen;
 //REDIS_IOBUF_LEN
 
 
    size_t qblen;
 //指示之前已经读的数据

 
 //设置几个变量

 

 
    REDIS_NOTUSED(el); 
    REDIS_NOTUSED(mask);
 //没有任何意义

    server.current_client = c;
 //存储当前的对象
 
 
    readlen = REDIS_IOBUF_LEN;

//每次想读的数据长度

 
    /* If this is a multi bulk request, and we are processing a bulk reply
     * that is large enough, try to maximize the probability that the query
     * buffer contains exactly the SDS string representing the object, even
     * at the risk of requiring more read(2) calls. This way the function
     * processMultiBulkBuffer() can avoid copying buffers to create the
     * Redis Object representing the argument. */
     //自定义检查点: 1 2 3
     //自定义检查点: 1 2 3
     //自定义检查点: 1 2 3

 
    
    if (c->reqtype == REDIS_REQ_MULTIBULK
  && c->multibulklen
  && c->bulklen != -1
        && c->bulklen >= REDIS_MBULK_BIG_ARG
        )
    {
        int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf);

        if (remaining < readlen) readlen = remaining;
    }
 //不需要执行
 

    qblen = sdslen(c->querybuf);

//之前缓冲区里已经存在的数据的长度

 
 
    if (c->querybuf_peak < qblen)
  c->querybuf_peak = qblen;
 //不需要执行
 //自定义检查点: 1 2 3
 
 
 
    c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);

//保证有足够的空间

 
 
    nread = read(fd, c->querybuf+qblen, readlen);
//开始读取数据

 if (nread == -1)
 {
        if (errno == EAGAIN)
  {
            nread = 0;
        }
  else
  {
            redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
            freeClient(c);
            return;
        }
    }
 else if (nread == 0)
 {
        redisLog(REDIS_VERBOSE, "Client closed connection");
        freeClient(c);
        return;
    }

//对读取操作的返回状态进行判断。

困了,睡觉!!!

你可能感兴趣的:(redis,redis,qiangzigege,强子哥哥)