// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/networking.c
// Function: readQueryFromClient
void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
    redisClient *c = (redisClient*) privdata;
    int nread, readlen;
    size_t qblen;
    REDIS_NOTUSED(el);
    REDIS_NOTUSED(mask);

    // 设置服务器的当前客户端
    server.current_client = c;
    
    // 读入长度（默认为 16 MB）
    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. */
    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;
    }

    // 获取查询缓冲区当前内容的长度
    // 如果读取出现 short read ，那么可能会有内容滞留在读取缓冲区里面
    // 这些滞留内容也许不能完整构成一个符合协议的命令，
    qblen = sdslen(c->querybuf);
    // 如果有需要，更新缓冲区内容长度的峰值（peak）
    if (c->querybuf_peak < qblen) c->querybuf_peak = qblen;
    // 为查询缓冲区分配空间
    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;
        }
    // 遇到 EOF
    } else if (nread == 0) {
        redisLog(REDIS_VERBOSE, "Client closed connection");
        freeClient(c);
        return;
    }

    if (nread) {
        // 根据内容，更新查询缓冲区（SDS） free 和 len 属性
        // 并将 '\0' 正确地放到内容的最后
        sdsIncrLen(c->querybuf,nread);
        // 记录服务器和客户端最后一次互动的时间
        c->lastinteraction = server.unixtime;
        // 如果客户端是 master 的话，更新它的复制偏移量
        if (c->flags & REDIS_MASTER) c->reploff += nread;
    } else {
        // 在 nread == -1 且 errno == EAGAIN 时运行
        server.current_client = NULL;
        return;
    }

    // 查询缓冲区长度超出服务器最大缓冲区长度
    // 清空缓冲区并释放客户端
    if (sdslen(c->querybuf) > server.client_max_querybuf_len) {
        sds ci = catClientInfoString(sdsempty(),c), bytes = sdsempty();

        bytes = sdscatrepr(bytes,c->querybuf,64);
        redisLog(REDIS_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci, bytes);
        sdsfree(ci);
        sdsfree(bytes);
        freeClient(c);
        return;
    }

    // 从查询缓存重读取内容，创建参数，并执行命令
    // 函数会执行到缓存中的所有内容都被处理完为止
    processInputBuffer(c);

    server.current_client = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdx, %rbx
movl %esi, %ebp
leaq 0x9cdd6(%rip), %r12     # 0xc9fa8
movq %rdx, 0x1d0(%r12)
movl $0x4000, %r14d          # imm = 0x4000
cmpl $0x2, 0x50(%rdx)
jne 0x2d211
cmpl $0x0, 0x54(%rbx)
je 0x2d211
movq 0x58(%rbx), %rax
cmpq $0x8000, %rax           # imm = 0x8000
jl 0x2d211
movq 0x20(%rbx), %rcx
movl -0x8(%rcx), %ecx
subq %rcx, %rax
addq $0x2, %rax
cmpl $0x4000, %eax           # imm = 0x4000
cltq
cmovlq %rax, %r14
movq 0x20(%rbx), %rdi
movslq -0x8(%rdi), %r15
cmpq %r15, 0x28(%rbx)
jae 0x2d223
movq %r15, 0x28(%rbx)
movq %r14, %rsi
callq 0x28543
movq %rax, 0x20(%rbx)
addq %rax, %r15
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1f330
movq %rax, %r14
testl %r14d, %r14d
je 0x2d27d
cmpl $-0x1, %r14d
jne 0x2d295
callq 0x1f810
movl (%rax), %edi
cmpl $0xb, %edi
je 0x2d342
callq 0x1f630
leaq 0x69da3(%rip), %rsi     # 0x9700c
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
jmp 0x2d32a
leaq 0x69da0(%rip), %rsi     # 0x97024
movl $0x1, %edi
xorl %eax, %eax
callq 0x23223
jmp 0x2d32a
movq 0x20(%rbx), %rdi
movl %r14d, %esi
callq 0x285c9
movq 0x798(%r12), %rax
movq %rax, 0x80(%rbx)
testb $0x2, 0x90(%rbx)
je 0x2d2c3
movslq %r14d, %rax
addq %rax, 0xb8(%rbx)
movq 0x20(%rbx), %rax
movslq -0x8(%rax), %rax
cmpq %rax, 0x478(%r12)
jae 0x2d33a
callq 0x2845e
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2f0da
movq %rax, %r14
callq 0x2845e
movq 0x20(%rbx), %rsi
movl $0x40, %edx
movq %rax, %rdi
callq 0x29259
movq %rax, %r15
leaq 0x69d35(%rip), %rsi     # 0x9703d
movl $0x3, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23223
movq %r14, %rdi
callq 0x2850d
movq %r15, %rdi
callq 0x2850d
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2e517
movq %rbx, %rdi
callq 0x2ef92
movq $0x0, 0x1d0(%r12)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq