// File: maclellant[P]ReliableGoBackN/server.cpp
// Function: send_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, sockaddr_in, GremlinInfo&)
bool send_file(std::string filename, int sockfd, struct sockaddr_in client_addr, GremlinInfo& info)
{
    socklen_t slen = sizeof(client_addr);

    vector<Packet> packets;
    vector<Timer> timers;
    parse_file(filename, packets, timers);

    int window_end = packets.size();
    int window_base = 0;
    int current = 0;
    int timeout_counter = 0;

    vector<Packet> delay_packets;
    vector<Timer> delay_timers;

    while (window_base < window_end)
    {
        // Check on the delayed packets
        if (!delay_timers.empty())
        {
            for (int i = 0; i < delay_timers.size(); ++i)
            {
                if (delay_timers[i].timeout(info.delay_amount_ms))
                {
                    std::cout << "SENDING: sequence " << (int)delay_packets[i].sequence() << "\n";
                    std::cout << "DATA:\n";
                    std::cout << packet_string(delay_packets[i], 48);
                    std::cout << "\n\n";
                    if (sendto(sockfd, delay_packets[i].buffer, PACKET_SIZE, 0, (struct sockaddr*) &client_addr, slen) == -1)
                    {
                        std::cerr << "Error: could not send packet to client" << std::endl;
                        close(sockfd);
                        exit(EXIT_FAILURE);
                    }

                    delay_timers.erase(delay_timers.begin() + i);
                    delay_packets.erase(delay_packets.begin() + i);
                    break;
                }
            }
        }
        else if (current < (window_base + WINDOW_SIZE) && current < window_end)
        {
            Packet temp = packets[current];
            int result = send_packet(sockfd, client_addr, temp, info);
            if (result == DELAYED)
            {
                Timer delay_timer = Timer();
                delay_timer.start();
                delay_timers.push_back(delay_timer);
                delay_packets.push_back(temp);
            }
            timers[current].start();
            current++;
        }
        else if (timers[window_base].timeout(SERVER_TIMEOUT_MSEC))
        {
            std::cout << "TIMEOUT: Retransmitting current window\n\n";
            current = window_base;
            timeout_counter++;
            if (timeout_counter > SERVER_CANCEL_TIMEOUT_COUNT)
            {
                return false;
            }
        }

        Packet received;
        struct sockaddr_in client_rec;
        socklen_t slen_rec = sizeof(client_addr);

        int receiver = recvfrom(sockfd, received.buffer, PACKET_SIZE, 0, (struct sockaddr*)&client_rec, &slen_rec);
        if (receiver < 0)
        {          
            if (errno == EWOULDBLOCK)
            {
                errno = 0;
            }
            else
            {
                std::cerr << "Error: Could not receive from client" << std::endl;
                close(sockfd);
                exit(EXIT_FAILURE);
            }
        }
        else if (receiver > 0)
        {
            timeout_counter = 0;

            if (received.type() == ACK)
            {
                std::cout << "ACKNOLEDGE: sequence " << (int) received.sequence() << "\n\n";
                if (received.sequence() == ((window_base + 1) % SEQ_NUM))
                {
                    window_base++;
                    if (current < window_base)
                        current = window_base;
                }             
            }
            else if (received.type() == NAK)
            {
                std::cout << "DAMAGED DATA: sequence " << (int) received.sequence() << "\n\n";
                current = window_base;
            }
        }
    }

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x660, %rsp            # imm = 0x660
movq %rdi, -0x588(%rbp)
movq %rdx, -0x14(%rbp)
movq %rcx, -0xc(%rbp)
movq %rdi, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movl $0x10, -0x34(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0x580(%rbp)
callq 0x3d60
leaq -0x68(%rbp), %rdi
movq %rdi, -0x578(%rbp)
callq 0x3d80
movq -0x588(%rbp), %rdi
movq -0x580(%rbp), %rsi
movq -0x578(%rbp), %rdx
callq 0x34c0
jmp 0x2ba8
leaq -0x50(%rbp), %rdi
callq 0x3f40
movl %eax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3d60
leaq -0xb8(%rbp), %rdi
callq 0x3d80
movl -0x7c(%rbp), %eax
cmpl -0x78(%rbp), %eax
jge 0x32e9
leaq -0xb8(%rbp), %rdi
callq 0x3f60
testb $0x1, %al
jne 0x2f30
movl $0x0, -0xbc(%rbp)
movslq -0xbc(%rbp), %rax
movq %rax, -0x590(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x3fa0
movq %rax, %rcx
movq -0x590(%rbp), %rax
cmpq %rcx, %rax
jae 0x2f2b
movslq -0xbc(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x3fc0
movq %rax, %rdi
movq -0x30(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x3fe0
movb %al, -0x591(%rbp)
jmp 0x2c65
movb -0x591(%rbp), %al
testb $0x1, %al
jne 0x2c74
jmp 0x2f15
movq 0x834d(%rip), %rdi      # 0xafc8
leaq 0x44ee(%rip), %rsi      # 0x7170
callq 0x2220
movq %rax, -0x5a0(%rbp)
jmp 0x2c90
movslq -0xbc(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x4050
movq %rax, %rdi
callq 0x4070
movb %al, -0x5a1(%rbp)
jmp 0x2cb3
movq -0x5a0(%rbp), %rdi
movb -0x5a1(%rbp), %al
movzbl %al, %esi
callq 0x2370
movq %rax, -0x5b0(%rbp)
jmp 0x2cd1
movq -0x5b0(%rbp), %rdi
leaq 0x4449(%rip), %rsi      # 0x7128
callq 0x2220
jmp 0x2ce6
movq 0x82db(%rip), %rdi      # 0xafc8
leaq 0x448f(%rip), %rsi      # 0x7183
callq 0x2220
jmp 0x2cfb
movslq -0xbc(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x4050
movq %rax, %rsi
leaq -0xe0(%rbp), %rdi
movl $0x30, %edx
callq 0x26e0
jmp 0x2d24
movq 0x829d(%rip), %rdi      # 0xafc8
leaq -0xe0(%rbp), %rsi
callq 0x2200
jmp 0x2d39
leaq -0xe0(%rbp), %rdi
callq 0x2150
movq 0x827c(%rip), %rdi      # 0xafc8
leaq 0x43d4(%rip), %rsi      # 0x7127
callq 0x2220
jmp 0x2d5a
movl -0x24(%rbp), %eax
movl %eax, -0x5bc(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x4050
movl -0x5bc(%rbp), %edi
movq %rax, %rsi
movl -0x34(%rbp), %r9d
movl $0x200, %edx            # imm = 0x200
xorl %ecx, %ecx
leaq -0x14(%rbp), %r8
callq 0x2060
movq %rax, -0x5b8(%rbp)
jmp 0x2d9c
movq -0x5b8(%rbp), %rax
cmpq $-0x1, %rax
jne 0x2e31
movq 0x823c(%rip), %rdi      # 0xaff0
leaq 0x43cf(%rip), %rsi      # 0x718a
callq 0x2220
movq %rax, -0x5c8(%rbp)
jmp 0x2dc9
movq -0x5c8(%rbp), %rdi
movq 0x81e1(%rip), %rsi      # 0xafb8
callq 0x2260
jmp 0x2dde
movl -0x24(%rbp), %edi
callq 0x23b0
jmp 0x2de8
movl $0x1, %edi
callq 0x22c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x3347
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x332f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x2150
jmp 0x332f
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x5d8(%rbp)
callq 0x4100
movq %rax, -0xf8(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x4130
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x4170
movq -0x5d8(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x4090
movq %rax, -0x5d0(%rbp)
jmp 0x2e94
movq -0x5d0(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x5e8(%rbp)
callq 0x4220
movq %rax, -0x118(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x4250
movq %rax, -0x110(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x4290
movq -0x5e8(%rbp), %rdi
movq -0x108(%rbp), %rsi
callq 0x41b0
movq %rax, -0x5e0(%rbp)
jmp 0x2f05
movq -0x5e0(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0x2f2b
jmp 0x2f17
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x2c0e
jmp 0x309e
movl -0x80(%rbp), %eax
movl -0x7c(%rbp), %ecx
addl $0x10, %ecx
cmpl %ecx, %eax
jge 0x3024
movl -0x80(%rbp), %eax
cmpl -0x78(%rbp), %eax
jge 0x3024
movslq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4050
movq %rax, %rsi
leaq -0x320(%rbp), %rdi
movq %rdi, -0x5f8(%rbp)
movl $0x200, %edx            # imm = 0x200
callq 0x21c0
movq -0x5f8(%rbp), %rcx
movl -0x24(%rbp), %edi
movups -0x14(%rbp), %xmm0
movaps %xmm0, -0x340(%rbp)
movq -0x30(%rbp), %r8
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
callq 0x36b0
movl %eax, -0x5ec(%rbp)
jmp 0x2fa9
movl -0x5ec(%rbp), %eax
movl %eax, -0x324(%rbp)
cmpl $0x2, -0x324(%rbp)
jne 0x3002
xorps %xmm0, %xmm0
movaps %xmm0, -0x350(%rbp)
leaq -0x350(%rbp), %rdi
callq 0x42d0
jmp 0x2fd6
leaq -0xb8(%rbp), %rdi
leaq -0x350(%rbp), %rsi
callq 0x3ed0
jmp 0x2feb
leaq -0xa0(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x3e60
jmp 0x3000
jmp 0x3002
movslq -0x80(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x3fc0
movq %rax, %rdi
callq 0x42d0
jmp 0x3019
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x309c
movslq -0x7c(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x3fc0
movq %rax, %rdi
movl $0xa, %esi
callq 0x3fe0
movb %al, -0x5f9(%rbp)
jmp 0x3046
movb -0x5f9(%rbp), %al
testb $0x1, %al
jne 0x3052
jmp 0x309a
movq 0x7f6f(%rip), %rdi      # 0xafc8
leaq 0x4151(%rip), %rsi      # 0x71b1
callq 0x2220
jmp 0x3067
movl -0x7c(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
cmpl $0xa, -0x84(%rbp)
jle 0x3098
movb $0x0, -0x1(%rbp)
movl $0x1, -0x354(%rbp)
jmp 0x32f7
jmp 0x309a
jmp 0x309c
jmp 0x309e
leaq -0x554(%rbp), %rdi
callq 0x6170
jmp 0x30ac
movl $0x10, -0x568(%rbp)
movl -0x24(%rbp), %edi
leaq -0x554(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
xorl %ecx, %ecx
leaq -0x564(%rbp), %r8
leaq -0x568(%rbp), %r9
callq 0x2190
movq %rax, -0x608(%rbp)
jmp 0x30e3
movq -0x608(%rbp), %rax
movl %eax, -0x56c(%rbp)
cmpl $0x0, -0x56c(%rbp)
jge 0x315a
callq 0x2030
cmpl $0xb, (%rax)
jne 0x3110
callq 0x2030
movl $0x0, (%rax)
jmp 0x3155
movq 0x7ed9(%rip), %rdi      # 0xaff0
leaq 0x40bc(%rip), %rsi      # 0x71da
callq 0x2220
movq %rax, -0x610(%rbp)
jmp 0x312c
movq -0x610(%rbp), %rdi
movq 0x7e7e(%rip), %rsi      # 0xafb8
callq 0x2260
jmp 0x3141
movl -0x24(%rbp), %edi
callq 0x23b0
jmp 0x314b
movl $0x1, %edi
callq 0x22c0
jmp 0x32e4
cmpl $0x0, -0x56c(%rbp)
jle 0x32e2
movl $0x0, -0x84(%rbp)
leaq -0x554(%rbp), %rdi
callq 0x3d20
movb %al, -0x611(%rbp)
jmp 0x3185
movb -0x611(%rbp), %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x3253
movq 0x7e2a(%rip), %rdi      # 0xafc8
leaq 0x405a(%rip), %rsi      # 0x71ff
callq 0x2220
movq %rax, -0x620(%rbp)
jmp 0x31b3
leaq -0x554(%rbp), %rdi
callq 0x4070
movb %al, -0x621(%rbp)
jmp 0x31c7
movq -0x620(%rbp), %rdi
movb -0x621(%rbp), %al
movzbl %al, %esi
callq 0x2370
movq %rax, -0x630(%rbp)
jmp 0x31e5
movq -0x630(%rbp), %rdi
leaq 0x3f34(%rip), %rsi      # 0x7127
callq 0x2220
jmp 0x31fa
leaq -0x554(%rbp), %rdi
callq 0x4070
movb %al, -0x631(%rbp)
jmp 0x320e
movb -0x631(%rbp), %al
movzbl %al, %eax
movl %eax, -0x638(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl -0x638(%rbp), %eax
cmpl %edx, %eax
jne 0x324e
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jge 0x324c
movl -0x7c(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x324e
jmp 0x32e0
leaq -0x554(%rbp), %rdi
callq 0x3d20
movb %al, -0x639(%rbp)
jmp 0x3267
movb -0x639(%rbp), %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x32de
movq 0x7d4c(%rip), %rdi      # 0xafc8
leaq 0x3f92(%rip), %rsi      # 0x7215
callq 0x2220
movq %rax, -0x648(%rbp)
jmp 0x3291
leaq -0x554(%rbp), %rdi
callq 0x4070
movb %al, -0x649(%rbp)
jmp 0x32a5
movq -0x648(%rbp), %rdi
movb -0x649(%rbp), %al
movzbl %al, %esi
callq 0x2370
movq %rax, -0x658(%rbp)
jmp 0x32c3
movq -0x658(%rbp), %rdi
leaq 0x3e56(%rip), %rsi      # 0x7127
callq 0x2220
jmp 0x32d8
movl -0x7c(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x32e0
jmp 0x32e2
jmp 0x32e4
jmp 0x2be4
movb $0x1, -0x1(%rbp)
movl $0x1, -0x354(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x3da0
leaq -0xa0(%rbp), %rdi
callq 0x3e00
leaq -0x68(%rbp), %rdi
callq 0x3da0
leaq -0x50(%rbp), %rdi
callq 0x3e00
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x660, %rsp            # imm = 0x660
popq %rbp
retq
leaq -0xb8(%rbp), %rdi
callq 0x3da0
leaq -0xa0(%rbp), %rdi
callq 0x3e00
leaq -0x68(%rbp), %rdi
callq 0x3da0
leaq -0x50(%rbp), %rdi
callq 0x3e00
movq -0x70(%rbp), %rdi
callq 0x2380
nopw %cs:(%rax,%rax)