// File: /workspace/github_c_makefile_star2_O1/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 0x9d076(%rip), %r12     # 0xc9fa8
movq %rdx, 0x1d0(%r12)
movl $0x4000, %r14d          # imm = 0x4000
cmpl $0x2, 0x50(%rdx)
jne 0x2cf71
cmpl $0x0, 0x54(%rbx)
je 0x2cf71
movq 0x58(%rbx), %rax
cmpq $0x8000, %rax           # imm = 0x8000
jl 0x2cf71
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 0x2cf83
movq %r15, 0x28(%rbx)
movq %r14, %rsi
callq 0x2823a
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 0x2d068
cmpl $-0x1, %r14d
jne 0x2cfc4
callq 0x1f810
movl (%rax), %edi
xorl %r14d, %r14d
cmpl $0xb, %edi
jne 0x2d07d
testl %r14d, %r14d
je 0x2d0b0
movq 0x20(%rbx), %rdi
movl %r14d, %esi
callq 0x282c0
movq 0x798(%r12), %rax
movq %rax, 0x80(%rbx)
testb $0x2, 0x90(%rbx)
je 0x2cffb
movslq %r14d, %rax
addq %rax, 0xb8(%rbx)
movq 0x20(%rbx), %rax
movslq -0x8(%rax), %rax
cmpq %rax, 0x478(%r12)
jae 0x2d0a8
callq 0x281b2
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2ee21
movq %rax, %r14
callq 0x281b2
movq 0x20(%rbx), %rsi
movl $0x40, %edx
movq %rax, %rdi
callq 0x28f45
movq %rax, %r15
leaq 0x69f99(%rip), %rsi     # 0x96fdd
movl $0x3, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23064
movq %r14, %rdi
callq 0x28204
movq %r15, %rdi
callq 0x28204
jmp 0x2d098
leaq 0x69f55(%rip), %rsi     # 0x96fc4
movl $0x1, %edi
xorl %eax, %eax
callq 0x23064
jmp 0x2d098
callq 0x1f630
leaq 0x69f23(%rip), %rsi     # 0x96fac
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2e287
movq %rbx, %rdi
callq 0x2ece8
movq $0x0, 0x1d0(%r12)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq