// File: /workspace/github_c_makefile_star2_O0/sszllzss[P]webSocket_Libevent_ThreadPool/websocket_common.c
// Function: webSocket_clientLinkToServer(char*, int, char*)
int webSocket_clientLinkToServer(char *ip, int port, char *interface_path)
{
    int ret, fd , timeOut;
    //int i;
    unsigned char loginBuf[512] = {0}, recBuf[512] = {0}, shakeKey[128] = {0}, *p;

    // zhd服务器端网络地址结构体   
    struct sockaddr_in report_addr; 	
    memset(&report_addr,0,sizeof(report_addr)); 			// 数据初始化--清零     
    report_addr.sin_family = AF_INET; 							// 设置为IP通信     
    report_addr.sin_addr.s_addr = inet_addr(ip);			// 服务器IP地址     
    report_addr.sin_port = htons(port); 							// 服务器端口号     

    //create unix socket  
    if((fd = socket(AF_INET,SOCK_STREAM, 0)) < 0) 
    {  
        De_printf("webSocket_login : cannot create socket\r\n");  
        return -1;  
    }
    /* 
    // 测试 -----  创建握手key 和 匹配返回key
    webSocket_buildShakeKey(shakeKey); 
    printf("key1:%s\r\n", shakeKey);
    webSocket_buildRespondShakeKey(shakeKey, strlen(shakeKey), shakeKey);
    printf("key2:%s\r\n", shakeKey);
    */
    //非阻塞
    ret = fcntl(fd , F_GETFL , 0);
    fcntl(fd , F_SETFL , ret | O_NONBLOCK);

    //connect
    timeOut = 0;
    while(connect(fd , (struct sockaddr *)&report_addr,sizeof(struct sockaddr)) == -1)
    {
        if(++timeOut > REPORT_LOGIN_CONNECT_TIMEOUT)
        {
            De_printf("webSocket_login : %s:%d cannot connect !  %d\r\n" , ip, port, timeOut);  
            //
            sprintf((char *)loginBuf, "webSocket_login : %s:%d cannot connect !  %d" , (char *)ip, port, timeOut);
            close(fd); 
            return -timeOut;  
        }
        delayms(1);  //1ms 
    }

    //发送http协议头
    memset(shakeKey, 0, sizeof(shakeKey));
    webSocket_buildShakeKey(shakeKey);  // 创建握手key

    memset(loginBuf, 0, sizeof(loginBuf));  // 创建协议包
    webSocket_buildHttpHead(ip, port, interface_path, shakeKey, (char *)loginBuf);   
    // 发出协议包
    ret = send(fd , loginBuf , strlen((const char*)loginBuf) , MSG_NOSIGNAL);     
    //
    //printf("\r\nconnect time : %d\r\nsend:\n%s\r\n" , timeOut, loginBuf); 

    while(1)
    {
        memset(recBuf , 0 , sizeof(recBuf));
        ret = recv(fd , recBuf , sizeof(recBuf) ,  MSG_NOSIGNAL);
        if(ret > 0)
        {
            if(strncmp((const char *)recBuf, (const char *)"HTTP", strlen((const char *)"HTTP")) == 0)    // 返回的是http回应信息
            {
                //printf("\r\nlogin_recv : %d / %d\r\n%s\r\n" , ret, timeOut, recBuf); 
                if((p = (unsigned char *)strstr((const char *)recBuf, (const char *)"Sec-WebSocket-Accept: ")) != NULL)    // 检查握手信号
                {
                    p += strlen((const char *)"Sec-WebSocket-Accept: ");
                    sscanf((const char *)p, "%s\r\n", p);
                    if(webSocket_matchShakeKey(shakeKey, strlen((const char *)shakeKey), p, strlen((const char *)p)) == 0)  // 握手成功, 发送登录数据包
                        return fd; // 连接成功, 返回连接句柄fd
                    else    // 握手信号不对, 重发协议包
                    {
                        ret = send(fd , loginBuf , strlen((const char*)loginBuf) , MSG_NOSIGNAL);     // 重发协议包
                    }
                }
                else
                {
                    ret = send(fd , loginBuf , strlen((const char*)loginBuf) , MSG_NOSIGNAL);     // 重发协议包
                }
            }/*
                else
                {
            // 显示数据
            if(recBuf[0] >= ' ' && recBuf[0] <= '~')
            { 
            printf("\r\nlogin_recv : %d\r\n%s\r\n" , ret, recBuf); 
            }
            else
            {
            printf("\r\nlogin_recv : %d\r\n" , ret); for(i = 0; i < ret; i++) printf("%.2X ", recBuf[i]); printf("\r\n");
            }
            }*/
        }
        else if(ret <= 0)
        {
            ;
        }
        if(++timeOut > REPORT_LOGIN_RESPOND_TIMEOUT)
        {
            close(fd); 
            return -timeOut;
        }
        delayms(1);  //1ms
    }
    //
    close(fd); 
    return -timeOut;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x510, %rsp            # imm = 0x510
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x230(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2130
leaq -0x430(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2130
leaq -0x4b0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x2130
leaq -0x4c8(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2130
movw $0x2, -0x4c8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2230
movl %eax, -0x4c4(%rbp)
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0x2050
movw %ax, -0x4c6(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2090
movl %eax, -0x28(%rbp)
cmpl $0x0, %eax
jge 0x3700
jmp 0x36b7
leaq 0x1add(%rip), %rdi      # 0x519b
leaq 0x1c7c(%rip), %rsi      # 0x5341
leaq 0x1af2(%rip), %rdx      # 0x51be
movl $0x363, %ecx            # imm = 0x363
movb $0x0, %al
callq 0x2040
leaq 0x1b67(%rip), %rdi      # 0x5246
movb $0x0, %al
callq 0x2040
leaq 0x1c21(%rip), %rdi      # 0x530e
movb $0x0, %al
callq 0x2040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a57
movl -0x28(%rbp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x2150
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %edi
movl -0x24(%rbp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x2150
movl $0x0, -0x2c(%rbp)
movl -0x28(%rbp), %edi
leaq -0x4c8(%rbp), %rsi
movl $0x10, %edx
callq 0x21b0
cmpl $-0x1, %eax
jne 0x37f1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
leaq 0x3a78(%rip), %rcx      # 0x71d8
cmpl (%rcx), %eax
jle 0x37e2
jmp 0x3766
leaq 0x1a2e(%rip), %rdi      # 0x519b
leaq 0x1bcd(%rip), %rsi      # 0x5341
leaq 0x1a43(%rip), %rdx      # 0x51be
movl $0x377, %ecx            # imm = 0x377
movb $0x0, %al
callq 0x2040
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x2c(%rbp), %ecx
leaq 0x1ad7(%rip), %rdi      # 0x526f
movb $0x0, %al
callq 0x2040
leaq 0x1b68(%rip), %rdi      # 0x530e
movb $0x0, %al
callq 0x2040
leaq -0x230(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x2c(%rbp), %r8d
leaq 0x1ad8(%rip), %rsi      # 0x529e
movb $0x0, %al
callq 0x2070
movl -0x28(%rbp), %edi
callq 0x2340
xorl %eax, %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3a57
movl $0x1, %edi
callq 0x3a70
jmp 0x3733
leaq -0x4b0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x2130
leaq -0x4b0(%rbp), %rdi
callq 0x29f0
leaq -0x230(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2130
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
leaq -0x4b0(%rbp), %rcx
leaq -0x230(%rbp), %r8
callq 0x2c60
movl -0x28(%rbp), %eax
movl %eax, -0x4d4(%rbp)
leaq -0x230(%rbp), %rax
movq %rax, -0x4d0(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2100
movl -0x4d4(%rbp), %edi
movq -0x4d0(%rbp), %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2120
movl %eax, -0x24(%rbp)
leaq -0x430(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2130
movl -0x28(%rbp), %edi
leaq -0x430(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movl $0x4000, %ecx           # imm = 0x4000
callq 0x20d0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x3a18
leaq -0x430(%rbp), %rdi
leaq 0x1a03(%rip), %rsi      # 0x52cb
movl $0x4, %edx
callq 0x2110
cmpl $0x0, %eax
jne 0x3a16
leaq -0x430(%rbp), %rdi
leaq 0x19e7(%rip), %rsi      # 0x52d0
callq 0x2080
movq %rax, -0x4b8(%rbp)
cmpq $0x0, %rax
je 0x39d4
movq -0x4b8(%rbp), %rax
addq $0x16, %rax
movq %rax, -0x4b8(%rbp)
movq -0x4b8(%rbp), %rdi
movq -0x4b8(%rbp), %rdx
leaq 0x1a13(%rip), %rsi      # 0x5339
movb $0x0, %al
callq 0x2210
leaq -0x4b0(%rbp), %rax
movq %rax, -0x4f0(%rbp)
leaq -0x4b0(%rbp), %rdi
callq 0x2100
movl %eax, -0x4e4(%rbp)
movq -0x4b8(%rbp), %rax
movq %rax, -0x4e0(%rbp)
movq -0x4b8(%rbp), %rdi
callq 0x2100
movq -0x4f0(%rbp), %rdi
movl -0x4e4(%rbp), %esi
movq -0x4e0(%rbp), %rdx
movl %eax, %ecx
callq 0x2b40
cmpl $0x0, %eax
jne 0x3992
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3a57
movl -0x28(%rbp), %eax
movl %eax, -0x4fc(%rbp)
leaq -0x230(%rbp), %rax
movq %rax, -0x4f8(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2100
movl -0x4fc(%rbp), %edi
movq -0x4f8(%rbp), %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2120
movl %eax, -0x24(%rbp)
jmp 0x3a14
movl -0x28(%rbp), %eax
movl %eax, -0x50c(%rbp)
leaq -0x230(%rbp), %rax
movq %rax, -0x508(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2100
movl -0x50c(%rbp), %edi
movq -0x508(%rbp), %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2120
movl %eax, -0x24(%rbp)
jmp 0x3a16
jmp 0x3a22
cmpl $0x0, -0x24(%rbp)
jg 0x3a20
jmp 0x3a20
jmp 0x3a22
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
leaq 0x37ce(%rip), %rcx      # 0x7200
cmpl (%rcx), %eax
jle 0x3a48
movl -0x28(%rbp), %edi
callq 0x2340
xorl %eax, %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3a57
movl $0x1, %edi
callq 0x3a70
jmp 0x3881
movl -0x4(%rbp), %eax
addq $0x510, %rsp            # imm = 0x510
popq %rbp
retq
nopw %cs:(%rax,%rax)