// File: /workspace/github_c_makefile_star2_O3/sszllzss[P]webSocket_Libevent_ThreadPool/websocket_common.c
// Function: ev_webSocket_recv(bufferevent*, unsigned char*, unsigned int)
int ev_webSocket_recv(struct bufferevent * bufferev, unsigned char *data, unsigned int dataMaxLen)
{
    unsigned char *webSocketPackage, *recvBuf;
    int ret, ret2 = 0;
    unsigned int retLen = 0;
    //int i;

    recvBuf = (unsigned char *)calloc(1, sizeof(char)*dataMaxLen);  memset(recvBuf, 0, dataMaxLen);
    ret = bufferevent_read(bufferev, recvBuf, dataMaxLen);
    if(ret > 0)
    {
        //------------------------------------------------------------------------------------------------------------- 和客户端的连接操作
        if(strncmp((char *)recvBuf, "GET", 3) == 0)
        {
            
            ret2 = ev_webSocket_serverLinkToClient(bufferev,(char *) recvBuf, (unsigned int)ret);
            free(recvBuf);
            if(ret2 < 0)
            {
                memset(data, 0, dataMaxLen);
                strcpy((char*)data, "connect false !\r\n");
                return strlen("connect false !\r\n");
            }
            memset(data, 0, dataMaxLen);
            strcpy((char *)data, "connect ...\r\n");
            return strlen("connect ...\r\n");
        }

        //------------------------------------------------------------------------------------------------------------- 正常数据交换

        //printf("webSocket_recv : %d\r\n", ret);
        //---------- websocket数据打包 ----------
        webSocketPackage = (unsigned char *)calloc(1, sizeof(char)*(ret + 128));  memset(webSocketPackage, 0, (ret + 128));
        ret2 = webSocket_dePackage(recvBuf, ret, webSocketPackage, (ret + 128), &retLen);
        if(ret2 == WCT_PING && retLen > 0)      // 解析为ping包, 自动回pong
        {
            ev_webSocket_send(bufferev, webSocketPackage, retLen, true, WCT_PONG);
            // 显示数据
            De_printf("webSocket_recv : PING %d\r\n%s\r\n" , retLen, webSocketPackage); 
            free(recvBuf);
            free(webSocketPackage);
            return WCT_NULL;
        }
        else if(retLen > 0 && (ret2 == WCT_TXTDATA || ret2 == WCT_BINDATA || ret2 == WCT_MINDATA))  // 解析为数据包
        {
            memcpy(data, webSocketPackage, retLen);     // 把解析得到的数据复制出去
            /*
            // 显示数据
            if(webSocketPackage[0] >= ' ' && webSocketPackage[0] <= '~')
            { 
            printf("\r\nwebSocket_recv : New Package StrFile ret2:%d/retLen:%d\r\n%s\r\n" , ret2, retLen, webSocketPackage); 
            }
            else
            {
            printf("\r\nwebSocket_recv : New Package BinFile ret2:%d/retLen:%d\r\n" , ret2, retLen); 
            for(i = 0; i < retLen; i++) printf("%.2X ", webSocketPackage[i]); printf("\r\n");
            }*/
            free(recvBuf);
            free(webSocketPackage);
            return retLen;
        }/*
            else
            {
        // 显示数据
        if(recvBuf[0] >= ' ' && recvBuf[0] <= '~') 
        printf("\r\nwebSocket_recv : ret:%d/ret2:%d/retLen:%d\r\n%s\r\n" , ret, ret2, retLen, recvBuf); 
        else 
        {
        printf("\r\nwebSocket_recv : ret:%d/ret2:%d/retLen:%d\r\n%s\r\n" , ret, ret2, retLen, recvBuf); 
        for(i = 0; i < ret; i++) printf("%.2X ", recvBuf[i]); printf("\r\n");
        }
        }*/
        free(recvBuf);
        free(webSocketPackage);
        return -ret;
    }
    else
    {
        free(recvBuf);
        return ret;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r13d
movq %rsi, -0x30(%rbp)
movq %rdi, %rbx
movl $0x0, -0x34(%rbp)
movl %edx, %r15d
movl $0x1, %edi
movq %r15, %rsi
callq 0x2150
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x2350
movq %rax, %r12
testl %r12d, %r12d
jle 0x3d9e
cmpb $0x47, (%r14)
jne 0x3ce8
cmpb $0x45, 0x1(%r14)
jne 0x3ce8
cmpb $0x54, 0x2(%r14)
jne 0x3ce8
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x3ad3
movl %eax, %ebx
movq %r14, %rdi
callq 0x2220
testl %ebx, %ebx
js 0x3dfe
addq $-0xe, %r15
xorl %edx, %edx
cmpl $0xf, %r13d
cmovaeq %r15, %rdx
movq -0x30(%rbp), %rbx
leaq 0xe(%rbx), %rdi
xorl %esi, %esi
callq 0x2110
movabsq $0x207463656e6e6f63, %rax # imm = 0x207463656E6E6F63
movq %rax, (%rbx)
movabsq $0xa0d2e2e2e2074, %rax  # imm = 0xA0D2E2E2E2074
movq %rax, 0x6(%rbx)
movl $0xd, %r13d
jmp 0x3dec
leal 0x80(%r12), %r13d
movl $0x1, %edi
movq %r13, %rsi
callq 0x2150
movq %rax, %r15
leaq -0x34(%rbp), %r8
movq %r14, %rdi
movl %r12d, %esi
movq %rax, %rdx
movl %r13d, %ecx
movq %r8, %r13
callq 0x2cb5
cmpl $-0x10, %eax
sete %cl
movl (%r13), %r13d
testl %r13d, %r13d
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3da8
movq %rbx, %rdi
movq %r15, %rsi
movl %r13d, %edx
movl $0x1, %ecx
movl $0xfffffff1, %r8d       # imm = 0xFFFFFFF1
callq 0x3ba6
leaq 0x145c(%rip), %rdi      # 0x51ab
leaq 0x1757(%rip), %rsi      # 0x54ad
leaq 0x1471(%rip), %rdx      # 0x51ce
movl $0x537, %ecx            # imm = 0x537
xorl %eax, %eax
callq 0x2040
leaq 0x158d(%rip), %rdi      # 0x52fd
movl %r13d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2040
leaq 0x18fe(%rip), %rdi      # 0x5682
callq 0x22b0
movq %r14, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
xorl %r13d, %r13d
jmp 0x3dec
movq %r14, %rdi
callq 0x2220
jmp 0x3de9
testl %r13d, %r13d
je 0x3dd6
addl $0x14, %eax
cmpl $0x2, %eax
ja 0x3dd6
movl %r13d, %edx
movq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x2180
movq %r14, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
jmp 0x3dec
movq %r14, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
negl %r12d
movl %r12d, %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $-0x12, %r15
xorl %edx, %edx
cmpl $0x13, %r13d
cmovaeq %r15, %rdx
movq -0x30(%rbp), %rbx
leaq 0x12(%rbx), %rdi
xorl %esi, %esi
callq 0x2110
movups 0x14bb(%rip), %xmm0     # 0x52dd
movups %xmm0, (%rbx)
movw $0xa, 0x10(%rbx)
movl $0x11, %r13d
jmp 0x3dec
nop