// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
movq %rdx, %r12
movl %esi, %ebx
movq %rdi, %r14
andq $0x0, -0x40(%rbp)
movw $0x2, -0x48(%rbp)
callq 0x2210
movl %eax, -0x44(%rbp)
movl %ebx, %eax
rolw $0x8, %ax
movw %ax, -0x46(%rbp)
pushq $0x2
popq %rdi
pushq $0x1
popq %r13
movl %r13d, %esi
xorl %edx, %edx
callq 0x2080
testl %eax, %eax
js 0x2e19
movl %eax, %r15d
movq %r12, -0x50(%rbp)
movq %r14, -0x38(%rbp)
movl %ebx, -0x2c(%rbp)
pushq $0x3
popq %rsi
movl %eax, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2130
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %r15d, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x2130
movl %r13d, %eax
leaq -0x48(%rbp), %r13
pushq $0x10
popq %r12
movl %eax, %r14d
leal -0x1(%r14), %ebx
movl %r15d, %edi
movq %r13, %rsi
movl %r12d, %edx
callq 0x2190
cmpl $-0x1, %eax
jne 0x2e5b
leaq 0x43d4(%rip), %rax      # 0x71d8
cmpl (%rax), %ebx
jge 0x2fde
pushq $0x1
popq %rdi
callq 0x3077
incl %r14d
jmp 0x2de6
leaq 0x238b(%rip), %rdi      # 0x51ab
leaq 0x24f8(%rip), %rsi      # 0x531f
leaq 0x23a0(%rip), %rdx      # 0x51ce
movl $0x363, %ecx            # imm = 0x363
xorl %eax, %eax
callq 0x2040
leaq 0x267e(%rip), %rdi      # 0x54bf
callq 0x22b0
leaq 0x2835(%rip), %rdi      # 0x5682
callq 0x22b0
pushq $-0x1
popq %r15
jmp 0x3062
xorps %xmm0, %xmm0
leaq -0xd0(%rbp), %r14
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x60(%r14)
movaps %xmm0, 0x50(%r14)
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movq %r14, %rdi
callq 0x26cd
leaq -0x2d0(%rbp), %r12
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x2110
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x50(%rbp), %rdx
movq %r14, %rcx
movq %r12, %r8
callq 0x28ce
movq %r12, %rdi
callq 0x20f0
movl %r15d, %edi
movq %r12, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
leaq -0x4d0(%rbp), %r12
leaq 0x42f3(%rip), %r13      # 0x71dc
pushq $0x1
popq %r14
movl %r15d, -0x30(%rbp)
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x2110
movl $0x200, %edx            # imm = 0x200
movl %r15d, %edi
movq %r12, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x20c0
testl %eax, %eax
jle 0x2fc5
cmpl $0x50545448, -0x4d0(%rbp) # imm = 0x50545448
jne 0x2fc5
movq %r12, %rdi
leaq 0x237b(%rip), %rsi      # 0x52b2
callq 0x2070
testq %rax, %rax
je 0x2f9f
movq %rax, %r14
addq $0x16, %r14
movq %r14, %rdi
leaq 0x23c5(%rip), %rsi      # 0x5317
movq %r14, %rdx
xorl %eax, %eax
callq 0x21f0
movq %r12, %r15
leaq -0xd0(%rbp), %r12
movq %r12, %rdi
callq 0x20f0
movq %rax, %r13
movq %r14, %rdi
callq 0x20f0
movq %r12, %rdi
movq %r15, %r12
movl -0x30(%rbp), %r15d
movl %r13d, %esi
leaq 0x424f(%rip), %r13      # 0x71dc
movq %r14, %rdx
movl %eax, %ecx
callq 0x27c9
testl %eax, %eax
je 0x3062
leaq -0x2d0(%rbp), %r14
movq %r14, %rdi
callq 0x20f0
movl %r15d, %edi
movq %r14, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
pushq $0x1
popq %r14
cmpl (%r13), %ebx
jge 0x3055
incl %ebx
movl %r14d, %edi
callq 0x3077
jmp 0x2ef1
leaq 0x21c6(%rip), %rdi      # 0x51ab
leaq 0x2333(%rip), %rsi      # 0x531f
leaq 0x21db(%rip), %rdx      # 0x51ce
movl $0x377, %ecx            # imm = 0x377
xorl %eax, %eax
callq 0x2040
leaq 0x2250(%rip), %rdi      # 0x5256
movq -0x38(%rbp), %r12
movq %r12, %rsi
movl -0x2c(%rbp), %ebx
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2040
leaq 0x265f(%rip), %rdi      # 0x5682
callq 0x22b0
leaq 0x2256(%rip), %rsi      # 0x5285
leaq -0x2d0(%rbp), %rdi
movq %r12, %rdx
movl %ebx, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x2060
movl %r15d, %edi
callq 0x2340
negl %r14d
movl %r14d, %r15d
jmp 0x3062
movl %r15d, %edi
callq 0x2340
notl %ebx
movl %ebx, %r15d
movl %r15d, %eax
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq