// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rcx, -0x60(%rbp)
movq %rdx, -0x70(%rbp)
movq %rsi, -0x78(%rbp)
movq %rdi, %r14
andl $0x0, -0x54(%rbp)
pushq $0x1
popq %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x32c0
movq %rax, %rbx
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x3680
movl $0x400, %eax            # imm = 0x400
movq %rax, -0x68(%rbp)
xorl %r12d, %r12d
movq %rbx, %rsi
movq %rbx, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
xorl %r13d, %r13d
movl $0x0, -0x34(%rbp)
xorl %r15d, %r15d
movq %r14, -0x48(%rbp)
xorl %eax, %eax
movl %eax, -0x50(%rbp)
movl -0x34(%rbp), %eax
testl %eax, %eax
je 0x4a1e
cmpl %eax, -0x40(%rbp)
jge 0x4b36
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rsi, -0x80(%rbp)
callq 0x3820
movq %rax, %r12
testl %r12d, %r12d
js 0x4c11
testl %r13d, %r13d
je 0x4a49
movq -0x40(%rbp), %rsi
addl %r12d, %esi
jmp 0x4a9d
movl %r12d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
addq %rbx, %r14
leal (%r15,%r12), %eax
movl %eax, -0x4c(%rbp)
movl %r12d, %r13d
negl %r13d
testl %r13d, %r13d
je 0x4a80
movq %rbx, %rdi
callq 0x40bb
cmpl $-0x2, %eax
jne 0x4a92
incq %rbx
incl %r15d
incl %r13d
jmp 0x4a63
xorl %r13d, %r13d
movl -0x4c(%rbp), %eax
movl %eax, %r15d
movq %r14, %rbx
movq -0x48(%rbp), %r14
jmp 0x4ac8
movq -0x40(%rbp), %rsi
subl %r13d, %esi
movq -0x48(%rbp), %r14
cmpl $0x0, -0x34(%rbp)
movq %rsi, -0x40(%rbp)
jne 0x4ac4
cmpl $0x3, %esi
jl 0x4ac4
movq %rbx, %rdi
callq 0x4100
pushq $0x1
popq %r13
movl %eax, -0x34(%rbp)
testl %eax, %eax
jg 0x4ac8
jmp 0x4c60
pushq $0x1
popq %r13
cmpl $0x400, %r12d           # imm = 0x400
je 0x4b09
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x80(%rbp), %rsi
addq %rax, %rsi
movl $0x0, %eax
testl %r12d, %r12d
jne 0x4a0b
movl -0x50(%rbp), %edx
incl %edx
movl %edx, %eax
leaq 0xb958(%rip), %rcx      # 0x10454
cmpl (%rcx), %edx
jb 0x4a0b
jmp 0x4cbe
movq -0x68(%rbp), %rsi
addl $0x400, %esi            # imm = 0x400
movq -0x30(%rbp), %rdi
movq %rsi, -0x68(%rbp)
callq 0x36f0
movslq -0x40(%rbp), %rsi
movl %r15d, %ebx
movq %rax, -0x30(%rbp)
addq %rax, %rbx
addq %rbx, %rsi
jmp 0x4a09
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x37c0
movq -0x40(%rbp), %r15
testl %r15d, %r15d
jle 0x4cb1
subl $-0x80, %r15d
pushq $0x1
popq %rdi
movq %r15, %rsi
callq 0x32c0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3260
leaq -0x54(%rbp), %r13
movq %rbx, %rdi
movl %r12d, %esi
movq %r14, %rdx
movl %r15d, %ecx
movq %r13, %r8
callq 0x41b0
movl %eax, %ebx
cmpl $-0x10, %eax
sete %al
movl (%r13), %r15d
testl %r15d, %r15d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4d1f
pushq $0x1
popq %rcx
pushq $-0xf
popq %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x4924
leaq 0x55ec(%rip), %rdi      # 0xa1ab
leaq 0x58a4(%rip), %rsi      # 0xa46a
leaq 0x5601(%rip), %rdx      # 0xa1ce
movl $0x49a, %ecx            # imm = 0x49A
xorl %eax, %eax
callq 0x3080
leaq 0x589c(%rip), %rdi      # 0xa47c
movl %r15d, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3080
leaq 0x58ea(%rip), %rdi      # 0xa4de
callq 0x36b0
movq -0x30(%rbp), %rdi
callq 0x3520
movq %r14, %rdi
callq 0x3520
xorl %ebx, %ebx
jmp 0x4d0e
leaq 0x5593(%rip), %rdi      # 0xa1ab
leaq 0x584b(%rip), %rsi      # 0xa46a
leaq 0x55a8(%rip), %rdx      # 0xa1ce
movl $0x45b, %ecx            # imm = 0x45B
xorl %eax, %eax
callq 0x3080
leaq 0x588a(%rip), %rdi      # 0xa4c3
callq 0x36b0
leaq 0x5899(%rip), %rdi      # 0xa4de
callq 0x36b0
movq -0x30(%rbp), %rdi
callq 0x3520
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x37c0
jmp 0x4cba
leaq 0x5544(%rip), %rdi      # 0xa1ab
leaq 0x57fc(%rip), %rsi      # 0xa46a
leaq 0x5559(%rip), %rdx      # 0xa1ce
movl $0x473, %ecx            # imm = 0x473
xorl %eax, %eax
callq 0x3080
leaq 0x5877(%rip), %rdi      # 0xa4ff
callq 0x36b0
leaq 0x584a(%rip), %rdi      # 0xa4de
callq 0x36b0
movq -0x30(%rbp), %rdi
callq 0x3520
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x37c0
pushq $-0x5
jmp 0x4d0d
movq -0x30(%rbp), %rdi
callq 0x3520
pushq $-0x1
jmp 0x4d0d
leaq 0x54e6(%rip), %rdi      # 0xa1ab
leaq 0x579e(%rip), %rsi      # 0xa46a
leaq 0x54fb(%rip), %rdx      # 0xa1ce
movl $0x487, %ecx            # imm = 0x487
xorl %eax, %eax
callq 0x3080
leaq 0x57fa(%rip), %rdi      # 0xa4e0
callq 0x36b0
leaq 0x57ec(%rip), %rdi      # 0xa4de
callq 0x36b0
movq -0x30(%rbp), %rdi
callq 0x3520
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x37c0
pushq $-0x3
popq %rbx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r15d, %r15d
je 0x4d74
leal 0x14(%rbx), %eax
cmpl $0x2, %eax
ja 0x4d74
movl %r15d, %r12d
movq %r12, %rdi
callq 0x35d0
movq -0x78(%rbp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x4dad
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3360
movq -0x30(%rbp), %rdi
callq 0x3520
movq %r14, %rdi
callq 0x3520
movq -0x70(%rbp), %rax
movl %r15d, (%rax)
movq -0x60(%rbp), %rax
movl %ebx, (%rax)
movl %r15d, %ebx
jmp 0x4d0e
testl %r15d, %r15d
sete %r15b
cmpl $-0x11, %ebx
setne %r12b
movq -0x30(%rbp), %rdi
callq 0x3520
movq %r14, %rdi
callq 0x3520
orb %r15b, %r12b
jne 0x4d0e
movq -0x60(%rbp), %rax
movl $0xffffffef, (%rax)     # imm = 0xFFFFFFEF
pushq $-0x11
jmp 0x4d0d
movq -0x30(%rbp), %rdi
callq 0x3520
movq %r14, %rdi
callq 0x3520
pushq $-0x4
jmp 0x4d0d
nopl (%rax)