// File: tylerkaraszewski[P]Argyle/Connection.cpp
// Function: Connection::process(short&, short&)
bool Connection::process(short int& pollEvents, short int& pollRevents)
{
  // Do writing first, since we can tell whether this will finish or not, and we don't want to block our writes on
  // infinite reads.
  if ((pollRevents & POLLOUT) && m_isWriting)
  {
    m_currentResponse->read(m_writeBuffer, S_MAX_WRITE_SIZE - m_writeBuffer.size());
    int bytesSent = send(m_socket, m_writeBuffer.c_str(), m_writeBuffer.size(), 0);
    if (bytesSent == -1)
    {
      if (errno == EPIPE)
      {
        return false; // try again, PIPEs happen.
      }
      m_logger.logError("Connection send error(" + Utils::llToString(errno) + "): " + strerror(errno));
    }
    m_writeBuffer = m_writeBuffer.substr(bytesSent);

    bool retval = false; // Assume we've got more to do unless we're done.
    if (m_currentResponse->done() && m_writeBuffer.empty())
    {
      m_logger.logAccess(Utils::llToString(m_currentResponse->getBytesRead()) + "\t" + 
                         Utils::llToString(m_currentResponse->getHttpResponseCode()) + "\t" + 
                         m_currentResponse->getPath() + "\t" + m_currentRequest->getUserAgent());
      if (m_currentRequest->isKeepAlive() && m_currentResponse->isKeepAlive())
      {
        pollEvents = POLLIN; // Ready for the next request.
      }
      else
      {
        pollEvents = 0; // Done with this connection.
        retval = true;
      }
      m_writeBuffer.resize(0);
      m_isWriting = false;
      delete m_currentResponse;
      m_currentResponse = NULL;
      delete m_currentRequest;
      m_currentRequest = NULL;
    }
    return retval;
  }

  // We don't do this while we're writing, because (for one) we want to keep our request object around till we're
  // finished with it, and we don't want to keep a whole list of requests.
  if (pollRevents & POLLIN)
  {
    char buffer[S_MAX_REQUEST_SIZE];
    int bytesRead = recv(m_socket, buffer, S_MAX_REQUEST_SIZE, 0);

    if (bytesRead == -1)
    {
      if (errno == EPIPE)
      {
        return false; // try again, PIPEs happen.
      }
      m_logger.logError("Recv error(" + Utils::llToString(errno) + "): " + strerror(errno));
      pollEvents = 0;
      return true;
    }

    if (bytesRead == 0)
    {
      return true; // EOF, done.
    }

    m_buffer.append(buffer, bytesRead);
    m_currentRequest = new HttpRequest();
    size_t bytesParsed = m_currentRequest->parse(m_buffer);
    if (bytesParsed == 0)
    {
      if (m_buffer.size() >= S_MAX_REQUEST_SIZE)
      {
        m_currentRequest->setHttpError(413);
      }
      else
      {
        return false;
      }
    }
    // proto-if-modified-since support.
    // @TODO: Make this actually do something useful.
    string ims = m_currentRequest->getHeader("if-modified-since");
    if (!ims.empty())
    {
      //m_logger.logError("Debug: parsed '" + ims + "' to: " + Utils::llToString(m_currentRequest->getIfModifiedSince()));
    }

    m_currentResponse = new HttpResponse(*m_currentRequest, m_logger, m_config);
    m_buffer = m_buffer.substr(bytesParsed); // Trim this off our input buffer since we're done reading it.
    pollEvents = POLLOUT; // Now we want to know if we can write.
    m_isWriting = true;
  }
  
  return false;
}

