// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x400, -0x64(%rbp)     # imm = 0x400
movl $0x1, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x4320
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x4290
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x4750
movb $0x1, %al
cmpl $0x0, -0x60(%rbp)
movb %al, -0x71(%rbp)
je 0x67c5
movl -0x54(%rbp), %eax
cmpl -0x60(%rbp), %eax
setl %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x67d1
jmp 0x6a27
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4900
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jl 0x67f7
movl -0x4c(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x6859
jmp 0x67f9
leaq 0xd99b(%rip), %rdi      # 0x1419b
leaq 0xdc81(%rip), %rsi      # 0x14488
leaq 0xd9b0(%rip), %rdx      # 0x141be
movl $0x45b, %ecx            # imm = 0x45B
movb $0x0, %al
callq 0x4080
leaq 0xdc79(%rip), %rdi      # 0x1449a
movb $0x0, %al
callq 0x4080
leaq 0xf4ba(%rip), %rdi      # 0x15ce9
movb $0x0, %al
callq 0x4080
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x48b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6c17
cmpl $0x0, -0x58(%rbp)
jne 0x68b0
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x68ae
movq -0x48(%rbp), %rdi
callq 0x5890
cmpl $-0x2, %eax
je 0x6885
movl $0x1, -0x58(%rbp)
jmp 0x68ae
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movl -0x54(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x6866
jmp 0x68b0
cmpl $0x0, -0x60(%rbp)
jne 0x693f
cmpl $0x0, -0x58(%rbp)
je 0x693f
cmpl $0x3, -0x54(%rbp)
jl 0x693f
movq -0x48(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x5950
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jg 0x693d
jmp 0x68dd
leaq 0xd8b7(%rip), %rdi      # 0x1419b
leaq 0xdb9d(%rip), %rsi      # 0x14488
leaq 0xd8cc(%rip), %rdx      # 0x141be
movl $0x473, %ecx            # imm = 0x473
movb $0x0, %al
callq 0x4080
leaq 0xdbb3(%rip), %rdi      # 0x144b8
movb $0x0, %al
callq 0x4080
leaq 0xf3d6(%rip), %rdi      # 0x15ce9
movb $0x0, %al
callq 0x4080
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x48b0
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x6c17
jmp 0x693f
cmpl $0x400, -0x4c(%rbp)     # imm = 0x400
jne 0x6989
movl -0x64(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
movl %eax, -0x64(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x47b0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movslq -0x54(%rbp), %rcx
addq %rcx, %rax
movl -0x6c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movl -0x6c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x699a
movl -0x4c(%rbp), %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x6a1b
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
leaq 0x16b15(%rip), %rcx     # 0x1d4c8
cmpl (%rcx), %eax
jb 0x6a19
jmp 0x69b9
leaq 0xd7db(%rip), %rdi      # 0x1419b
leaq 0xdac1(%rip), %rsi      # 0x14488
leaq 0xd7f0(%rip), %rdx      # 0x141be
movl $0x487, %ecx            # imm = 0x487
movb $0x0, %al
callq 0x4080
leaq 0xdaef(%rip), %rdi      # 0x144d0
movb $0x0, %al
callq 0x4080
leaq 0xf2fa(%rip), %rdi      # 0x15ce9
movb $0x0, %al
callq 0x4080
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x48b0
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x6c17
jmp 0x6a22
movl $0x0, -0x70(%rbp)
jmp 0x67ae
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x48b0
cmpl $0x0, -0x54(%rbp)
jle 0x6c07
movl -0x54(%rbp), %eax
addl $0x80, %eax
movslq %eax, %rsi
shlq $0x0, %rsi
movl $0x1, %edi
callq 0x4320
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movl -0x54(%rbp), %eax
addl $0x80, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x4290
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq -0x30(%rbp), %rdx
movl -0x54(%rbp), %ecx
addl $0x80, %ecx
leaq -0x68(%rbp), %r8
callq 0x5b10
movl %eax, -0x50(%rbp)
cmpl $-0x10, -0x50(%rbp)
jne 0x6b1f
cmpl $0x0, -0x68(%rbp)
jbe 0x6b1f
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x68(%rbp), %edx
movl $0x1, %ecx
movl $0xfffffff1, %r8d       # imm = 0xFFFFFFF1
callq 0x6660
leaq 0xd6d7(%rip), %rdi      # 0x1419b
leaq 0xd9bd(%rip), %rsi      # 0x14488
leaq 0xd6ec(%rip), %rdx      # 0x141be
movl $0x49a, %ecx            # imm = 0x49A
movb $0x0, %al
callq 0x4080
movl -0x68(%rbp), %esi
movq -0x30(%rbp), %rdx
leaq 0xda04(%rip), %rdi      # 0x144f0
movb $0x0, %al
callq 0x4080
leaq 0xf1ef(%rip), %rdi      # 0x15ce9
movb $0x0, %al
callq 0x4080
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x30(%rbp), %rdi
callq 0x45f0
movl $0x0, -0x4(%rbp)
jmp 0x6c17
cmpl $0x0, -0x68(%rbp)
jbe 0x6bb8
cmpl $-0x13, -0x50(%rbp)
je 0x6b3b
cmpl $-0x12, -0x50(%rbp)
je 0x6b3b
cmpl $-0x14, -0x50(%rbp)
jne 0x6bb8
movl -0x68(%rbp), %eax
movl %eax, %edi
callq 0x46a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x6b77
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x30(%rbp), %rdi
callq 0x45f0
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x6c17
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
movl -0x68(%rbp), %eax
movl %eax, %edx
callq 0x43f0
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x30(%rbp), %rdi
callq 0x45f0
movl -0x68(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x50(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6c17
cmpl $0x0, -0x68(%rbp)
jbe 0x6be9
cmpl $-0x11, -0x50(%rbp)
jne 0x6be9
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x30(%rbp), %rdi
callq 0x45f0
movq -0x28(%rbp), %rax
movl $0xffffffef, (%rax)     # imm = 0xFFFFFFEF
movl $0xffffffef, -0x4(%rbp) # imm = 0xFFFFFFEF
jmp 0x6c17
jmp 0x6beb
jmp 0x6bed
movq -0x38(%rbp), %rdi
callq 0x45f0
movq -0x30(%rbp), %rdi
callq 0x45f0
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6c17
movq -0x38(%rbp), %rdi
callq 0x45f0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)