// File: /workspace/github_c_makefile_star2_O1/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 $0x4c8, %rsp            # imm = 0x4C8
movq %rdx, %r13
movl %esi, %r15d
movq %rdi, %r12
leaq -0x2f0(%rbp), %rdi
xorl %ebx, %ebx
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x2120
leaq -0x4f0(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x2120
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x50(%rbp)
movw $0x2, -0x50(%rbp)
movq %r12, %rdi
callq 0x2220
movl %eax, -0x4c(%rbp)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, -0x4e(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2080
testl %eax, %eax
js 0x3236
movl %eax, %r14d
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2140
orl $0x800, %eax            # imm = 0x800
movl %r14d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2140
leaq -0x50(%rbp), %rsi
movl %r14d, %edi
movl $0x10, %edx
callq 0x21a0
cmpl $-0x1, %eax
movl %r15d, -0x38(%rbp)
movq %r12, -0x68(%rbp)
jne 0x3060
movq %r13, -0x30(%rbp)
movl $0x1, %ebx
leaq 0x41d2(%rip), %r13      # 0x71e0
leaq -0x60(%rbp), %r15
leaq -0x50(%rbp), %r12
leal -0x1(%rbx), %eax
cmpl (%r13), %eax
jge 0x3277
movq $0x0, -0x60(%rbp)
movq $0x3e8, -0x58(%rbp)     # imm = 0x3E8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x21e0
movl %r14d, %edi
movq %r12, %rsi
movl $0x10, %edx
callq 0x21a0
incl %ebx
cmpl $-0x1, %eax
je 0x3016
decl %ebx
movq -0x30(%rbp), %r13
xorps %xmm0, %xmm0
movl %r14d, -0x30(%rbp)
leaq -0xf0(%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)
leaq -0x60(%rbp), %r12
movaps %xmm0, (%r12)
movq %r12, %rdi
movl $0x10, %esi
callq 0x27e4
movq %r12, %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x25db
leaq -0x2f0(%rbp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x2120
leaq 0x1fc8(%rip), %rsi      # 0x50a0
movq %r15, %rdi
movq %r13, %rdx
movq -0x68(%rbp), %rcx
movl -0x38(%rbp), %r8d
movq %r14, %r9
movl -0x30(%rbp), %r14d
xorl %eax, %eax
callq 0x2060
movq %r15, %rdi
callq 0x20f0
movl %r14d, %edi
movq %r15, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2110
leaq -0x4f0(%rbp), %r13
leaq 0x40c7(%rip), %r15      # 0x71e4
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x2120
movl $0x200, %edx            # imm = 0x200
movl %r14d, %edi
movq %r13, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x20c0
testl %eax, %eax
jle 0x31fb
cmpl $0x50545448, -0x4f0(%rbp) # imm = 0x50545448
jne 0x31fb
movq %r13, %rdi
leaq 0x214f(%rip), %rsi      # 0x52b2
callq 0x2070
testq %rax, %rax
je 0x31d2
movq %rax, %r15
addq $0x16, %r15
movq %r15, %rdi
leaq 0x219d(%rip), %rsi      # 0x531b
movq %r15, %rdx
xorl %eax, %eax
callq 0x2200
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
callq 0x20f0
movq %rbx, -0x38(%rbp)
movq %r13, %rbx
movq %r12, %r13
movq %rax, %r12
movq %r15, %rdi
callq 0x20f0
movq %r14, %rdi
movl -0x30(%rbp), %r14d
movl %r12d, %esi
movq %r13, %r12
movq %rbx, %r13
movq -0x38(%rbp), %rbx
movq %r15, %rdx
movl %eax, %ecx
callq 0x2940
testl %eax, %eax
je 0x32ed
leaq -0x2f0(%rbp), %r15
movq %r15, %rdi
callq 0x20f0
movl %r14d, %edi
movq %r15, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2110
leaq 0x3fe9(%rip), %r15      # 0x71e4
cmpl (%r15), %ebx
jge 0x3227
incl %ebx
movq $0x0, -0x60(%rbp)
movq $0x3e8, -0x58(%rbp)     # imm = 0x3E8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x21e0
jmp 0x311d
movl %r14d, %edi
callq 0x2350
notl %ebx
jmp 0x32ea
leaq 0x1f6e(%rip), %rdi      # 0x51ab
leaq 0x20df(%rip), %rsi      # 0x5323
leaq 0x1f83(%rip), %rdx      # 0x51ce
movl $0x363, %ecx            # imm = 0x363
xorl %eax, %eax
callq 0x2040
leaq 0x2265(%rip), %rdi      # 0x54c3
callq 0x22c0
leaq 0x241c(%rip), %rdi      # 0x5686
callq 0x22c0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x32ed
leaq 0x1f2d(%rip), %rdi      # 0x51ab
leaq 0x209e(%rip), %rsi      # 0x5323
leaq 0x1f42(%rip), %rdx      # 0x51ce
movl $0x377, %ecx            # imm = 0x377
xorl %eax, %eax
callq 0x2040
leaq 0x1fb7(%rip), %rdi      # 0x5256
movq -0x68(%rbp), %r12
movq %r12, %rsi
movl -0x38(%rbp), %r15d
movl %r15d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2040
leaq 0x23c9(%rip), %rdi      # 0x5686
callq 0x22c0
leaq 0x1fbc(%rip), %rsi      # 0x5285
leaq -0x2f0(%rbp), %rdi
movq %r12, %rdx
movl %r15d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x2060
movl %r14d, %edi
callq 0x2350
negl %ebx
movl %ebx, %r14d
movl %r14d, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq