// File: /workspace/github_c_makefile_star2_O1/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 %rsp, %r14
movl $0x10d0, %r12d          # imm = 0x10D0
movq 0x18(%rbx), %rax
movl -0x8(%rax), %r13d
cmpl $0x7, %r13d
jg 0x510bd
movl $0x8, %eax
subl %r13d, %eax
jmp 0x510ef
cmpl $0x8, %r13d
jne 0x510e0
cmpl $0x626d4352, (%rax)     # imm = 0x626D4352
jne 0x5117c
movl 0x4(%rax), %ecx
bswapl %ecx
cmpl $0x89f, %ecx            # imm = 0x89F
jbe 0x5117c
movl 0x4(%rax), %eax
bswapl %eax
subl %r13d, %eax
cmpl %r12d, %eax
cmovael %r12d, %eax
movslq %eax, %rdx
movl %ebp, %edi
movq %r14, %rsi
callq 0x1f330
movq %rax, %r15
cmpq $-0x1, %rax
jne 0x51113
callq 0x1f810
cmpl $0xb, (%rax)
je 0x511b0
testq %r15, %r15
jle 0x51169
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x28380
movq %rax, 0x18(%rbx)
addl %r15d, %r13d
cmpl $0x8, %r13d
jl 0x510a5
movl 0x4(%rax), %eax
bswapl %eax
cmpl %r13d, %eax
jne 0x510a5
movq %rbx, %rdi
callq 0x521f2
testl %eax, %eax
je 0x511b0
movq 0x18(%rbx), %rdi
callq 0x28204
callq 0x281b2
movq %rax, 0x18(%rbx)
jmp 0x510a5
je 0x51191
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
jmp 0x51198
leaq 0x4b394(%rip), %rsi     # 0x9c517
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
jmp 0x511a8
leaq 0x4b3df(%rip), %rdx     # 0x9c577
leaq 0x4b3b3(%rip), %rsi     # 0x9c552
xorl %edi, %edi
xorl %eax, %eax
callq 0x23064
movq %rbx, %rdi
callq 0x51014
addq $0x10d8, %rsp           # imm = 0x10D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq