// File: /workspace/github_c_makefile_star2_O1/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
movl $0x0, -0x48(%rbp)
movl $0x1, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x32b0
movq %rax, %rbx
movl $0x0, -0x34(%rbp)
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
xorl %esi, %esi
callq 0x3250
movq %r14, -0x40(%rbp)
movq %r14, %rdi
movl $0x2, %esi
callq 0x3650
movl $0x400, -0x44(%rbp)     # imm = 0x400
movb $0x1, %dl
xorl %r15d, %r15d
xorl %r14d, %r14d
movl $0x0, -0x30(%rbp)
xorl %ecx, %ecx
movq %rbx, -0x50(%rbp)
movq %rbx, %r12
movb %dl, -0x29(%rbp)
movl %r14d, -0x58(%rbp)
movq %rcx, %r14
movl $0x400, %edx            # imm = 0x400
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x37f0
movq %rax, %r13
testl %r13d, %r13d
js 0x5509
leal (%r14,%r13), %ecx
movb $0x1, %al
cmpl $0x0, -0x30(%rbp)
je 0x5493
movl $0x1, -0x30(%rbp)
movb -0x29(%rbp), %dl
andb %al, %dl
cmpb $0x1, %dl
movl -0x58(%rbp), %r14d
jne 0x5410
cmpl $0x3, %ecx
jl 0x5410
movq %rbx, %rdi
movq %rcx, %r14
movl %r14d, %esi
callq 0x4a10
movq %r14, %rcx
movl %eax, %r14d
testl %eax, %eax
jle 0x5643
cmpl $0x400, %r13d           # imm = 0x400
jne 0x544a
movl -0x44(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
movq %rcx, %rbx
movl %eax, -0x44(%rbp)
movslq %eax, %rsi
movq %r12, %rdi
callq 0x36c0
movq %rbx, %rcx
movq %rax, %r12
movslq %ecx, %rax
movl %r15d, %ebx
addq %r12, %rbx
addq %rbx, %rax
movq %rax, -0x50(%rbp)
jmp 0x5456
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, -0x50(%rbp)
xorl %eax, %eax
testl %r13d, %r13d
jne 0x5473
movl -0x34(%rbp), %edx
incl %edx
movl %edx, %eax
leaq 0xbfd1(%rip), %rsi      # 0x1143c
cmpl (%rsi), %edx
jae 0x5699
movl %eax, -0x34(%rbp)
testl %r14d, %r14d
sete %dl
je 0x53a2
movl %eax, -0x34(%rbp)
cmpl %r14d, %ecx
jl 0x53a2
jmp 0x555d
movl $0x0, -0x30(%rbp)
testl %r13d, %r13d
je 0x54ec
movq %r12, -0x68(%rbp)
leal -0x1(%r13), %eax
addq %rbx, %rax
incq %rax
movq %rax, -0x80(%rbp)
leal (%r15,%r13), %eax
movl %eax, -0x54(%rbp)
movl %r13d, %r12d
movq %rbx, %rdi
callq 0x49cb
cmpl $-0x2, %eax
setne %al
jne 0x54f3
incq %rbx
incl %r15d
decl %r12d
jne 0x54bb
movq -0x80(%rbp), %rbx
movl -0x54(%rbp), %ecx
movl %ecx, %r15d
movq -0x68(%rbp), %r12
movq %r14, %rcx
jmp 0x53e1
xorl %eax, %eax
jmp 0x53e1
movq %r14, %rcx
addl %r12d, %ecx
movl $0x1, -0x30(%rbp)
movq -0x68(%rbp), %r12
jmp 0x53e1
leaq 0x5c9b(%rip), %rdi      # 0xb1ab
leaq 0x5f53(%rip), %rsi      # 0xb46a
leaq 0x5cb0(%rip), %rdx      # 0xb1ce
movl $0x45b, %ecx            # imm = 0x45B
xorl %eax, %eax
callq 0x3080
leaq 0x5f92(%rip), %rdi      # 0xb4c3
callq 0x3680
leaq 0x5fa1(%rip), %rdi      # 0xb4de
callq 0x3680
movq %r12, %rdi
callq 0x3500
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x3790
jmp 0x56f7
movq -0x40(%rbp), %rdi
movq %rcx, %r14
movl $0x2, %esi
callq 0x3790
testl %r14d, %r14d
jle 0x56ef
subl $-0x80, %r14d
movl $0x1, %edi
movq %r14, %rsi
callq 0x32b0
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3250
leaq -0x48(%rbp), %r8
movq %rbx, %rdi
movl %r13d, %esi
movq %r15, %r13
movq %r15, %rdx
movl %r14d, %ecx
movq %r8, %r14
callq 0x4ac2
movl %eax, %ebx
cmpl $-0x10, %eax
sete %al
movl (%r14), %ecx
movl %ecx, %r15d
testl %ecx, %ecx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x570d
movq -0x40(%rbp), %rdi
movq %r13, %rsi
movl %r15d, %edx
movl $0x1, %ecx
movl $0xfffffff1, %r8d       # imm = 0xFFFFFFF1
callq 0x529c
leaq 0x5bb9(%rip), %rdi      # 0xb1ab
leaq 0x5e71(%rip), %rsi      # 0xb46a
leaq 0x5bce(%rip), %rdx      # 0xb1ce
movl $0x49a, %ecx            # imm = 0x49A
xorl %eax, %eax
callq 0x3080
leaq 0x5e69(%rip), %rdi      # 0xb47c
movl %r15d, %esi
movq %r13, %rdx
xorl %eax, %eax
callq 0x3080
leaq 0x5eb7(%rip), %rdi      # 0xb4de
callq 0x3680
movq %r12, %rdi
callq 0x3500
movq %r13, %rdi
callq 0x3500
xorl %ebx, %ebx
jmp 0x56fc
leaq 0x5b61(%rip), %rdi      # 0xb1ab
leaq 0x5e19(%rip), %rsi      # 0xb46a
leaq 0x5b76(%rip), %rdx      # 0xb1ce
movl $0x473, %ecx            # imm = 0x473
xorl %eax, %eax
callq 0x3080
leaq 0x5e94(%rip), %rdi      # 0xb4ff
callq 0x3680
leaq 0x5e67(%rip), %rdi      # 0xb4de
callq 0x3680
movq %r12, %rdi
callq 0x3500
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x3790
movl $0xfffffffb, %ebx       # imm = 0xFFFFFFFB
jmp 0x56fc
leaq 0x5b0b(%rip), %rdi      # 0xb1ab
leaq 0x5dc3(%rip), %rsi      # 0xb46a
leaq 0x5b20(%rip), %rdx      # 0xb1ce
movl $0x487, %ecx            # imm = 0x487
xorl %eax, %eax
callq 0x3080
leaq 0x5e1f(%rip), %rdi      # 0xb4e0
callq 0x3680
leaq 0x5e11(%rip), %rdi      # 0xb4de
callq 0x3680
movq %r12, %rdi
callq 0x3500
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x3790
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jmp 0x56fc
movq %r12, %rdi
callq 0x3500
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r15, %r15
je 0x575f
leal 0x14(%rbx), %eax
cmpl $0x2, %eax
ja 0x575f
movq %r15, %rdi
callq 0x35a0
movq -0x78(%rbp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x579a
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3350
movq %r12, %rdi
callq 0x3500
movq %r13, %rdi
callq 0x3500
movl -0x48(%rbp), %eax
movq -0x70(%rbp), %rcx
movl %eax, (%rcx)
movq -0x60(%rbp), %rcx
movl %ebx, (%rcx)
movl %eax, %ebx
jmp 0x56fc
testq %r15, %r15
sete %r15b
cmpl $-0x11, %ebx
movq %r12, %rdi
setne %r12b
callq 0x3500
movq %r13, %rdi
callq 0x3500
orb %r15b, %r12b
jne 0x56fc
movq -0x60(%rbp), %rax
movl $0xffffffef, (%rax)     # imm = 0xFFFFFFEF
movl $0xffffffef, %ebx       # imm = 0xFFFFFFEF
jmp 0x56fc
movq %r12, %rdi
callq 0x3500
movq %r13, %rdi
callq 0x3500
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
jmp 0x56fc