// File: /workspace/github_cpp_makefile_star2_O2/DavieV[P]virtual-usb-printer/server.cc
// Function: run_server(UsbPrinter)
void run_server(UsbPrinter printer) {
  int listenfd = setup_server_socket();
  struct sockaddr_in server = bind_server_socket(listenfd);
  char address[INET_ADDRSTRLEN];

  if (inet_ntop(AF_INET, &server.sin_addr, address, INET_ADDRSTRLEN)) {
    printf("Bound server to address %s:%hu\n", address, server.sin_port);
  } else {
    printf("inet_ntop error : %s\n", strerror(errno));
    exit(1);
  }

  if (listen(listenfd, SOMAXCONN) < 0) {
    printf("listen error : %s\n", strerror(errno));
    exit(1);
  }

  while (1) {
    int connection = accept_connection(listenfd);
    int attached = 0;

    while (1) {
      if (!attached) {
        // Read in the header first in order to determine whether the request is
        // an OP_REQ_DEVLIST or an OP_REQ_IMPORT.
        OP_HEADER request;
        ssize_t received = recv(connection, &request, sizeof(request), 0);
        if (received != sizeof(request)) {
          printf("receive error : %s\n", strerror (errno));
          break;
        }

        request.command = ntohs(request.command);
        printf("Header Packet\n");
        printf("command: 0x%02X\n", request.command);

        // Request is OP_REQ_DEVLIST 
        if (request.command == OP_REQ_DEVLIST_CMD) {
          handle_device_list(printer, connection);
        // Request is OP_REQ_IMPORT
        } else if (request.command == OP_REQ_IMPORT_CMD) {
          if (!handle_attach(printer, connection)) {
            attached = 1;
          }
        }
      } else {
        printf("------------------------------------------------\n");
        printf("handles requests\n");
        USBIP_CMD_SUBMIT command;
        ssize_t received = recv(connection, &command, sizeof(command), 0);
        if (received != sizeof(command)) {
          printf("receive error : %s\n", strerror(errno));
          break;
        }

        unpack_usbip((int *)&command, sizeof(command));
        print_usbip_cmd_submit(command);

        if (command.command == 1) {
          printer.HandleUsbRequest(connection, command);
        }

        // Unlink URB
        if (command.command == 2) {
          printf("####################### Unlink URB %u  (not working!!!)\n",
                 command.transfer_flags);
          // FIXME
          /*
           USBIP_RET_UNLINK ret;
           printf("####################### Unlink URB %u\n",cmd.transfer_flags);
           ret.command=htonl(0x04);
           ret.devid=htonl(cmd.devid);
           ret.direction=htonl(cmd.direction);
           ret.ep=htonl(cmd.ep);
           ret.seqnum=htonl(cmd.seqnum);
           ret.status=htonl(1);

           if (send (sockfd, (char *)&ret, sizeof(USBIP_RET_UNLINK), 0) !=
           sizeof(USBIP_RET_UNLINK))
           {
             printf ("send error : %s \n", strerror (errno));
             exit(-1);
           }
          */
        }

        if (command.command > 2) {
          printf("Unknown USBIP cmd!\n");
          close(connection);
          return;
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
callq 0x2430
movl %eax, %ebp
movl %eax, %edi
callq 0x24a3
leaq 0x5c(%rsp), %rsi
movq %rax, -0x4(%rsi)
movq %rdx, 0x4(%rsi)
pushq $0x2
popq %rdi
leaq 0x40(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x1160
testq %rax, %rax
je 0x2746
movzwl 0x5a(%rsp), %edx
leaq 0xe39(%rip), %rdi       # 0x33ea
leaq 0x40(%rsp), %rsi
xorl %eax, %eax
callq 0x1050
movl %ebp, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1170
testl %eax, %eax
js 0x275b
leaq 0x10(%rsp), %r14
leaq 0xef3(%rip), %r12       # 0x34d0
pushq $0x30
popq %r13
movl %ebp, 0xc(%rsp)
movl %ebp, %edi
callq 0x24f9
movl %eax, %r15d
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x2681
leaq 0xe9f(%rip), %rdi       # 0x349f
callq 0x11e0
movq %r12, %rdi
callq 0x11e0
movl %r15d, %edi
movq %r14, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x10a0
cmpq $0x30, %rax
jne 0x26fd
movq %r14, %rdi
movq %r13, %rsi
callq 0x1732
movq %r14, %rdi
callq 0x1787
movl 0x10(%rsp), %eax
cmpl $0x1, %eax
jne 0x2655
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x1b7e
movl 0x10(%rsp), %eax
cmpl $0x2, %eax
jne 0x2670
movl 0x24(%rsp), %esi
leaq 0xdf3(%rip), %rdi       # 0x3458
xorl %eax, %eax
callq 0x1050
movl 0x10(%rsp), %eax
cmpl $0x2, %eax
jg 0x2723
pushq $0x1
popq %rbp
jmp 0x25f1
movl %r15d, %edi
movq %r14, %rsi
pushq $0x8
popq %rdx
xorl %ecx, %ecx
callq 0x10a0
cmpq $0x8, %rax
jne 0x26fd
rolw $0x8, 0x12(%rsp)
leaq 0xded(%rip), %rdi       # 0x3491
callq 0x11e0
movzwl 0x12(%rsp), %esi
xorl %ebp, %ebp
leaq 0xd90(%rip), %rdi       # 0x3447
xorl %eax, %eax
callq 0x1050
movzwl 0x12(%rsp), %eax
cmpl $0x8003, %eax           # imm = 0x8003
je 0x26e5
cmpl $0x8005, %eax           # imm = 0x8005
jne 0x25f1
movq %rbx, %rdi
movl %r15d, %esi
callq 0x14f4
jmp 0x25f1
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1619
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
jmp 0x25f1
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0xd23(%rip), %rdi       # 0x3433
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
movl 0xc(%rsp), %ebp
jmp 0x25e5
leaq 0xdb7(%rip), %rdi       # 0x34e1
callq 0x11e0
movl %r15d, %edi
callq 0x1230
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0xcb1(%rip), %rdi       # 0x340a
jmp 0x276e
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0xcb2(%rip), %rdi       # 0x3420
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
pushq $0x1
popq %rdi
callq 0x1190