// Assembly:
subq $0x2388, %rsp           # imm = 0x2388
movq %rdi, 0x2378(%rsp)
movq %rsi, 0x2370(%rsp)
movq %rdx, 0x2368(%rsp)
movq 0x2378(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x2368(%rsp), %rax
movswl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x9217
movq 0xa0(%rsp), %rax
testb $0x1, 0x48(%rax)
je 0x9217
movq 0xa0(%rsp), %rdi
movq 0x40(%rdi), %rax
movq %rax, 0x80(%rsp)
movq %rdi, %rax
addq $0x50, %rax
movq %rax, 0x88(%rsp)
addq $0x50, %rdi
callq 0x42a0
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x40000, %edx          # imm = 0x40000
subq %rax, %rdx
callq 0xdb10
movq 0xa0(%rsp), %rdi
movl (%rdi), %eax
movl %eax, 0x94(%rsp)
addq $0x50, %rdi
callq 0x4190
movq 0xa0(%rsp), %rdi
movq %rax, 0x98(%rsp)
addq $0x50, %rdi
callq 0x42a0
movl 0x94(%rsp), %edi
movq 0x98(%rsp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x41e0
movl %eax, 0x2364(%rsp)
cmpl $-0x1, 0x2364(%rsp)
jne 0x8daf
callq 0x4060
cmpl $0x20, (%rax)
jne 0x8c49
movb $0x0, 0x2387(%rsp)
jmp 0x96b2
movq 0xa0(%rsp), %rax
movq 0x70(%rax), %rax
movq %rax, 0x70(%rsp)
callq 0x4060
movslq (%rax), %rsi
leaq 0x22e0(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x10f70
movq 0x78(%rsp), %rdx
leaq 0xa4a8(%rip), %rsi      # 0x13128
leaq 0x2300(%rsp), %rdi
callq 0x6230
jmp 0x8c8f
leaq 0xa37c(%rip), %rdx      # 0x13012
leaq 0x2320(%rsp), %rdi
leaq 0x2300(%rsp), %rsi
callq 0x61e0
jmp 0x8cad
callq 0x4060
movl (%rax), %edi
callq 0x4150
movq %rax, %rdx
leaq 0x2340(%rsp), %rdi
leaq 0x2320(%rsp), %rsi
callq 0x61e0
jmp 0x8cd3
movq 0x70(%rsp), %rdi
leaq 0x2340(%rsp), %rsi
callq 0x10f40
jmp 0x8ce7
leaq 0x2340(%rsp), %rdi
callq 0x4788
leaq 0x2320(%rsp), %rdi
callq 0x4788
leaq 0x2300(%rsp), %rdi
callq 0x4788
leaq 0x22e0(%rsp), %rdi
callq 0x4788
jmp 0x8daf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x8d9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x8d90
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x8d83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
leaq 0x2340(%rsp), %rdi
callq 0x4788
leaq 0x2320(%rsp), %rdi
callq 0x4788
leaq 0x2300(%rsp), %rdi
callq 0x4788
leaq 0x22e0(%rsp), %rdi
callq 0x4788
jmp 0x96c3
movq 0xa0(%rsp), %rsi
addq $0x50, %rsi
movslq 0x2364(%rsp), %rdx
leaq 0x22b0(%rsp), %rdi
movq $-0x1, %rcx
callq 0x43d0
movq 0xa0(%rsp), %rdi
addq $0x50, %rdi
leaq 0x22b0(%rsp), %rsi
callq 0x44d0
leaq 0x22b0(%rsp), %rdi
callq 0x4788
movq 0xa0(%rsp), %rax
movb $0x0, 0x22af(%rsp)
movq 0x40(%rax), %rdi
callq 0xdc20
testb $0x1, %al
jne 0x8e1f
jmp 0x9202
movq 0xa0(%rsp), %rdi
addq $0x50, %rdi
callq 0x4670
testb $0x1, %al
jne 0x8e39
jmp 0x9202
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x70(%rax), %rax
movq %rax, 0x60(%rsp)
callq 0xdcd0
movq %rax, %rsi
leaq 0x21c8(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x10f70
movq 0x68(%rsp), %rsi
leaq 0xa2cb(%rip), %rdx      # 0x1313f
leaq 0x21e8(%rsp), %rdi
callq 0x61e0
jmp 0x8e83
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rdi
callq 0xdcb0
movl %eax, 0x5c(%rsp)
jmp 0x8e9a
movl 0x5c(%rsp), %eax
movslq %eax, %rsi
leaq 0x21a8(%rsp), %rdi
callq 0x10f70
jmp 0x8eb0
leaq 0x2208(%rsp), %rdi
leaq 0x21e8(%rsp), %rsi
leaq 0x21a8(%rsp), %rdx
callq 0x97b0
jmp 0x8ecf
leaq 0xa269(%rip), %rdx      # 0x1313f
leaq 0x2228(%rsp), %rdi
leaq 0x2208(%rsp), %rsi
callq 0x61e0
jmp 0x8eed
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rdi
callq 0xdca0
movq %rax, 0x50(%rsp)
jmp 0x8f05
movq 0x50(%rsp), %rdx
leaq 0x2248(%rsp), %rdi
leaq 0x2228(%rsp), %rsi
callq 0x9760
jmp 0x8f21
leaq 0xa217(%rip), %rdx      # 0x1313f
leaq 0x2268(%rsp), %rdi
leaq 0x2248(%rsp), %rsi
callq 0x61e0
jmp 0x8f3f
movq 0xa0(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0xb610
movq %rax, 0x48(%rsp)
jmp 0x8f57
movq 0x48(%rsp), %rdx
leaq 0x2288(%rsp), %rdi
leaq 0x2268(%rsp), %rsi
callq 0x9760
jmp 0x8f73
movq 0x60(%rsp), %rdi
leaq 0x2288(%rsp), %rsi
callq 0x10d10
jmp 0x8f87
leaq 0x2288(%rsp), %rdi
callq 0x4788
leaq 0x2268(%rsp), %rdi
callq 0x4788
leaq 0x2248(%rsp), %rdi
callq 0x4788
leaq 0x2228(%rsp), %rdi
callq 0x4788
leaq 0x2208(%rsp), %rdi
callq 0x4788
leaq 0x21a8(%rsp), %rdi
callq 0x4788
leaq 0x21e8(%rsp), %rdi
callq 0x4788
leaq 0x21c8(%rsp), %rdi
callq 0x4788
movq 0xa0(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0xb5b0
testb $0x1, %al
jne 0x9009
jmp 0x915c
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rdi
callq 0xdc40
testb $0x1, %al
jne 0x9023
jmp 0x915c
movq 0x2370(%rsp), %rax
movw $0x1, (%rax)
jmp 0x9171
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x914a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x913d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x9130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x9123
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x9116
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x9109
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x90fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
leaq 0x2288(%rsp), %rdi
callq 0x4788
leaq 0x2268(%rsp), %rdi
callq 0x4788
leaq 0x2248(%rsp), %rdi
callq 0x4788
leaq 0x2228(%rsp), %rdi
callq 0x4788
leaq 0x2208(%rsp), %rdi
callq 0x4788
leaq 0x21a8(%rsp), %rdi
callq 0x4788
leaq 0x21e8(%rsp), %rdi
callq 0x4788
leaq 0x21c8(%rsp), %rdi
callq 0x4788
jmp 0x96c3
movq 0x2370(%rsp), %rax
movw $0x0, (%rax)
movb $0x1, 0x22af(%rsp)
movq 0xa0(%rsp), %rdi
addq $0x50, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4560
movq 0xa0(%rsp), %rax
movb $0x0, 0x48(%rax)
movq 0x40(%rax), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x91ba
movq 0x40(%rsp), %rdi
callq 0xdc50
movq 0x40(%rsp), %rdi
movl $0x60, %esi
callq 0x43c0
movq 0xa0(%rsp), %rax
movq $0x0, 0x40(%rax)
movq 0x38(%rax), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x91f2
movq 0x38(%rsp), %rdi
callq 0x98a0
movq 0x38(%rsp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x43c0
movq 0xa0(%rsp), %rax
movq $0x0, 0x38(%rax)
movb 0x22af(%rsp), %al
andb $0x1, %al
movb %al, 0x2387(%rsp)
jmp 0x96b2
movq 0x2368(%rsp), %rax
movswl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x96aa
movq 0xa0(%rsp), %rax
movl (%rax), %edi
leaq 0x1a0(%rsp), %rsi
movl $0x2000, %edx           # imm = 0x2000
xorl %ecx, %ecx
callq 0x4140
movl %eax, 0x19c(%rsp)
cmpl $-0x1, 0x19c(%rsp)
jne 0x93f3
callq 0x4060
cmpl $0x20, (%rax)
jne 0x9278
movb $0x0, 0x2387(%rsp)
jmp 0x96b2
movq 0xa0(%rsp), %rax
movq 0x70(%rax), %rax
movq %rax, 0x28(%rsp)
callq 0x4060
movslq (%rax), %rsi
leaq 0x118(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x10f70
movq 0x30(%rsp), %rdx
leaq 0x9e92(%rip), %rsi      # 0x13141
leaq 0x138(%rsp), %rdi
callq 0x6230
jmp 0x92be
leaq 0x9d4d(%rip), %rdx      # 0x13012
leaq 0x158(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x61e0
jmp 0x92dc
callq 0x4060
movl (%rax), %edi
callq 0x4150
movq %rax, %rdx
leaq 0x178(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x61e0
jmp 0x9302
movq 0x28(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x10f40
jmp 0x9316
leaq 0x178(%rsp), %rdi
callq 0x4788
leaq 0x158(%rsp), %rdi
callq 0x4788
leaq 0x138(%rsp), %rdi
callq 0x4788
leaq 0x118(%rsp), %rdi
callq 0x4788
movq 0x2370(%rsp), %rax
movw $0x0, (%rax)
movb $0x1, 0x2387(%rsp)
jmp 0x96b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x93e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x93d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x93c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x4788
leaq 0x158(%rsp), %rdi
callq 0x4788
leaq 0x138(%rsp), %rdi
callq 0x4788
leaq 0x118(%rsp), %rdi
callq 0x4788
jmp 0x96c3
cmpl $0x0, 0x19c(%rsp)
jne 0x940a
movb $0x1, 0x2387(%rsp)
jmp 0x96b2
movq 0xa0(%rsp), %rdi
addq $0x8, %rdi
movslq 0x19c(%rsp), %rdx
leaq 0x1a0(%rsp), %rsi
callq 0x40a0
movl $0x110, %edi            # imm = 0x110
callq 0x43b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
callq 0xa830
jmp 0x9447
movq 0xa0(%rsp), %rsi
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rsi)
movq 0x38(%rsi), %rdi
addq $0x8, %rsi
callq 0xa8e0
movq %rax, 0x110(%rsp)
cmpq $0x0, 0x110(%rsp)
jne 0x94e0
movq 0xa0(%rsp), %rdi
addq $0x8, %rdi
callq 0x42a0
cmpq $0x2000, %rax           # imm = 0x2000
jb 0x94d1
movq 0xa0(%rsp), %rax
movq 0x38(%rax), %rdi
movl $0x19d, %esi            # imm = 0x19D
callq 0xb5f0
jmp 0x94de
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
movl $0x110, %esi            # imm = 0x110
callq 0x43c0
jmp 0x96c3
movb $0x0, 0x2387(%rsp)
jmp 0x96b2
jmp 0x94e0
movq 0xa0(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x10(%rsp)
leaq 0xcf(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x46c0
movq 0x18(%rsp), %rdx
leaq 0x9c3e(%rip), %rsi      # 0x1314d
leaq 0xd0(%rsp), %rdi
callq 0x6450
jmp 0x951e
movq 0x10(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
callq 0xb650
jmp 0x953a
leaq 0xd0(%rsp), %rdi
callq 0x4788
leaq 0xcf(%rsp), %rdi
callq 0x4400
leaq 0xf0(%rsp), %rdi
callq 0x4670
testb $0x1, %al
jne 0x95b0
jmp 0x95b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x959e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x4788
leaq 0xcf(%rsp), %rdi
callq 0x4400
jmp 0x96c3
movl $0x60, %edi
callq 0x43b0
movq %rax, 0x8(%rsp)
jmp 0x95c1
movq 0x8(%rsp), %rdi
movq 0xa0(%rsp), %rax
movq 0x38(%rax), %rsi
movq 0x70(%rax), %rdx
movq 0x78(%rax), %rcx
callq 0xc550
jmp 0x95e1
movq 0xa0(%rsp), %rsi
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsi)
addq $0x8, %rsi
movq 0x110(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
movq $-0x1, %rcx
callq 0x43d0
jmp 0x9614
movq 0xa0(%rsp), %rdi
addq $0x8, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x44d0
leaq 0xa8(%rsp), %rdi
callq 0x4788
movq 0xa0(%rsp), %rax
movq 0x2370(%rsp), %rcx
movw $0x4, (%rcx)
movb $0x1, 0x48(%rax)
leaq 0xf0(%rsp), %rdi
callq 0x4788
jmp 0x96aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
jmp 0x969b
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x22d8(%rsp)
movl %eax, 0x22d4(%rsp)
movl $0x60, %esi
callq 0x43c0
leaq 0xf0(%rsp), %rdi
callq 0x4788
jmp 0x96c3
movb $0x0, 0x2387(%rsp)
movb 0x2387(%rsp), %al
andb $0x1, %al
addq $0x2388, %rsp           # imm = 0x2388
retq
movq 0x22d8(%rsp), %rdi
callq 0x46b0