// File: 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
subq $0x860, %rsp            # imm = 0x860
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x838(%rbp)
jmp 0x2b0c
leaq -0x29(%rbp), %rdi
movq %rdi, -0x840(%rbp)
callq 0x23b0
movq -0x840(%rbp), %rdx
leaq 0x550e(%rip), %rsi      # 0x8038
leaq -0x28(%rbp), %rdi
callq 0x2300
jmp 0x2b35
leaq -0x29(%rbp), %rdi
callq 0x2290
leaq -0x250(%rbp), %rdi
callq 0x22b0
jmp 0x2b4c
jmp 0x2b4e
movq -0x838(%rbp), %rax
movq 0x20(%rax), %rax
movb 0x620(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2b6a
jmp 0x2e36
movl $0x20, %edi
movl $0x10, %esi
callq 0x3450
movl %eax, -0x844(%rbp)
jmp 0x2b81
movl -0x844(%rbp), %edx
leaq -0x250(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2310
jmp 0x2b99
jmp 0x2b9b
jmp 0x2b9d
movl $0x19, -0x25c(%rbp)
leaq -0x258(%rbp), %rdi
leaq -0x25c(%rbp), %rsi
callq 0x3520
jmp 0x2bbc
leaq -0x258(%rbp), %rdi
callq 0x3470
jmp 0x2bca
movq -0x838(%rbp), %rax
movb $0x0, -0x821(%rbp)
movb $0x0, -0x822(%rbp)
movq 0x20(%rax), %rax
movq 0x628(%rax), %rdi
leaq -0x820(%rbp), %rsi
movl $0x5c0, %edx            # imm = 0x5C0
callq 0x6f30
movq %rax, -0x850(%rbp)
jmp 0x2c04
movq -0x850(%rbp), %rax
movl %eax, -0x260(%rbp)
movsbl -0x818(%rbp), %eax
sarl $0x7, %eax
cmpl $0x0, %eax
je 0x2c71
movb $0x1, -0x821(%rbp)
jmp 0x2c71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x2290
jmp 0x2e5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x2e54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x20c0
jmp 0x2e54
movsbl -0x818(%rbp), %eax
sarl $0x6, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2c8a
movb $0x1, -0x822(%rbp)
movsbl -0x818(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x2cd6
leaq -0x28(%rbp), %rdi
movl $0x4, %esi
callq 0x23e0
movq %rax, -0x858(%rbp)
jmp 0x2cb0
movq -0x858(%rbp), %rax
movb (%rax), %cl
incb %cl
movb %cl, (%rax)
leaq -0x250(%rbp), %rdi
callq 0x2080
jmp 0x2ccb
jmp 0x2ccd
jmp 0x2ccf
jmp 0x2cd1
jmp 0x2e31
movq -0x838(%rbp), %rdi
movsd 0x5343(%rip), %xmm0     # 0x8028
callq 0x2e70
movb %al, -0x859(%rbp)
jmp 0x2cf2
movb -0x859(%rbp), %al
testb $0x1, %al
jne 0x2df9
jmp 0x2d02
jmp 0x2d04
jmp 0x2d06
jmp 0x2d08
testb $0x1, -0x822(%rbp)
je 0x2d3b
movq -0x838(%rbp), %rdi
movl -0x81c(%rbp), %esi
callq 0x2eb0
jmp 0x2d25
movq -0x838(%rbp), %rdi
movl -0x81c(%rbp), %esi
callq 0x3220
jmp 0x2d39
jmp 0x2d3b
testb $0x1, -0x821(%rbp)
je 0x2df7
movq -0x838(%rbp), %rax
movl 0x28(%rax), %eax
cmpl -0x820(%rbp), %eax
je 0x2d6d
movq -0x838(%rbp), %rax
movl 0x28(%rax), %ecx
movq 0x20(%rax), %rax
movl %ecx, 0x54(%rax)
jmp 0x2de2
movq -0x838(%rbp), %rax
movl -0x820(%rbp), %esi
movl -0x260(%rbp), %ecx
movl %ecx, %edx
movl %esi, %ecx
leal -0xc(%rcx,%rdx), %edx
movq 0x20(%rax), %rcx
movl %edx, 0x54(%rcx)
movl -0x260(%rbp), %esi
movl 0x28(%rax), %ecx
movl %ecx, %edx
movl %esi, %ecx
leal -0xc(%rcx,%rdx), %ecx
movl %ecx, 0x28(%rax)
leaq -0x814(%rbp), %rax
movq %rax, -0x830(%rbp)
leaq -0x240(%rbp), %rdi
movq -0x830(%rbp), %rsi
movl -0x260(%rbp), %eax
addl $-0xc, %eax
movslq %eax, %rdx
callq 0x22e0
jmp 0x2dd2
leaq -0x240(%rbp), %rdi
callq 0x2180
jmp 0x2de0
jmp 0x2de2
movq -0x838(%rbp), %rax
movq 0x20(%rax), %rax
movsbl 0x58(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x58(%rax)
jmp 0x2e01
jmp 0x2dfb
jmp 0x2dfd
jmp 0x2dff
jmp 0x2e01
movq -0x838(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x628(%rax), %rdi
movl -0x260(%rbp), %ecx
movl $0x1, %esi
leaq -0x820(%rbp), %rdx
callq 0x6ff0
jmp 0x2e2c
jmp 0x2b9b
jmp 0x2b4e
leaq -0x250(%rbp), %rdi
callq 0x20c0
leaq -0x28(%rbp), %rdi
callq 0x2160
addq $0x860, %rsp            # imm = 0x860
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x2160
movq -0x38(%rbp), %rdi
callq 0x23a0
nopw %cs:(%rax,%rax)