// File: vianna274[P]dropbox/src/WrapperSocket.cpp
// Function: Dropbox::WrapperSocket::receive(int, int)
MessageData* WrapperSocket::receive(int timeout, int time) {
  if(timeout == TIMEOUT_ON) {
    struct pollfd fd;
    fd.fd = this->localSocketHandler;
    fd.events = POLLIN;
    int ret = poll(&fd, 1, time);
    switch(ret) {
      case -1: printf("Error\n");
        return NULL;
      case 0: 
        return NULL;
      default: break;
    }
  }

  char* msg = new char[PACKET_LEN];
  memset(msg, 0, PACKET_LEN);
  bool receivedCorrectly = false;
  MessageData * data;
  while(!receivedCorrectly) {

    int msgSize = recvfrom(this->localSocketHandler, (void *) msg, 
      PACKET_LEN, 0, (struct sockaddr *) &this->remoteSocketAddr, &this->remoteSocketLen);
    if (msgSize < 0) {
      fprintf(stderr, "Error on receiving\n");
      exit(1);
    }
    data = (MessageData *) msg;

    if (data->socketSeq == -1)
      break;
    if (data->socketSeq == 0)
      this->socketSeq = 0;
    if (data->socketSeq == this->socketSeq && data->type != TYPE_ACK) {
      MessageData message = make_packet(TYPE_ACK, data->seq, 1, -1, "");
      this->sendAck(&message);
      this->socketSeq = data->socketSeq + 1;
      receivedCorrectly = true;
    }
  }

  
  // TODO delete msg
  return data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1030, %rsp           # imm = 0x1030
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1020(%rbp)
cmpl $0x1, -0x14(%rbp)
jne 0x17c54
movq -0x1020(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movw $0x1, -0x1c(%rbp)
movl -0x18(%rbp), %edx
leaq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x3530
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x1024(%rbp)
subl $-0x1, %eax
je 0x17c28
jmp 0x17c1c
movl -0x1024(%rbp), %eax
testl %eax, %eax
je 0x17c43
jmp 0x17c50
leaq 0x1a24(%rip), %rdi      # 0x19653
movb $0x0, %al
callq 0x3080
movq $0x0, -0x8(%rbp)
jmp 0x17d7e
movq $0x0, -0x8(%rbp)
jmp 0x17d7e
jmp 0x17c52
jmp 0x17c54
movl $0x1000, %edi           # imm = 0x1000
callq 0x3030
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3240
movb $0x0, -0x31(%rbp)
movb -0x31(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x17c84
jmp 0x17d76
movq -0x1020(%rbp), %r9
movl (%r9), %edi
movq -0x30(%rbp), %rsi
movq %r9, %r8
addq $0x20, %r8
addq $0x40, %r9
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0x32d0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0x17cd4
movq 0xb317(%rip), %rax      # 0x22fd0
movq (%rax), %rdi
leaq 0x197c(%rip), %rsi      # 0x1963f
movb $0x0, %al
callq 0x34b0
movl $0x1, %edi
callq 0x3420
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpl $-0x1, 0x10(%rax)
jne 0x17ceb
jmp 0x17d76
movq -0x40(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x17d03
movq -0x1020(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x1020(%rbp), %rcx
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
cmpl 0x8(%rcx), %eax
jne 0x17d71
movq -0x40(%rbp), %rax
cmpl $0x1, (%rax)
je 0x17d71
movq -0x40(%rbp), %rax
movl 0x4(%rax), %edx
leaq -0x1018(%rbp), %rdi
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
leaq 0x1913(%rip), %r9       # 0x19652
movl %ecx, %esi
callq 0x97a0
movq -0x1020(%rbp), %rdi
leaq -0x1018(%rbp), %rsi
callq 0x18580
movq -0x1020(%rbp), %rax
movq -0x40(%rbp), %rcx
movl 0x10(%rcx), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movb $0x1, -0x31(%rbp)
jmp 0x17c76
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1030, %rsp           # imm = 0x1030
popq %rbp
retq
nopl (%rax,%rax)