// File: /workspace/github_c_makefile_star2_O1/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, %r15d
movq %rsi, -0x30(%rbp)
movq %rdi, %r13
movl $0x0, -0x34(%rbp)
movl %edx, %ebx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x2160
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2120
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2360
movq %rax, %r12
testl %r12d, %r12d
jle 0x3e3b
leaq 0x157f(%rip), %rsi      # 0x52dd
movl $0x3, %edx
movq %r14, %rdi
callq 0x2100
testl %eax, %eax
je 0x3e48
leal 0x80(%r12), %r15d
movl $0x1, %edi
movq %r15, %rsi
callq 0x2160
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2120
leaq -0x34(%rbp), %r8
movq %r14, %rdi
movl %r12d, %esi
movq %rbx, %rdx
movl %r15d, %ecx
movq %r8, %r15
callq 0x2cbb
cmpl $-0x10, %eax
sete %cl
movl (%r15), %r15d
testl %r15d, %r15d
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3ea9
movq %r13, %rdi
movq %rbx, %rsi
movl %r15d, %edx
movl $0x1, %ecx
movl $0xfffffff1, %r8d       # imm = 0xFFFFFFF1
callq 0x3c75
leaq 0x13c1(%rip), %rdi      # 0x51ab
leaq 0x16c0(%rip), %rsi      # 0x54b1
leaq 0x13d6(%rip), %rdx      # 0x51ce
movl $0x537, %ecx            # imm = 0x537
xorl %eax, %eax
callq 0x2040
leaq 0x14f6(%rip), %rdi      # 0x5301
movl %r15d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2040
leaq 0x1867(%rip), %rdi      # 0x5686
callq 0x22c0
movq %r14, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2230
xorl %eax, %eax
jmp 0x3ef0
movq %r14, %rdi
callq 0x2230
jmp 0x3eed
movq %r13, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x3ba2
movl %eax, %r12d
movq %r14, %rdi
callq 0x2230
testl %r12d, %r12d
js 0x3eff
addq $-0xe, %rbx
xorl %edx, %edx
cmpl $0xf, %r15d
cmovaeq %rbx, %rdx
movq -0x30(%rbp), %rbx
leaq 0xe(%rbx), %rdi
xorl %esi, %esi
callq 0x2120
movabsq $0x207463656e6e6f63, %rax # imm = 0x207463656E6E6F63
movq %rax, (%rbx)
movabsq $0xa0d2e2e2e2074, %rax  # imm = 0xA0D2E2E2E2074
movq %rax, 0x6(%rbx)
movl $0xd, %eax
jmp 0x3ef0
testl %r15d, %r15d
je 0x3eda
addl $0x14, %eax
cmpl $0x2, %eax
ja 0x3eda
movl %r15d, %edx
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x2190
movq %r14, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2230
movl %r15d, %eax
jmp 0x3ef0
movq %r14, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2230
negl %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $-0x12, %rbx
xorl %edx, %edx
cmpl $0x13, %r15d
cmovaeq %rbx, %rdx
movq -0x30(%rbp), %rbx
leaq 0x12(%rbx), %rdi
xorl %esi, %esi
callq 0x2120
movups 0x13be(%rip), %xmm0     # 0x52e1
movups %xmm0, (%rbx)
movw $0xa, 0x10(%rbx)
movl $0x11, %eax
jmp 0x3ef0
nop