// File: /workspace/github_c_makefile_star2_O3/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 $0x4b8, %rsp            # imm = 0x4B8
movq %rdx, %r15
movl %esi, %r13d
movq %rdi, %r12
movq $0x0, -0x48(%rbp)
movw $0x2, -0x50(%rbp)
callq 0x2210
movl %eax, -0x4c(%rbp)
movl %r13d, %eax
rolw $0x8, %ax
movw %ax, -0x4e(%rbp)
xorl %ebx, %ebx
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2080
testl %eax, %eax
js 0x31e6
movl %eax, %r14d
movq %r15, -0x38(%rbp)
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2130
orl $0x800, %eax            # imm = 0x800
movl %r14d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2130
leaq -0x50(%rbp), %rsi
movl %r14d, %edi
movl $0x10, %edx
callq 0x2190
cmpl $-0x1, %eax
jne 0x300a
movq %r12, -0x40(%rbp)
movl %r13d, -0x2c(%rbp)
movl $0x1, %ebx
leaq 0x422d(%rip), %r12      # 0x71d8
leaq -0x2e0(%rbp), %r15
leaq -0x50(%rbp), %r13
leal -0x1(%rbx), %eax
cmpl (%r12), %eax
jge 0x3227
movq $0x0, -0x2e0(%rbp)
movq $0x3e8, -0x2d8(%rbp) # imm = 0x3E8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x21d0
movl %r14d, %edi
movq %r13, %rsi
movl $0x10, %edx
callq 0x2190
incl %ebx
cmpl $-0x1, %eax
je 0x2fb6
decl %ebx
movl -0x2c(%rbp), %r13d
movq -0x40(%rbp), %r12
xorps %xmm0, %xmm0
movl %r14d, -0x2c(%rbp)
leaq -0xe0(%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 -0x2e0(%rbp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movl $0x10, %esi
callq 0x27e6
movq %r15, %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x25c2
movl %r13d, %r15d
leaq -0x2e0(%rbp), %r13
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x2110
leaq 0x2019(%rip), %rsi      # 0x50a0
movq %r13, %rdi
movq -0x38(%rbp), %rdx
movq %r12, %rcx
movl %r15d, %r8d
movq %r14, %r9
movl -0x2c(%rbp), %r14d
xorl %eax, %eax
callq 0x2060
movq %r13, %rdi
callq 0x20f0
movl %r14d, %edi
movq %r13, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
leaq -0x4e0(%rbp), %r12
leaq 0x4111(%rip), %r13      # 0x71dc
leaq -0x60(%rbp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x2110
movl $0x200, %edx            # imm = 0x200
movl %r14d, %edi
movq %r12, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x20c0
testl %eax, %eax
jle 0x31aa
cmpl $0x50545448, -0x4e0(%rbp) # imm = 0x50545448
jne 0x31aa
movq %r12, %rdi
leaq 0x219d(%rip), %rsi      # 0x52b2
callq 0x2070
testq %rax, %rax
je 0x3184
movq %rax, %r15
addq $0x16, %r15
movq %r15, %rdi
leaq 0x21e7(%rip), %rsi      # 0x5317
movq %r15, %rdx
xorl %eax, %eax
callq 0x21f0
leaq -0xe0(%rbp), %r14
movq %r14, %rdi
callq 0x20f0
movq %rbx, -0x38(%rbp)
movq %r12, %rbx
movq %r13, %r12
movq %rax, %r13
movq %r15, %rdi
callq 0x20f0
movq %r14, %rdi
movl -0x2c(%rbp), %r14d
movl %r13d, %esi
movq %r12, %r13
movq %rbx, %r12
movq -0x38(%rbp), %rbx
movq %r15, %rdx
movl %eax, %ecx
callq 0x2930
testl %eax, %eax
je 0x329d
leaq -0x2e0(%rbp), %r15
movq %r15, %rdi
callq 0x20f0
movl %r14d, %edi
movq %r15, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
leaq -0x60(%rbp), %r15
cmpl (%r13), %ebx
jge 0x31d7
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 %r15, %r8
callq 0x21d0
jmp 0x30cf
movl %r14d, %edi
callq 0x2340
notl %ebx
jmp 0x329a
leaq 0x1fbe(%rip), %rdi      # 0x51ab
leaq 0x212b(%rip), %rsi      # 0x531f
leaq 0x1fd3(%rip), %rdx      # 0x51ce
movl $0x363, %ecx            # imm = 0x363
xorl %eax, %eax
callq 0x2040
leaq 0x22b1(%rip), %rdi      # 0x54bf
callq 0x22b0
leaq 0x2468(%rip), %rdi      # 0x5682
callq 0x22b0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x329d
leaq 0x1f7d(%rip), %rdi      # 0x51ab
leaq 0x20ea(%rip), %rsi      # 0x531f
leaq 0x1f92(%rip), %rdx      # 0x51ce
movl $0x377, %ecx            # imm = 0x377
xorl %eax, %eax
callq 0x2040
leaq 0x2007(%rip), %rdi      # 0x5256
movq -0x40(%rbp), %r12
movq %r12, %rsi
movl -0x2c(%rbp), %r15d
movl %r15d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2040
leaq 0x2415(%rip), %rdi      # 0x5682
callq 0x22b0
leaq 0x200c(%rip), %rsi      # 0x5285
leaq -0x2e0(%rbp), %rdi
movq %r12, %rdx
movl %r15d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x2060
movl %r14d, %edi
callq 0x2340
negl %ebx
movl %ebx, %r14d
movl %r14d, %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq