// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20f8, %rsp           # imm = 0x20F8
movq %rsi, %rbx
movq %rdi, %r14
movzwl (%rdx), %eax
testb $0x4, %al
je 0x4d2a
cmpb $0x1, 0x48(%r14)
jne 0x4d2a
movq 0x40(%r14), %rdi
leaq 0x50(%r14), %r15
movl $0x40000, %edx          # imm = 0x40000
subq 0x58(%r14), %rdx
movq %r15, %rsi
callq 0x6abc
movl (%r14), %edi
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdx
xorl %ecx, %ecx
callq 0x3170
movq %rax, %r12
cmpl $-0x1, %r12d
jne 0x4b78
callq 0x3040
movslq (%rax), %rsi
cmpq $0x20, %rsi
je 0x4efc
movq %rax, %rbp
movq 0x70(%r14), %rax
movq %rax, 0x68(%rsp)
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x8104
leaq 0x464d(%rip), %rsi      # 0x9148
leaq 0x8(%rsp), %rdi
movq %r13, %rdx
callq 0x42fb
leaq 0x451f(%rip), %rdx      # 0x902e
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x42dc
movl (%rbp), %edi
callq 0x3110
leaq 0xf0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movq %rax, %rdx
callq 0x42dc
leaq 0xf0(%rsp), %rsi
movq 0x68(%rsp), %rdi
callq 0x80f6
leaq 0xf0(%rsp), %rdi
callq 0x35d8
leaq 0x28(%rsp), %rdi
callq 0x35d8
leaq 0x8(%rsp), %rdi
callq 0x35d8
leaq 0x48(%rsp), %rdi
callq 0x35d8
movslq %r12d, %rdx
leaq 0xf0(%rsp), %r12
pushq $-0x1
popq %rcx
movq %r12, %rdi
movq %r15, %rsi
callq 0x32e0
movq %r15, %rdi
movq %r12, %rsi
callq 0x3380
movq %r12, %rdi
callq 0x35d8
movq 0x40(%r14), %rdi
callq 0x6b42
xorl %ebp, %ebp
testb %al, %al
je 0x4efe
cmpq $0x0, 0x58(%r14)
jne 0x4efe
movq 0x70(%r14), %r12
movq 0x40(%r14), %rdi
callq 0x6b8a
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x8104
leaq 0x4576(%rip), %rdx      # 0x915f
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
callq 0x42dc
movq 0x40(%r14), %rdi
callq 0x6b80
movslq %eax, %rsi
leaq 0x90(%rsp), %rdi
callq 0x8104
leaq 0x70(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x512c
leaq 0x452c(%rip), %rdx      # 0x915f
leaq 0x48(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x42dc
movq 0x40(%r14), %rdi
callq 0x6b7a
leaq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %rax, %rdx
callq 0x510d
leaq 0x44fb(%rip), %rdx      # 0x915f
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x42dc
movq 0x38(%r14), %rdi
callq 0x5eb0
leaq 0xf0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movq %rax, %rdx
callq 0x510d
leaq 0xf0(%rsp), %rsi
movq %r12, %rdi
callq 0x7fd2
leaq 0xf0(%rsp), %rdi
callq 0x35d8
leaq 0x28(%rsp), %rdi
callq 0x35d8
leaq 0x8(%rsp), %rdi
callq 0x35d8
leaq 0x48(%rsp), %rdi
callq 0x35d8
leaq 0x70(%rsp), %rdi
callq 0x35d8
leaq 0x90(%rsp), %rdi
callq 0x35d8
leaq 0xb0(%rsp), %rdi
callq 0x35d8
leaq 0xd0(%rsp), %rdi
callq 0x35d8
movq 0x38(%r14), %rdi
callq 0x5e96
testb %al, %al
je 0x4f12
movq 0x40(%r14), %rdi
callq 0x6b4c
testb %al, %al
je 0x4f12
movw $0x1, %ax
xorl %ebp, %ebp
jmp 0x4f17
testb $0x1, %al
je 0x4efc
movl (%r14), %edi
leaq 0xf0(%rsp), %rsi
movl $0x2000, %edx           # imm = 0x2000
xorl %ecx, %ecx
callq 0x3100
movb $0x1, %bpl
testl %eax, %eax
je 0x4efe
cmpl $-0x1, %eax
jne 0x4e05
callq 0x3040
movslq (%rax), %rsi
cmpq $0x20, %rsi
je 0x4efc
movq %rax, %r15
movq 0x70(%r14), %r14
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x8104
leaq 0x43d7(%rip), %rsi      # 0x9161
leaq 0x48(%rsp), %rdi
movq %r12, %rdx
callq 0x42fb
leaq 0x4290(%rip), %rdx      # 0x902e
leaq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x42dc
movl (%r15), %edi
callq 0x3110
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x42dc
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x80f6
leaq 0x28(%rsp), %rdi
callq 0x35d8
leaq 0x8(%rsp), %rdi
callq 0x35d8
leaq 0x48(%rsp), %rdi
callq 0x35d8
leaq 0x70(%rsp), %rdi
callq 0x35d8
andw $0x0, (%rbx)
jmp 0x4efe
leaq 0x8(%r14), %r15
movslq %eax, %rdx
leaq 0xf0(%rsp), %rsi
movq %r15, %rdi
callq 0x3070
movl $0x110, %edi            # imm = 0x110
callq 0x32c0
movq %rax, %r12
movq %rax, %rdi
callq 0x5774
movq %r12, 0x38(%r14)
movq %r12, %rdi
movq %r15, %rsi
callq 0x5838
movq %rax, %r12
testq %rax, %rax
jne 0x4e64
cmpq $0x2000, 0x10(%r14)     # imm = 0x2000
jb 0x4efc
movq 0x38(%r14), %rdi
movl $0x19d, %esi            # imm = 0x19D
callq 0x5ea8
movq 0x38(%r14), %r13
leaq 0x42fe(%rip), %rsi      # 0x916d
leaq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x438c
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %r13, %rsi
callq 0x5ec0
leaq 0x8(%rsp), %rdi
callq 0x35d8
pushq $0x60
popq %rdi
callq 0x32c0
movq %rax, %r13
movq 0x38(%r14), %rsi
movq 0x70(%r14), %rdx
movq 0x78(%r14), %rcx
movq %rax, %rdi
callq 0x6238
movq %r13, 0x40(%r14)
leaq 0x8(%rsp), %rdi
pushq $-0x1
popq %rcx
movq %r15, %rsi
movq %r12, %rdx
callq 0x32e0
leaq 0x8(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3380
movq %r12, %rdi
callq 0x35d8
movw $0x4, (%rbx)
movb $0x1, 0x48(%r14)
leaq 0x28(%rsp), %rdi
callq 0x35d8
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x20f8, %rsp           # imm = 0x20F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %bpl
xorl %eax, %eax
movw %ax, (%rbx)
movq %r15, %rdi
xorl %esi, %esi
callq 0x33e0
movb $0x0, 0x48(%r14)
movq 0x40(%r14), %rbx
testq %rbx, %rbx
je 0x4f3a
movq %rbx, %rdi
callq 0x6b50
pushq $0x60
popq %rsi
movq %rbx, %rdi
callq 0x32d0
andq $0x0, 0x40(%r14)
movq 0x38(%r14), %rbx
testq %rbx, %rbx
je 0x4f5b
movq %rbx, %rdi
callq 0x5194
movl $0x110, %esi            # imm = 0x110
movq %rbx, %rdi
callq 0x32d0
andq $0x0, 0x38(%r14)
jmp 0x4efe
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x35d8
jmp 0x4f93
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x35d8
jmp 0x4fa2
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x35d8
jmp 0x4fb1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x35d8
jmp 0x4fc0
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x35d8
jmp 0x4fcf
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x35d8
jmp 0x4fdc
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x50a3
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0x50a3
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x35d8
jmp 0x504f
movq %rax, %rbx
jmp 0x5059
movq %rax, %rbx
jmp 0x5072
movq %rax, %rbx
jmp 0x507c
movq %rax, %rbx
jmp 0x509b
jmp 0x5021
movq %rax, %rbx
pushq $0x60
popq %rsi
movq %r13, %rdi
callq 0x32d0
jmp 0x5024
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x50a3
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x50a3
movq %rax, %rbx
jmp 0x50a8
movq %rax, %rbx
movl $0x110, %esi            # imm = 0x110
movq %r12, %rdi
callq 0x32d0
jmp 0x50a8
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x35d8
leaq 0x8(%rsp), %rdi
callq 0x35d8
jmp 0x5068
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x35d8
leaq 0x70(%rsp), %rdi
callq 0x35d8
leaq 0x90(%rsp), %rdi
callq 0x35d8
jmp 0x508e
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x35d8
leaq 0xd0(%rsp), %rdi
callq 0x35d8
movq %rbx, %rdi
callq 0x3510