// File: /workspace/github_c_makefile_star2_O3/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, %r12d          # imm = 0x10D0
movl -0x8(%rax), %r13d
cmpl $0x7, %r13d
jg 0x50b05
movl $0x8, %eax
subl %r13d, %eax
jmp 0x50b37
cmpl $0x8, %r13d
jne 0x50b28
cmpl $0x626d4352, (%rax)     # imm = 0x626D4352
jne 0x50bbd
movl 0x4(%rax), %eax
bswapl %eax
cmpl $0x89f, %eax            # imm = 0x89F
ja 0x50b2d
jmp 0x50bbd
movl 0x4(%rax), %eax
bswapl %eax
subl %r13d, %eax
cmpl %r12d, %eax
cmovael %r12d, %eax
movl %eax, %edx
movl %ebp, %edi
movq %r14, %rsi
callq 0x1f330
cmpq $-0x1, %rax
je 0x50b9e
movq %rax, %r15
testq %rax, %rax
jle 0x50baa
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x28689
movq %rax, 0x18(%rbx)
addl %r15d, %r13d
cmpl $0x8, %r13d
jl 0x50af1
movl 0x4(%rax), %ecx
bswapl %ecx
cmpl %r13d, %ecx
jne 0x50af1
movq %rbx, %rdi
callq 0x51c70
testl %eax, %eax
je 0x50bf1
movq 0x18(%rbx), %rdi
callq 0x2850d
callq 0x2845e
movq %rax, 0x18(%rbx)
jmp 0x50af1
callq 0x1f810
cmpl $0xb, (%rax)
jne 0x50bb1
jmp 0x50bf1
je 0x50bd2
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
jmp 0x50bd9
leaq 0x4b993(%rip), %rsi     # 0x9c557
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
jmp 0x50be9
leaq 0x4b9de(%rip), %rdx     # 0x9c5b7
leaq 0x4b9b2(%rip), %rsi     # 0x9c592
xorl %edi, %edi
xorl %eax, %eax
callq 0x23223
movq %rbx, %rdi
callq 0x50a5c
addq $0x10d8, %rsp           # imm = 0x10D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq