// File: maclellant[P]ReliableGoBackN/server.cpp
// Function: receive_commands(int, GremlinInfo&)
void receive_commands(int sockfd, GremlinInfo& info)
{
    Packet packet;
    struct sockaddr_in client_addr;
    socklen_t slen = sizeof(client_addr);

    std::cout << "Waiting for client connection...\n\n";

    while (1)
    {

        int receiver = recvfrom(sockfd, packet.buffer, PACKET_SIZE, 0, (struct sockaddr*)&client_addr, &slen);
        if (receiver < 0)
        {          
            if (errno == EWOULDBLOCK)
            {
                errno = 0;
                continue;
            }
            else
            {
                std::cerr << "Error: Could not receive from client\n\n";
                close(sockfd);
                exit(EXIT_FAILURE);
            }
        }
        else if (receiver == 0)
        {
            continue;
        }
        else
        {
            if (packet.type() == GET)
            {
                std::string filename = packet_string(packet);

                if (filename.empty())
                {
                    std::cout << "Warning: Received invalid filename request: Discarding\n\n";
                }
                else if (filename == SUCCESS_MSG)
                {
                    std::cout << "Warning: Received success message: Discarding\n\n";
                }
                else
                {
                    std::cout << "Received GET request from client\n\n";
                    vector<Packet> packets;
                    vector<Timer> timers;
                    if (!send_file(filename, sockfd, client_addr, info))
                    {
                        std::cout << "ERROR: Client stopped responding. Ending connection...\n\n";
                        return;
                    }
                    receive_success_msg(sockfd, client_addr);

                    std::cout << "Waiting for client connection...\n\n";
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x6170
movl $0x10, -0x224(%rbp)
movq 0x8799(%rip), %rdi      # 0xafc8
leaq 0x47ce(%rip), %rsi      # 0x7004
callq 0x2220
movl -0x4(%rbp), %edi
leaq -0x210(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
xorl %ecx, %ecx
leaq -0x220(%rbp), %r8
leaq -0x224(%rbp), %r9
callq 0x2190
movl %eax, -0x228(%rbp)
cmpl $0x0, -0x228(%rbp)
jge 0x28aa
callq 0x2030
cmpl $0xb, (%rax)
jne 0x2885
callq 0x2030
movl $0x0, (%rax)
jmp 0x283b
movq 0x8764(%rip), %rdi      # 0xaff0
leaq 0x4794(%rip), %rsi      # 0x7027
callq 0x2220
movl -0x4(%rbp), %edi
callq 0x23b0
movl $0x1, %edi
callq 0x22c0
cmpl $0x0, -0x228(%rbp)
jne 0x28b5
jmp 0x283b
leaq -0x210(%rbp), %rdi
callq 0x3d20
movzbl %al, %eax
cmpl $0x2, %eax
jne 0x2b16
leaq -0x248(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x25d0
leaq -0x248(%rbp), %rdi
callq 0x2360
testb $0x1, %al
jne 0x28f2
jmp 0x2923
movq 0x86cf(%rip), %rdi      # 0xafc8
leaq 0x474e(%rip), %rsi      # 0x704e
callq 0x2220
jmp 0x2907
jmp 0x2ae2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
jmp 0x2b08
leaq 0x475d(%rip), %rsi      # 0x7087
leaq -0x248(%rbp), %rdi
callq 0x3d30
movb %al, -0x2e1(%rbp)
jmp 0x293e
movb -0x2e1(%rbp), %al
testb $0x1, %al
jne 0x294a
jmp 0x2964
movq 0x8677(%rip), %rdi      # 0xafc8
leaq 0x4746(%rip), %rsi      # 0x709e
callq 0x2220
jmp 0x295f
jmp 0x2ae0
movq 0x865d(%rip), %rdi      # 0xafc8
leaq 0x475c(%rip), %rsi      # 0x70ce
callq 0x2220
jmp 0x2979
leaq -0x270(%rbp), %rdi
callq 0x3d60
leaq -0x288(%rbp), %rdi
callq 0x3d80
leaq -0x2a8(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x2100
jmp 0x29a6
movl -0x4(%rbp), %esi
movups -0x220(%rbp), %xmm0
movaps %xmm0, -0x2c0(%rbp)
movq -0x10(%rbp), %r8
movq -0x2c0(%rbp), %rdx
movq -0x2b8(%rbp), %rcx
leaq -0x2a8(%rbp), %rdi
callq 0x2b40
movb %al, -0x2e2(%rbp)
jmp 0x29dd
movb -0x2e2(%rbp), %al
xorb $-0x1, %al
movb %al, -0x2e3(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x2150
movb -0x2e3(%rbp), %al
testb $0x1, %al
jne 0x2a03
jmp 0x2a5b
movq 0x85be(%rip), %rdi      # 0xafc8
leaq 0x46e0(%rip), %rsi      # 0x70f1
callq 0x2220
jmp 0x2a18
movl $0x1, -0x2c4(%rbp)
jmp 0x2aa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
jmp 0x2ac6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x2150
jmp 0x2ac6
movl -0x4(%rbp), %edi
movups -0x220(%rbp), %xmm0
movaps %xmm0, -0x2e0(%rbp)
movq -0x2e0(%rbp), %rsi
movq -0x2d8(%rbp), %rdx
callq 0x3370
jmp 0x2a81
movq 0x8540(%rip), %rdi      # 0xafc8
leaq 0x4575(%rip), %rsi      # 0x7004
callq 0x2220
jmp 0x2a96
movl $0x0, -0x2c4(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x3da0
leaq -0x270(%rbp), %rdi
callq 0x3e00
movl -0x2c4(%rbp), %eax
testl %eax, %eax
jne 0x2aec
jmp 0x2ac4
jmp 0x2ae0
leaq -0x288(%rbp), %rdi
callq 0x3da0
leaq -0x270(%rbp), %rdi
callq 0x3e00
jmp 0x2b08
jmp 0x2ae2
movl $0x0, -0x2c4(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x2150
movl -0x2c4(%rbp), %eax
testl %eax, %eax
je 0x2b06
jmp 0x2b04
jmp 0x2b1f
jmp 0x2b16
leaq -0x248(%rbp), %rdi
callq 0x2150
jmp 0x2b28
jmp 0x2b18
jmp 0x2b1a
jmp 0x283b
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
movq -0x250(%rbp), %rdi
callq 0x2380
nopw %cs:(%rax,%rax)