// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %r8, %r14
movq %rcx, -0x78(%rbp)
movl %edx, -0x30(%rbp)
movq %rsi, %r13
movq %rdi, %r12
leaq -0x80(%rbp), %rbx
movq $0x0, (%rbx)
leaq -0x330(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3250
leaq -0x530(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3250
xorps %xmm0, %xmm0
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, -0x100(%rbp)
leaq -0x130(%rbp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps 0x62a4(%rip), %xmm0     # 0xb010
movaps %xmm0, (%rdx)
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x3150
testl %eax, %eax
jne 0x515a
movq %r14, -0x70(%rbp)
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x5169
movq %r15, %r14
movq %r13, -0x58(%rbp)
movq %r12, -0x68(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movw $0x2, -0x40(%rbp)
movq 0x18(%rdi), %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x30(%rbp), %ebx
movl %ebx, %eax
rolw $0x8, %ax
movw %ax, -0x3e(%rbp)
callq 0x3550
testl %ebx, %ebx
jg 0x4e08
leaq 0x63d7(%rip), %rdi      # 0xb1ab
leaq 0x6477(%rip), %rsi      # 0xb252
leaq 0x63ec(%rip), %rdx      # 0xb1ce
movl $0x3b7, %ecx            # imm = 0x3B7
xorl %eax, %eax
callq 0x3080
leaq 0x65bb(%rip), %rdi      # 0xb3b0
xorl %eax, %eax
callq 0x3080
leaq 0x66db(%rip), %rdi      # 0xb4de
callq 0x3680
xorl %r13d, %r13d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3110
testl %eax, %eax
js 0x5083
movl %eax, %r15d
movq %r14, -0x60(%rbp)
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3270
orl $0x800, %eax            # imm = 0x800
movl %r15d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x3270
leaq -0x40(%rbp), %rsi
movl %r15d, %edi
movl $0x10, %edx
callq 0x3360
cmpl $-0x1, %eax
jne 0x4ebf
xorl %r13d, %r13d
leaq 0xc5bf(%rip), %r12      # 0x11430
leaq -0x50(%rbp), %rbx
leaq -0x40(%rbp), %r14
movl %r13d, %eax
leal 0x1(%rax), %r13d
cmpl (%r12), %eax
jge 0x50fe
movq $0x0, -0x50(%rbp)
movq $0x3e8, -0x48(%rbp)     # imm = 0x3E8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x33f0
movl %r15d, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x3360
cmpl $-0x1, %eax
je 0x4e79
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 -0x50(%rbp), %r14
movaps %xmm0, (%r14)
movq %r14, %rdi
movl $0x10, %esi
callq 0x44f4
movq %r14, %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x42eb
leaq -0x330(%rbp), %r12
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x3250
leaq 0x6176(%rip), %rsi      # 0xb0a0
movq %r12, %rdi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rcx
movl -0x30(%rbp), %r8d
movq %rbx, %r9
xorl %eax, %eax
callq 0x30f0
movq %r12, %rdi
callq 0x31f0
movl %r15d, %edi
movq %r12, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3210
leaq -0x530(%rbp), %r12
leaq 0xc4c8(%rip), %rbx      # 0x11434
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x3250
movl $0x200, %edx            # imm = 0x200
movl %r15d, %edi
movq %r12, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3160
testl %eax, %eax
jle 0x5047
cmpl $0x50545448, -0x530(%rbp) # imm = 0x50545448
jne 0x5047
movq %r12, %rdi
leaq 0x648d(%rip), %rsi      # 0xb43f
callq 0x3100
testq %rax, %rax
je 0x501e
movq %rax, %rbx
addq $0x16, %rbx
movq %rbx, %rdi
leaq 0x64c9(%rip), %rsi      # 0xb496
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3410
movq %r13, -0x30(%rbp)
movq %r12, %r13
leaq -0x100(%rbp), %rdi
callq 0x31f0
movq %r14, %r12
movq %rax, %r14
movq %rbx, %rdi
callq 0x31f0
leaq -0x100(%rbp), %rdi
movl %r14d, %esi
movq %r12, %r14
movq %r13, %r12
movq -0x30(%rbp), %r13
movq %rbx, %rdx
movl %eax, %ecx
callq 0x4650
testl %eax, %eax
je 0x50c3
leaq -0x330(%rbp), %rbx
movq %rbx, %rdi
callq 0x31f0
movl %r15d, %edi
movq %rbx, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3210
leaq 0xc3ed(%rip), %rbx      # 0x11434
cmpl (%rbx), %r13d
jge 0x5074
incl %r13d
movq $0x0, -0x50(%rbp)
movq $0x3e8, -0x48(%rbp)     # imm = 0x3E8
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x33f0
jmp 0x4f6c
movl %r15d, %edi
callq 0x37e0
xorl %ebx, %ebx
jmp 0x5145
leaq 0x6121(%rip), %rdi      # 0xb1ab
leaq 0x61c1(%rip), %rsi      # 0xb252
leaq 0x6136(%rip), %rdx      # 0xb1ce
xorl %ebx, %ebx
movl $0x3bc, %ecx            # imm = 0x3BC
xorl %eax, %eax
callq 0x3080
leaq 0x63ee(%rip), %rdi      # 0xb49b
callq 0x3680
leaq 0x6425(%rip), %rdi      # 0xb4de
callq 0x3680
jmp 0x5145
movq -0x68(%rbp), %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x3700
movq %rax, %rbx
movq %rax, %rdi
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
callq 0x3450
movq %rbx, %rdi
movl $0x6, %esi
callq 0x3790
jmp 0x5145
leaq 0x62de(%rip), %rdi      # 0xb3e3
xorl %ebx, %ebx
movq -0x58(%rbp), %r14
movq %r14, %rsi
movl -0x30(%rbp), %r12d
movl %r12d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x3080
leaq 0x62ec(%rip), %rsi      # 0xb412
leaq -0x330(%rbp), %rdi
movq %r14, %rdx
movl %r12d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x30f0
movl %r15d, %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 0x507c
leaq 0x6191(%rip), %rdi      # 0xb301
leaq 0x6057(%rip), %rsi      # 0xb1ce
leaq 0x618c(%rip), %rcx      # 0xb30a
movl $0x3ac, %edx            # imm = 0x3AC
callq 0x32a0