// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: clusterReadHandler
void clusterReadHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    char buf[sizeof(clusterMsg)];
    ssize_t nread;
    clusterMsg *hdr;
    clusterLink *link = (clusterLink*) privdata;
    int readlen, rcvbuflen;
    REDIS_NOTUSED(el);
    REDIS_NOTUSED(mask);

    // 尽可能地多读数据
    while(1) { /* Read as long as there is data to read. */

        // 检查输入缓冲区的长度
        rcvbuflen = sdslen(link->rcvbuf);
        // 头信息（8 字节）未读入完
        if (rcvbuflen < 8) {
            /* First, obtain the first 8 bytes to get the full message
             * length. */
            readlen = 8 - rcvbuflen;
        // 已读入完整的信息
        } else {
            /* Finally read the full message. */
            hdr = (clusterMsg*) link->rcvbuf;
            if (rcvbuflen == 8) {
                /* Perform some sanity check on the message signature
                 * and length. */
                if (memcmp(hdr->sig,"RCmb",4) != 0 ||
                    ntohl(hdr->totlen) < CLUSTERMSG_MIN_LEN)
                {
                    redisLog(REDIS_WARNING,
                        "Bad message length or signature received "
                        "from Cluster bus.");
                    handleLinkIOError(link);
                    return;
                }
            }
            // 记录已读入内容长度
            readlen = ntohl(hdr->totlen) - rcvbuflen;
            if (readlen > sizeof(buf)) readlen = sizeof(buf);
        }

        // 读入内容
        nread = read(fd,buf,readlen);

        // 没有内容可读
        if (nread == -1 && errno == EAGAIN) return; /* No more data ready. */

        // 处理读入错误
        if (nread <= 0) {
            /* I/O error... */
            redisLog(REDIS_DEBUG,"I/O error reading from node link: %s",
                (nread == 0) ? "connection closed" : strerror(errno));
            handleLinkIOError(link);
            return;
        } else {
            /* Read data and recast the pointer to the new buffer. */
            // 将读入的内容追加进输入缓冲区里面
            link->rcvbuf = sdscatlen(link->rcvbuf,buf,nread);
            hdr = (clusterMsg*) link->rcvbuf;
            rcvbuflen += nread;
        }

        /* Total length obtained? Process this packet. */
        // 检查已读入内容的长度，看是否整条信息已经被读入了
        if (rcvbuflen >= 8 && rcvbuflen == ntohl(hdr->totlen)) {
            // 如果是的话，执行处理信息的函数
            if (clusterProcessPacket(link)) {
                sdsfree(link->rcvbuf);
                link->rcvbuf = sdsempty();
            } else {
                return; /* Link no longer valid. */
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10d8, %rsp           # imm = 0x10D8
movq %rdx, %rbx
movl %esi, %ebp
movq 0x18(%rdx), %rax
movq %rsp, %r14
movl $0x10d0, %r13d          # imm = 0x10D0
movl -0x8(%rax), %r12d
cmpl $0x7, %r12d
jg 0x4ca1b
pushq $0x8
popq %rax
subl %r12d, %eax
jmp 0x4ca4d
cmpl $0x8, %r12d
jne 0x4ca3e
cmpl $0x626d4352, (%rax)     # imm = 0x626D4352
jne 0x4cad3
movl 0x4(%rax), %eax
bswapl %eax
cmpl $0x89f, %eax            # imm = 0x89F
ja 0x4ca43
jmp 0x4cad3
movl 0x4(%rax), %eax
bswapl %eax
subl %r12d, %eax
cmpl %r13d, %eax
cmovael %r13d, %eax
movl %eax, %edx
movl %ebp, %edi
movq %r14, %rsi
callq 0x1f330
cmpq $-0x1, %rax
je 0x4cab4
movq %rax, %r15
testq %rax, %rax
jle 0x4cac0
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2736d
movq %rax, 0x18(%rbx)
addl %r15d, %r12d
cmpl $0x8, %r12d
jl 0x4ca09
movl 0x4(%rax), %ecx
bswapl %ecx
cmpl %r12d, %ecx
jne 0x4ca09
movq %rbx, %rdi
callq 0x4d9f4
testl %eax, %eax
je 0x4cb05
movq 0x18(%rbx), %rdi
callq 0x271f7
callq 0x271bf
movq %rax, 0x18(%rbx)
jmp 0x4ca09
callq 0x1f810
cmpl $0xb, (%rax)
jne 0x4cac7
jmp 0x4cb05
je 0x4cae6
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
jmp 0x4caed
leaq 0x45a3d(%rip), %rsi     # 0x92517
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
jmp 0x4cafd
leaq 0x45a8a(%rip), %rdx     # 0x92577
leaq 0x45a5e(%rip), %rsi     # 0x92552
xorl %edi, %edi
xorl %eax, %eax
callq 0x22613
movq %rbx, %rdi
callq 0x4c97b
addq $0x10d8, %rsp           # imm = 0x10D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq