// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: migrateGetSocket
int migrateGetSocket(redisClient *c, robj *host, robj *port, long timeout) {
    int fd;
    sds name = sdsempty();
    migrateCachedSocket *cs;

    /* Check if we have an already cached socket for this ip:port pair. */
    // 根据 ip 和 port 创建地址名字
    name = sdscatlen(name,host->ptr,sdslen(host->ptr));
    name = sdscatlen(name,":",1);
    name = sdscatlen(name,port->ptr,sdslen(port->ptr));
    
    // 在套接字缓存中查找套接字是否已经存在
    cs = dictFetchValue(server.migrate_cached_sockets,name);
    // 缓存存在，更新最后一次使用时间，以免它被当作过期套接字而被释放
    if (cs) {
        sdsfree(name);
        cs->last_use_time = server.unixtime;
        return cs->fd;
    }

    /* No cached socket, create one. */
    // 没有缓存，创建一个新的缓存
    if (dictSize(server.migrate_cached_sockets) == MIGRATE_SOCKET_CACHE_ITEMS) {

        // 如果缓存数已经达到上线，那么在创建套接字之前，先随机删除一个连接

        /* Too many items, drop one at random. */
        dictEntry *de = dictGetRandomKey(server.migrate_cached_sockets);
        cs = dictGetVal(de);
        close(cs->fd);
        zfree(cs);
        dictDelete(server.migrate_cached_sockets,dictGetKey(de));
    }

    /* Create the socket */
    // 创建连接
    fd = anetTcpNonBlockConnect(server.neterr,c->argv[1]->ptr,
                atoi(c->argv[2]->ptr));
    if (fd == -1) {
        sdsfree(name);
        addReplyErrorFormat(c,"Can't connect to target node: %s",
            server.neterr);
        return -1;
    }
    anetEnableTcpNoDelay(server.neterr,fd);

    /* Check if it connects within the specified timeout. */
    // 检查连接的超时设置
    if ((aeWait(fd,AE_WRITABLE,timeout) & AE_WRITABLE) == 0) {
        sdsfree(name);
        addReplySds(c,
            sdsnew("-IOERR error or timeout connecting to the client\r\n"));
        close(fd);
        return -1;
    }

    /* Add to the cache and return it to the caller. */
    // 将连接添加到缓存
    cs = zmalloc(sizeof(*cs));
    cs->fd = fd;
    cs->last_use_time = server.unixtime;
    dictAdd(server.migrate_cached_sockets,name,cs);

    return fd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
callq 0x281b2
movq 0x8(%r12), %rsi
movslq -0x8(%rsi), %rdx
movq %rax, %rdi
callq 0x28380
leaq 0x46118(%rip), %rsi     # 0x9ba37
movl $0x1, %edx
movq %rax, %rdi
callq 0x28380
movq 0x8(%r15), %rsi
movslq -0x8(%rsi), %rdx
movq %rax, %rdi
callq 0x28380
movq %rax, %r15
leaq 0x74662(%rip), %r14     # 0xc9fa8
movq 0x2e8(%r14), %rdi
movq %rax, %rsi
callq 0x22813
testq %rax, %rax
je 0x5597a
movq %rax, %r12
movq %r15, %rdi
callq 0x28204
movq 0x798(%r14), %rax
movq %rax, 0x8(%r12)
movl (%r12), %ebp
jmp 0x55a8c
movq %r13, (%rsp)
movq 0x2e8(%r14), %rdi
movq 0x48(%rdi), %rax
addq 0x28(%rdi), %rax
cmpq $0x40, %rax
jne 0x559c0
callq 0x22a1c
movq %rax, %r12
movq 0x8(%rax), %r13
movl (%r13), %edi
callq 0x1f170
movq %r13, %rdi
callq 0x2984b
movq 0x2e8(%r14), %rdi
movq (%r12), %rsi
callq 0x225d3
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rax
movq 0x8(%rcx), %r13
movq 0x8(%rax), %rdi
callq 0x1f710
movl $0x1e8, %r12d           # imm = 0x1E8
addq 0x705d2(%rip), %r12     # 0xc5fb8
movq %r12, %rdi
movq %r13, %rsi
movl %eax, %edx
callq 0x21766
cmpl $-0x1, %eax
je 0x55a40
movl %eax, %ebp
movq %r12, %rdi
movl %eax, %esi
callq 0x2134b
movl %ebp, %edi
movl $0x2, %esi
movq (%rsp), %rdx
callq 0x21090
testb $0x2, %al
jne 0x55a63
movq %r15, %rdi
callq 0x28204
leaq 0x47666(%rip), %rdi     # 0x9d08d
callq 0x281da
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2d894
movl %ebp, %edi
callq 0x1f170
jmp 0x55a5c
movq %r15, %rdi
callq 0x28204
leaq 0x4761d(%rip), %rsi     # 0x9d06c
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2d9fe
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x55a8c
movl $0x10, %edi
callq 0x29709
movl %ebp, (%rax)
movq 0x798(%r14), %rcx
movq %rcx, 0x8(%rax)
movq 0x2e8(%r14), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x222b6
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq