// File: /workspace/github_cpp_makefile_star2_O2/wenzhelu[P]RUDP/Listener.cpp
// Function: Listener::recAns()
void Listener::recAns()
{
    debug_print("recAns is invoked\n", nullptr);
    string s = "./tm0.pdf";
    fstream fs;
    
    while (!master->close) {
        
        fs.open(s, ios::trunc | ios::out);
        while(1)
        {
            this_thread::sleep_for(ms(25)); // sleep for a while
            int recbits;
            char rec[1472];
            //        char databuf[1460];
            bool databit=false;
            bool ackbit=false;
            recbits=master->sock->read(rec,1472);
            //this_thread::sleep_for(ms(50));
            //    printf("%d bytes received\n",recbits);
            //    printf("sequence num: %u \n",*(uint*)rec);
            if(rec[Listener::control]>>7)
                databit=true;
            if((rec[Listener::control]>>6)&1)
                ackbit=true;
            if (rec[8] & 0x20) {
                // this is a fin packet we are not dropping
                s[4]++;
                fs.close();
                debug_print("Received new file!\n", nullptr);
                //                debug_print("RTT time: %u ms\n",this->eRTT);
                break;
            }
            
            if(!randomdrop(0.1))
            {
                debug_print("Not dropping this packet\n", nullptr);
                if(ackbit)
                {
                    //            printf("ack exist\n");
                    getTimeout(*(uint*)(rec+4));
                    this->update(*(uint*)(rec+4));
                }
                if(databit)
                {
                    //            printf("data exist\n");
                    if(this->totalrec!=*(uint*)rec)
                    {
                        *(uint*)(master->buff+4) = this->totalrec;
                    }
                    else
                    {
                        *(uint*)(master->buff+4) = *(uint*)rec+recbits-12;
                        this->totalrec += (recbits - 12);
                        char *data = rec + 12;
                        fs.write(data, recbits - 12);
                        fs.flush();
                        //                    this->totalrec += (recbits-12);
                    }
                    master->buff[Listener::control]|=1<<6;
                    //
                    //                strncpy(databuf,rec+12,1460);
                    //            printf("%s\n\n",databuf);
                }
            } else {
                debug_print("dropping this packet\n", nullptr);
            }
            master->sock->printPacket(true,rec,(uint)recbits);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x7f8, %rsp            # imm = 0x7F8
movq %rdi, %rbx
leaq 0x1a46(%rip), %rsi      # 0x4038
leaq -0x48(%rbp), %rdi
leaq -0x820(%rbp), %rdx
callq 0x2100
leaq -0x258(%rbp), %rdi
callq 0x2290
leaq -0x820(%rbp), %r13
pushq $0x1
popq %r12
movq 0x20(%rbx), %rax
cmpb $0x0, 0x620(%rax)
jne 0x273b
leaq -0x258(%rbp), %rdi
leaq -0x48(%rbp), %rsi
pushq $0x30
popq %rdx
callq 0x22e0
movq $0x19, -0x820(%rbp)
movq %r13, %rdi
callq 0x29a5
movq 0x20(%rbx), %rax
movq 0x628(%rax), %rdi
movl $0x5c0, %edx            # imm = 0x5C0
movq %r13, %rsi
callq 0x38d4
movb -0x818(%rbp), %r15b
testb $0x20, %r15b
jne 0x2723
movq %rax, %r14
movsd 0x19a4(%rip), %xmm0     # 0x4028
callq 0x2790
testb %al, %al
jne 0x2705
testb $0x40, %r15b
je 0x26af
movl -0x81c(%rbp), %esi
movq %rbx, %rdi
callq 0x27c6
movl -0x81c(%rbp), %esi
movq %rbx, %rdi
callq 0x28aa
testb %r15b, %r15b
jns 0x2705
movl 0x28(%rbx), %eax
cmpl -0x820(%rbp), %eax
jne 0x26fa
leal -0xc(%r14), %ecx
addl %r14d, %eax
addl $-0xc, %eax
movq 0x20(%rbx), %rdx
movl %eax, 0x54(%rdx)
addl %ecx, 0x28(%rbx)
movslq %ecx, %rdx
leaq -0x248(%rbp), %r15
movq %r15, %rdi
leaq -0x814(%rbp), %rsi
callq 0x22c0
movq %r15, %rdi
callq 0x2180
movq 0x20(%rbx), %rcx
jmp 0x2701
movq 0x20(%rbx), %rcx
movl %eax, 0x54(%rcx)
orb $0x40, 0x58(%rcx)
movq 0x20(%rbx), %rax
movq 0x628(%rax), %rdi
movl %r12d, %esi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x390e
jmp 0x263d
movq -0x48(%rbp), %rax
incb 0x4(%rax)
leaq -0x258(%rbp), %rdi
callq 0x2060
jmp 0x2619
leaq -0x258(%rbp), %rdi
callq 0x20a0
leaq -0x48(%rbp), %rdi
callq 0x2120
addq $0x7f8, %rsp            # imm = 0x7F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x277e
jmp 0x276f
jmp 0x276f
jmp 0x276f
jmp 0x276f
movq %rax, %rbx
leaq -0x258(%rbp), %rdi
callq 0x20a0
leaq -0x48(%rbp), %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x2370
nop