// File: /workspace/github_c_makefile_star2_O2/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 $0x4f8, %rsp            # imm = 0x4F8
movq %r9, %r14
movq %r8, %r15
movq %rcx, -0x68(%rbp)
movl %edx, -0x2c(%rbp)
movq %rsi, %r13
movq %rdi, %r12
leaq -0x70(%rbp), %rbx
andq $0x0, (%rbx)
leaq -0x320(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3260
xorps %xmm0, %xmm0
leaq -0x120(%rbp), %rdx
movups %xmm0, 0x10(%rdx)
movups %xmm0, 0x20(%rdx)
movaps 0x5c3c(%rip), %xmm0     # 0xa010
movups %xmm0, (%rdx)
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x3150
testl %eax, %eax
jne 0x4741
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x47fd
movq %r13, -0x38(%rbp)
andq $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 0x3570
testl %ebx, %ebx
jg 0x4463
leaq 0x5d7c(%rip), %rdi      # 0xa1ab
leaq 0x5e1c(%rip), %rsi      # 0xa252
leaq 0x5d91(%rip), %rdx      # 0xa1ce
movl $0x3b7, %ecx            # imm = 0x3B7
xorl %eax, %eax
callq 0x3080
leaq 0x5f60(%rip), %rdi      # 0xa3b0
xorl %eax, %eax
callq 0x3080
leaq 0x6080(%rip), %rdi      # 0xa4de
callq 0x36b0
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x3110
testl %eax, %eax
js 0x44e7
movl %eax, %r13d
movq %r12, -0x50(%rbp)
movq %r15, -0x58(%rbp)
movq %r14, -0x60(%rbp)
pushq $0x3
popq %rsi
movl %eax, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3280
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %r13d, -0x30(%rbp)
movl %r13d, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x3280
leaq -0x48(%rbp), %r15
pushq $0x10
popq %r13
leaq 0xbf8e(%rip), %r12      # 0x10448
pushq $0x1
popq %r14
movl -0x30(%rbp), %edi
movq %r15, %rsi
movl %r13d, %edx
callq 0x3370
cmpl $-0x1, %eax
jne 0x4528
cmpl (%r12), %ebx
jge 0x46a9
movl %r14d, %edi
callq 0x481c
incl %ebx
jmp 0x44be
leaq 0x5cbd(%rip), %rdi      # 0xa1ab
leaq 0x5d5d(%rip), %rsi      # 0xa252
leaq 0x5cd2(%rip), %rdx      # 0xa1ce
xorl %r15d, %r15d
movl $0x3bc, %ecx            # imm = 0x3BC
xorl %eax, %eax
callq 0x3080
leaq 0x5f89(%rip), %rdi      # 0xa49b
callq 0x36b0
leaq 0x5fc0(%rip), %rdi      # 0xa4de
callq 0x36b0
jmp 0x47e8
xorps %xmm0, %xmm0
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)
movq %r14, %rdi
callq 0x3c4d
leaq -0x320(%rbp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x3260
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x68(%rbp), %rdx
movq %r14, %rcx
movq %r15, %r8
callq 0x3e4e
movq %r15, %rdi
callq 0x31f0
movl -0x30(%rbp), %r12d
movl %r12d, %edi
movq %r15, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3220
leaq -0x520(%rbp), %r15
leaq 0xbe92(%rip), %r14      # 0x1044c
pushq $0x1
popq %r13
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x3260
movl $0x200, %edx            # imm = 0x200
movl %r12d, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3160
testl %eax, %eax
jle 0x4695
cmpl $0x50545448, -0x520(%rbp) # imm = 0x50545448
jne 0x4695
movq %r15, %rdi
leaq 0x5e3b(%rip), %rsi      # 0xa43f
callq 0x3100
testq %rax, %rax
je 0x4668
movq %rax, %r13
addq $0x16, %r13
movq %r13, %rdi
leaq 0x5e77(%rip), %rsi      # 0xa496
movq %r13, %rdx
xorl %eax, %eax
callq 0x3420
leaq -0xf0(%rbp), %rdi
callq 0x31f0
movl %r12d, %r14d
movq %r15, %r12
movq %rax, %r15
movq %r13, %rdi
callq 0x31f0
leaq -0xf0(%rbp), %rdi
movl %r15d, %esi
movq %r12, %r15
movl %r14d, %r12d
movq %r13, %rdx
movl %eax, %ecx
callq 0x3d49
testl %eax, %eax
je 0x4707
leaq -0x320(%rbp), %r14
movq %r14, %rdi
callq 0x31f0
movl %r12d, %edi
movq %r14, %rsi
movq %rax, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3220
leaq 0xbdbb(%rip), %r14      # 0x1044c
pushq $0x1
popq %r13
cmpl (%r14), %ebx
jge 0x46f7
incl %ebx
movl %r13d, %edi
callq 0x481c
jmp 0x45be
incl %ebx
leaq 0x5d31(%rip), %rdi      # 0xa3e3
xorl %r15d, %r15d
movq -0x38(%rbp), %r14
movq %r14, %rsi
movl -0x2c(%rbp), %r12d
movl %r12d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3080
leaq 0x5d3f(%rip), %rsi      # 0xa412
leaq -0x320(%rbp), %rdi
movq %r14, %rdx
movl %r12d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x30f0
movl -0x30(%rbp), %edi
callq 0x3810
jmp 0x47e8
movl %r12d, %edi
callq 0x3810
xorl %r15d, %r15d
jmp 0x47e8
pushq $0x1
popq %rdx
movq -0x50(%rbp), %rdi
movl %r12d, %esi
callq 0x3730
movq %rax, %r15
movq %rax, %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
callq 0x3460
pushq $0x6
popq %rsi
movq %r15, %rdi
callq 0x37c0
jmp 0x47e8
movl %eax, %ebx
leaq 0x5a61(%rip), %r14      # 0xa1ab
leaq 0x5b01(%rip), %rsi      # 0xa252
leaq 0x5a76(%rip), %rdx      # 0xa1ce
xorl %r15d, %r15d
movq %r14, %rdi
movl $0x3a7, %ecx            # imm = 0x3A7
xorl %eax, %eax
callq 0x3080
leaq 0x5b01(%rip), %rdi      # 0xa272
movq %r13, %rsi
xorl %eax, %eax
callq 0x3080
leaq 0x5d5c(%rip), %rdi      # 0xa4de
callq 0x36b0
movq 0xb84a(%rip), %r12      # 0xffd8
movq (%r12), %rdi
movq %r14, %rsi
leaq 0x5ab6(%rip), %rdx      # 0xa252
leaq 0x5a2b(%rip), %rcx      # 0xa1ce
movl $0x3a9, %r8d            # imm = 0x3A9
xorl %eax, %eax
callq 0x3610
movq (%r12), %r14
movl %ebx, %edi
callq 0x3740
leaq 0x5b00(%rip), %rsi      # 0xa2c2
movq %r14, %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3610
movq (%r12), %rcx
leaq 0x74c7(%rip), %rdi      # 0xbca4
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x3800
movq %r15, %rax
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5afd(%rip), %rdi      # 0xa301
leaq 0x59c3(%rip), %rsi      # 0xa1ce
leaq 0x5af8(%rip), %rcx      # 0xa30a
movl $0x3ac, %edx            # imm = 0x3AC
callq 0x32b0