// File: /workspace/github_c_makefile_star2_O3/sszllzss[P]webSocket_Libevent_ThreadPool/ev_websocket.c
// Function: ev_webSocket_clientLinkToServer(event_base*, char*, int, char*, void (*)(bufferevent*, void*), void (*)(bufferevent*, void*), void (*)(bufferevent*, short, void*), void*)
struct bufferevent *  ev_webSocket_clientLinkToServer(struct event_base * base ,char *ip, int port, char *interface_path, bufferevent_data_cb read_cb, bufferevent_data_cb write_cb, bufferevent_event_cb event_cb, void * arg)
{
    int ret, fd , timeOut;
    struct evutil_addrinfo hints;
    struct evutil_addrinfo *addr_out = NULL;
    //int i;
    unsigned char loginBuf[512] = {0}, recBuf[512] = {0}, shakeKey[128] = {0}, *p;

    memset(&hints,0 , sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = EVUTIL_AI_ADDRCONFIG;
    int err = evutil_getaddrinfo(ip, NULL, &hints, &addr_out);
    if(err != 0)
    {
        De_printf("ev_webSocket_clientLinkToServer:你输入的'%s'无效或者无法解析！/r/n", ip);
        De_fprintf(stderr, "ev_webSocket_clientLinkToServer:Error while resoliving '%s':%s", 
                ip, evutil_gai_strerror(err));
        return NULL;
    }
    assert(addr_out);
    // 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 = ((struct sockaddr_in *)addr_out->ai_addr)->sin_addr.s_addr;
    //report_addr.sin_addr.s_addr = inet_addr(ip);			// 服务器IP地址     
    report_addr.sin_port = htons(port); 							// 服务器端口号     
    evutil_freeaddrinfo(addr_out);
    if(port<=0)
    {
        De_printf("ev_webSocket_clientLinkToServer:端口输入错误");
    }
    //create unix socket  
    if((fd = socket(AF_INET,SOCK_STREAM, 0)) < 0) 
    {  
        De_printf("webSocket_login : cannot create socket\r\n");  
        return NULL;  
    }
    /* 
    // 测试 -----  创建握手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)
        {
            printf("webSocket_login : %s:%d cannot connect !  %d\r\n" , ip, port, timeOut);  
            //
            sprintf((char *)loginBuf, "webSocket_login : %s:%d cannot connect !  %d" , ip, port, timeOut);
            close(fd); 
            return NULL;  
        }
        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)  // 握手成功, 发送登录数据包
                    {
                        //创建bufferevnt
                        struct bufferevent * bufferev =  bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
                        bufferevent_setcb(bufferev, read_cb, write_cb, event_cb, arg);
                        bufferevent_enable(bufferev, EV_READ| EV_WRITE);
                        return bufferev;
                    }
                    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 NULL;
        }
        delayms(1);  //1ms
    }
    //
    close(fd); 
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %r9, %r14
movq %r8, %r15
movq %rcx, -0x68(%rbp)
movl %edx, -0x2c(%rbp)
movq %rsi, %r13
movq %rdi, %r12
leaq -0x70(%rbp), %rbx
movq $0x0, (%rbx)
leaq -0x530(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3250
xorps %xmm0, %xmm0
leaq -0x130(%rbp), %rdx
movups %xmm0, 0x10(%rdx)
movups %xmm0, 0x20(%rdx)
movaps 0x6205(%rip), %xmm0     # 0xb010
movups %xmm0, (%rdx)
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x3150
testl %eax, %eax
jne 0x51fb
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x520a
movq %r13, -0x38(%rbp)
movq %r12, -0x58(%rbp)
movq %r15, -0x60(%rbp)
movq $0x0, -0x40(%rbp)
movw $0x2, -0x48(%rbp)
movq 0x18(%rdi), %rax
movl 0x4(%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x2c(%rbp), %ebx
movl %ebx, %eax
rolw $0x8, %ax
movw %ax, -0x46(%rbp)
callq 0x3550
testl %ebx, %ebx
jg 0x4ea5
leaq 0x633a(%rip), %rdi      # 0xb1ab
leaq 0x63da(%rip), %rsi      # 0xb252
leaq 0x634f(%rip), %rdx      # 0xb1ce
movl $0x3b7, %ecx            # imm = 0x3B7
xorl %eax, %eax
callq 0x3080
leaq 0x651e(%rip), %rdi      # 0xb3b0
xorl %eax, %eax
callq 0x3080
leaq 0x663e(%rip), %rdi      # 0xb4de
callq 0x3680
xorl %r13d, %r13d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3110
testl %eax, %eax
js 0x5124
movl %eax, %r12d
movq %r14, -0x50(%rbp)
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3270
orl $0x800, %eax            # imm = 0x800
movl %r12d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x3270
leaq -0x48(%rbp), %rsi
movl %r12d, %edi
movl $0x10, %edx
callq 0x3360
cmpl $-0x1, %eax
jne 0x4f64
xorl %r13d, %r13d
leaq 0xc522(%rip), %r15      # 0x11430
leaq -0x330(%rbp), %rbx
leaq -0x48(%rbp), %r14
movl %r13d, %eax
leal 0x1(%rax), %r13d
cmpl (%r15), %eax
jge 0x519f
movq $0x0, -0x330(%rbp)
movq $0x3e8, -0x328(%rbp) # imm = 0x3E8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x33f0
movl %r12d, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x3360
cmpl $-0x1, %eax
je 0x4f19
xorps %xmm0, %xmm0
leaq -0x100(%rbp), %rbx
movaps %xmm0, 0x70(%rbx)
movaps %xmm0, 0x60(%rbx)
movaps %xmm0, 0x50(%rbx)
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
leaq -0x330(%rbp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movl $0x10, %esi
callq 0x45e6
movq %r15, %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x43c2
leaq -0x530(%rbp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x3250
leaq 0x60ce(%rip), %rsi      # 0xb0a0
movq %r14, %rdi
movq -0x68(%rbp), %rdx
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %r8d
movq %rbx, %r9
xorl %eax, %eax
callq 0x30f0
movq %r14, %rdi
callq 0x31f0
movl %r12d, %edi
movq %r14, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3210
leaq 0xc427(%rip), %rbx      # 0x11434
leaq -0x80(%rbp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x3250
movl $0x200, %edx            # imm = 0x200
movl %r12d, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3160
testl %eax, %eax
jle 0x50e8
cmpl $0x50545448, -0x330(%rbp) # imm = 0x50545448
jne 0x50e8
movq %r15, %rdi
leaq 0x63e8(%rip), %rsi      # 0xb43f
callq 0x3100
testq %rax, %rax
je 0x50bb
movq %rax, %r14
addq $0x16, %r14
movq %r14, %rdi
leaq 0x6424(%rip), %rsi      # 0xb496
movq %r14, %rdx
xorl %eax, %eax
callq 0x3410
leaq -0x100(%rbp), %rdi
callq 0x31f0
movl %r12d, %ebx
movq %r15, %r12
movq %rax, %r15
movq %r14, %rdi
callq 0x31f0
leaq -0x100(%rbp), %rdi
movl %r15d, %esi
movq %r12, %r15
movl %ebx, %r12d
movq %r14, %rdx
movl %eax, %ecx
callq 0x4730
testl %eax, %eax
je 0x5164
leaq -0x530(%rbp), %rbx
movq %rbx, %rdi
callq 0x31f0
movl %r12d, %edi
movq %rbx, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3210
leaq 0xc350(%rip), %rbx      # 0x11434
leaq -0x80(%rbp), %r14
cmpl (%rbx), %r13d
jge 0x5115
incl %r13d
movq $0x0, -0x80(%rbp)
movq $0x3e8, -0x78(%rbp)     # imm = 0x3E8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x33f0
jmp 0x5011
movl %r12d, %edi
callq 0x37e0
xorl %ebx, %ebx
jmp 0x51e6
leaq 0x6080(%rip), %rdi      # 0xb1ab
leaq 0x6120(%rip), %rsi      # 0xb252
leaq 0x6095(%rip), %rdx      # 0xb1ce
xorl %ebx, %ebx
movl $0x3bc, %ecx            # imm = 0x3BC
xorl %eax, %eax
callq 0x3080
leaq 0x634d(%rip), %rdi      # 0xb49b
callq 0x3680
leaq 0x6384(%rip), %rdi      # 0xb4de
callq 0x3680
jmp 0x51e6
movq -0x58(%rbp), %rdi
movl %r12d, %esi
movl $0x1, %edx
callq 0x3700
movq %rax, %rbx
movq %rax, %rdi
movq -0x60(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
callq 0x3450
movq %rbx, %rdi
movl $0x6, %esi
callq 0x3790
jmp 0x51e6
leaq 0x623d(%rip), %rdi      # 0xb3e3
xorl %ebx, %ebx
movq -0x38(%rbp), %r14
movq %r14, %rsi
movl -0x2c(%rbp), %r15d
movl %r15d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x3080
leaq 0x624b(%rip), %rsi      # 0xb412
leaq -0x530(%rbp), %rdi
movq %r14, %rdx
movl %r15d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x30f0
movl %r12d, %edi
callq 0x37e0
movq %rbx, %rax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
movl %eax, %esi
callq 0x3810
jmp 0x511d
leaq 0x60f0(%rip), %rdi      # 0xb301
leaq 0x5fb6(%rip), %rsi      # 0xb1ce
leaq 0x60eb(%rip), %rcx      # 0xb30a
movl $0x3ac, %edx            # imm = 0x3AC
callq 0x32a0