// File: /workspace/github_c_makefile_star2_O3/sszllzss[P]webSocket_Libevent_ThreadPool/ev_websocket.c
// Function: ev_webSocket_recv(bufferevent*, unsigned char**, unsigned int*, Websocket_CommunicationType*)
int ev_webSocket_recv(struct bufferevent * bufferev, unsigned char **data, unsigned int *dataLen, Websocket_CommunicationType *type)
{
    unsigned char *webSocketPackage, *recvBuf, *temp, *temp3;
    int ret=0, ret2 = 0, len=0, temp2 = 0, i, in_len = 0, buff_len = 0;
    unsigned int retLen = 0, start_inedx = 0, timer_out = 0;
    //int i;
    buff_len = 1024;
    recvBuf = (unsigned char *)calloc(1, sizeof(char)*1024); memset(recvBuf, 0, 1024);
    temp3 = temp = recvBuf;
    bufferevent_disable(bufferev, EV_READ);
    while(in_len == 0 || len < in_len)
    {
        ret = bufferevent_read(bufferev, temp, 1024);
        
        if(ret>=0)
            len += ret;
        else
        {

            De_printf("webSocket read  Data error!\r\n"); 
            free(recvBuf);
            bufferevent_enable(bufferev, EV_READ);
            return WCT_ERR_EVB;
        }
        if(!temp2)
        {
            for(i=0;i<ret;i++)
            {
                if(webSocket_getInType(temp3) != WCT_ERR)
                {
                    temp2 = 1;
                    break;
                }
                temp3++;
                len--;
                start_inedx++;
            }
        }
        if(in_len == 0 && temp2 != 0 && len >= 3)
        {
            in_len = webSocket_getInLen(temp3, len);
            if(in_len <= 0)
            {
                De_printf("webSocket Data error!\r\n"); 
                free(recvBuf);
                bufferevent_enable(bufferev, EV_READ);
                return WCT_ERR_DATA;
            }
        }
        if(ret == 1024)
        {
            buff_len += 1024;
            recvBuf = (unsigned char *) realloc(recvBuf, buff_len);
            temp = recvBuf + len + start_inedx;
            temp3 = recvBuf + start_inedx;
        }
        else
            temp += ret;
        if(ret == 0)
        {
            timer_out++;
            if(timer_out >= RECV_DATA_TIME_OUT)
            {
                De_printf("webSocket read data time out!\r\n"); 
                free(recvBuf);
                bufferevent_enable(bufferev, EV_READ);
                return WCT_ERR_DATA_TIMEOUT;
            }
        }
        else
            timer_out = 0;
    }
    bufferevent_enable(bufferev, EV_READ);
    if(len > 0)
    {
        //---------- websocket数据打包 ----------
        webSocketPackage = (unsigned char *)calloc(1, sizeof(char)*(len + 128));  memset(webSocketPackage, 0, (len + 128));
        ret2 = webSocket_dePackage(temp3, ret, webSocketPackage, (len + 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))  // 解析为数据包
        {
            *data= (unsigned char*)malloc(retLen);
            if(*data == NULL)
            {
                free(recvBuf);
                free(webSocketPackage);
                return WCT_ERR_MEM;
            }
            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);
            *dataLen = retLen;
            *type = (Websocket_CommunicationType)ret2;
            return retLen;
        }
        else if(retLen > 0 && (ret2 == WCT_DISCONN))
        {
            free(recvBuf);
            free(webSocketPackage);
            *type = (Websocket_CommunicationType)WCT_DISCONN;
            return WCT_DISCONN;
        } /*
            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 ret2;
    }
    else
    {
        free(recvBuf);
        return WCT_ERR_EVB;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, -0x68(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, -0x80(%rbp)
movq %rdi, %r14
movl $0x0, -0x5c(%rbp)
movl $0x1, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x32b0
movq %rax, %rbx
movq %r14, -0x40(%rbp)
movq %r14, %rdi
movl $0x2, %esi
callq 0x3650
movl $0x400, -0x44(%rbp)     # imm = 0x400
xorl %ecx, %ecx
movb $0x1, %r15b
xorl %r14d, %r14d
xorl %r13d, %r13d
movl $0x0, -0x34(%rbp)
xorl %r12d, %r12d
movq %rbx, %rsi
movq %rbx, -0x30(%rbp)
movl %ecx, -0x58(%rbp)
movl $0x400, %edx            # imm = 0x400
movq -0x40(%rbp), %rdi
movq %rsi, -0x50(%rbp)
callq 0x37f0
movq %rax, %rdx
testl %edx, %edx
js 0x5595
movl %r13d, -0x48(%rbp)
leal (%r12,%rdx), %r13d
cmpl $0x0, -0x34(%rbp)
je 0x5490
cmpl $0x3, %r13d
setge %al
andb %al, %r15b
movl $0x1, -0x34(%rbp)
cmpb $0x1, %r15b
jne 0x54f6
movq %rbx, %rdi
movl %r13d, %esi
movq %rdx, %r12
callq 0x4afa
movq %r12, %rdx
movl %eax, -0x48(%rbp)
testl %eax, %eax
movq -0x50(%rbp), %rsi
jg 0x54fa
jmp 0x56cd
testl %edx, %edx
je 0x5548
leal -0x1(%rdx), %eax
addq %rbx, %rax
incq %rax
movq %rax, -0x88(%rbp)
leal (%r14,%rdx), %eax
movl %eax, -0x54(%rbp)
movq %rdx, -0x70(%rbp)
movl %edx, %r13d
negl %r13d
movq %rbx, %rdi
callq 0x4ab5
cmpl $-0x2, %eax
jne 0x5586
incq %rbx
incl %r14d
incl %r13d
jne 0x54b9
movl $0x0, -0x34(%rbp)
movl -0x54(%rbp), %eax
movl %eax, %r14d
movl %r12d, %r13d
movq -0x88(%rbp), %rbx
movq -0x50(%rbp), %rsi
movq -0x70(%rbp), %rdx
jmp 0x54fa
movq -0x50(%rbp), %rsi
cmpl $0x400, %edx            # imm = 0x400
jne 0x5533
movl -0x44(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
movl %eax, -0x44(%rbp)
movslq %eax, %rsi
movq -0x30(%rbp), %rdi
movq %rdx, %rbx
callq 0x36c0
movq %rbx, %rdx
movslq %r13d, %rsi
movl %r14d, %ebx
movq %rax, -0x30(%rbp)
addq %rax, %rbx
addq %rbx, %rsi
xorl %ecx, %ecx
jmp 0x5567
movl %edx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %rsi
testl %edx, %edx
je 0x5553
movl %r13d, %r12d
xorl %ecx, %ecx
jmp 0x556a
movl $0x0, -0x34(%rbp)
movq -0x50(%rbp), %rsi
movl -0x58(%rbp), %ecx
incl %ecx
leaq 0xbedd(%rip), %rax      # 0x1143c
cmpl (%rax), %ecx
jae 0x57a4
movl %r13d, %r12d
movl -0x48(%rbp), %r13d
testl %r13d, %r13d
sete %r15b
je 0x5426
cmpl %r13d, %r12d
jl 0x5426
jmp 0x55ea
subl %r13d, %r12d
movl %r12d, %r13d
movq -0x70(%rbp), %rdx
jmp 0x5454
leaq 0x5c0f(%rip), %rdi      # 0xb1ab
leaq 0x5ec7(%rip), %rsi      # 0xb46a
leaq 0x5c24(%rip), %rdx      # 0xb1ce
movl $0x45b, %ecx            # imm = 0x45B
xorl %eax, %eax
callq 0x3080
leaq 0x5f06(%rip), %rdi      # 0xb4c3
callq 0x3680
leaq 0x5f15(%rip), %rdi      # 0xb4de
callq 0x3680
movq -0x30(%rbp), %rdi
callq 0x3500
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x3790
jmp 0x572d
movq %rdx, %r14
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x3790
testl %r12d, %r12d
jle 0x5724
subl $-0x80, %r12d
movl $0x1, %edi
movq %r12, %rsi
movq %r12, %r15
callq 0x32b0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3250
leaq -0x5c(%rbp), %r13
movq %rbx, %rdi
movl %r14d, %esi
movq %r12, %rdx
movl %r15d, %ecx
movq %r13, %r8
callq 0x4bac
movl %eax, %ebx
cmpl $-0x10, %eax
sete %al
movl (%r13), %r15d
testl %r15d, %r15d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5743
movq -0x40(%rbp), %rdi
movq %r12, %rsi
movl %r15d, %edx
movl $0x1, %ecx
movl $0xfffffff1, %r8d       # imm = 0xFFFFFFF1
callq 0x533d
leaq 0x5b2d(%rip), %rdi      # 0xb1ab
leaq 0x5de5(%rip), %rsi      # 0xb46a
leaq 0x5b42(%rip), %rdx      # 0xb1ce
movl $0x49a, %ecx            # imm = 0x49A
xorl %eax, %eax
callq 0x3080
leaq 0x5ddd(%rip), %rdi      # 0xb47c
movl %r15d, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x3080
leaq 0x5e2b(%rip), %rdi      # 0xb4de
callq 0x3680
movq -0x30(%rbp), %rdi
callq 0x3500
movq %r12, %rdi
callq 0x3500
xorl %ebx, %ebx
jmp 0x5732
leaq 0x5ad7(%rip), %rdi      # 0xb1ab
leaq 0x5d8f(%rip), %rsi      # 0xb46a
leaq 0x5aec(%rip), %rdx      # 0xb1ce
movl $0x473, %ecx            # imm = 0x473
xorl %eax, %eax
callq 0x3080
leaq 0x5e0a(%rip), %rdi      # 0xb4ff
callq 0x3680
leaq 0x5ddd(%rip), %rdi      # 0xb4de
callq 0x3680
movq -0x30(%rbp), %rdi
callq 0x3500
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x3790
movl $0xfffffffb, %ebx       # imm = 0xFFFFFFFB
jmp 0x5732
movq -0x30(%rbp), %rdi
callq 0x3500
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r15d, %r15d
je 0x57fe
leal 0x14(%rbx), %eax
cmpl $0x2, %eax
ja 0x57fe
movl %r15d, %r14d
movq %r14, %rdi
callq 0x35a0
movq -0x80(%rbp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x583a
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x3350
movq -0x30(%rbp), %rdi
callq 0x3500
movq %r12, %rdi
callq 0x3500
movq -0x78(%rbp), %rax
movl %r15d, (%rax)
movq -0x68(%rbp), %rax
movl %ebx, (%rax)
movl %r15d, %ebx
jmp 0x5732
leaq 0x5a00(%rip), %rdi      # 0xb1ab
leaq 0x5cb8(%rip), %rsi      # 0xb46a
leaq 0x5a15(%rip), %rdx      # 0xb1ce
movl $0x487, %ecx            # imm = 0x487
xorl %eax, %eax
callq 0x3080
leaq 0x5d14(%rip), %rdi      # 0xb4e0
callq 0x3680
leaq 0x5d06(%rip), %rdi      # 0xb4de
callq 0x3680
movq -0x30(%rbp), %rdi
callq 0x3500
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x3790
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jmp 0x5732
testl %r15d, %r15d
sete %r15b
cmpl $-0x11, %ebx
setne %r14b
movq -0x30(%rbp), %rdi
callq 0x3500
movq %r12, %rdi
callq 0x3500
orb %r15b, %r14b
jne 0x5732
movq -0x68(%rbp), %rax
movl $0xffffffef, (%rax)     # imm = 0xFFFFFFEF
movl $0xffffffef, %ebx       # imm = 0xFFFFFFEF
jmp 0x5732
movq -0x30(%rbp), %rdi
callq 0x3500
movq %r12, %rdi
callq 0x3500
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
jmp 0x5732
nopl (%rax)