// File: /workspace/github_cpp_makefile_star2_O2/roberty65[P]bServer/frame/main/Connection.cpp
// Function: beyondy::Async::Connection::onReadable(int)
int Connection::onReadable(int fd)
{
	BPROF_TRACE(BPT_CONN_READABLE)

	assert(this->fd == fd);
	SYSLOG_DEBUG("connection fd=%d flow=%d onReadable", fd, flow);

	if (inMsg == NULL) {
		// TODO: message-pool
		inMsg = Message::create(fd, flow);
		if (inMsg == NULL) {
			/* not enough memory now, remove it for 1s */
			//emgr->ModifyConnection(this, 0, IN);
			//emgr->AddTimer(this, 1000, ENABLE_IN_EVENT);
			SYSLOG_ERROR("connection fd=%d flow=%d allocate memory for incoming message failed. try later", fd, flow);
			return 0;
		}
	}

	size_t hsize = proc->headerSize();
	size_t msize;
	if ((size_t)inMsg->getWptr() < hsize) {
		size_t rest = hsize - inMsg->getWptr();
		assert(rest >= 0 && rest <= hsize);
		SYSLOG_DEBUG("connection fd=%d flow=%d to read head-rest=%d", fd, flow, (int)rest);
		
		ssize_t retval = readN(rest);
		if (retval > 0 && rest == hsize) {
			// only set tsLastRead for the first byte, so that can avoid client sending one message
			// bytes after bytes to take a long time to get a whole message
			gettimeofday(&tsLastRead, NULL);
			inMsg->ts_byte_first = tsLastRead;
		}

		if ((size_t)retval != rest) {
			SYSLOG_DEBUG("connection fd=%d flow=%d read head got %ld", fd, flow, (long)retval);
			return retval;	// partial, or error
		}

		msize = proc->calcMessageSize(inMsg);
		if ((ssize_t)msize < 1) {
			SYSLOG_ERROR("connection fd=%d flow=%d invalid message size: %ld", fd, flow, (long)msize);
			return -1;
		}

		SYSLOG_DEBUG("connection fd=%d flow=%d read head OK, got msize=%ld", fd, flow, (long)msize);
		if (inMsg->ensureCapacity(msize) < 0) {
			SYSLOG_DEBUG("connection fd=%d flow=%d read resize to msize=%ld failed", fd, flow, (long)msize);
			return -1;	/* ???try later */
		}
	}
	else {
		/* cache it? */
		msize = proc->calcMessageSize(inMsg);
		if (inMsg->ensureCapacity(msize) < 0) {
			SYSLOG_DEBUG("connection fd=%d flow=%d read resize to msize=%ld failed", fd, flow, (long)msize);
			return -1;	/* try later */
		}
	}

	size_t rest = msize - inMsg->getWptr();
	assert(rest >= 0 && rest <= msize - hsize);
	SYSLOG_DEBUG("connection fd=%d flow=%d to read body rest=%ld", fd, flow, (long)rest);

	ssize_t rlen = (rest == 0) ? (ssize_t)rest: readN(rest);
	if ((size_t)rlen == rest) {
		SYSLOG_DEBUG("connection fd=%d flow=%d got a msg, msize=%ld", fd, flow, (long)msize);

		gettimeofday(&tsLastRead, NULL);
		inMsg->ts_enqueue = tsLastRead;	// same time

		if (inQueue->push(inMsg) < 0) {
			SYSLOG_ERROR("connection fd=%d flow=%d inQ over-flow", fd, flow);
			Message::destroy(inMsg);
			
			inMsg = NULL;
			return -1;
		}

		inMsg = NULL;
		return 0;
	}

	SYSLOG_DEBUG("connection fd=%d flow=%d read body got rlen=%ld", fd, flow, (long)rlen);	
	int retval = rlen < 0 ? (errno == EINPROGRESS ? 0 : -1) : 0;

	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl %esi, 0x8(%rdi)
jne 0x5c7c
movl %esi, %ebx
movq %rdi, %r14
leaq 0xabbe(%rip), %r12      # 0x10528
movq (%r12), %rdi
cmpl $0x5, (%rdi)
jl 0x598a
movl 0x10(%r14), %r8d
leaq 0x4f30(%rip), %rdx      # 0xa8ae
pushq $0x5
popq %rsi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x6d1a
cmpq $0x0, 0x50(%r14)
jne 0x59a9
movl 0x10(%r14), %esi
movl %ebx, %edi
callq 0x6f30
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x5be3
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r13
movq 0x50(%r14), %rsi
movq 0x10(%rsi), %rbp
movq %rax, %r15
subq %rbp, %r15
jbe 0x5a75
movq (%r12), %rdi
cmpl $0x5, (%rdi)
jl 0x59ed
movl 0x10(%r14), %r8d
leaq 0x4f44(%rip), %rdx      # 0xa922
pushq $0x5
popq %rsi
movl %ebx, %ecx
movl %r15d, %r9d
xorl %eax, %eax
callq 0x6d1a
movq %r14, %rdi
movq %r15, %rsi
callq 0x5cb2
movq %rax, %r12
testq %rbp, %rbp
jne 0x5a1d
testq %r12, %r12
jle 0x5a1d
leaq 0x28(%r14), %rdi
xorl %esi, %esi
callq 0x35a0
movq 0x50(%r14), %rax
movups 0x28(%r14), %xmm0
movups %xmm0, 0x30(%rax)
cmpq %r15, %r12
jne 0x5ae2
movq 0x50(%r14), %rsi
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, %r15
leaq 0xaaea(%rip), %r12      # 0x10528
movq (%r12), %rdi
movl (%rdi), %eax
testq %r15, %r15
jle 0x5bf1
cmpl $0x5, %eax
jl 0x5a6c
movl 0x10(%r14), %r8d
leaq 0x4f51(%rip), %rdx      # 0xa9ae
pushq $0x5
popq %rsi
movl %ebx, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x6d1a
movq 0x50(%r14), %rdi
movq %r15, %rsi
jmp 0x5a89
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, %r15
movq 0x50(%r14), %rdi
movq %rax, %rsi
callq 0x6366
testl %eax, %eax
js 0x5b0d
movq 0x50(%r14), %rax
movq 0x10(%rax), %rbp
movq %r15, %r12
subq %rbp, %r12
movq %r15, %rax
subq %r13, %rax
cmpq %rax, %r12
ja 0x5c95
leaq 0xaa72(%rip), %rax      # 0x10528
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5ad8
movl 0x10(%r14), %r8d
leaq 0x4f76(%rip), %rdx      # 0xaa3f
pushq $0x5
popq %rsi
movl %ebx, %ecx
movq %r12, %r9
xorl %eax, %eax
callq 0x6d1a
cmpq %rbp, %r15
jne 0x5b30
xorl %r13d, %r13d
jmp 0x5b3e
leaq 0xaa3f(%rip), %rax      # 0x10528
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5c6a
movl 0x10(%r14), %r8d
leaq 0x4e50(%rip), %rdx      # 0xa950
pushq $0x5
popq %rsi
movl %ebx, %ecx
movq %r12, %r9
jmp 0x5c0d
movq (%r12), %rdi
pushq $-0x1
popq %r12
cmpl $0x5, (%rdi)
jl 0x5c6a
movl 0x10(%r14), %r8d
leaq 0x4eba(%rip), %rdx      # 0xa9e3
pushq $0x5
jmp 0x5c07
movq %r14, %rdi
movq %r12, %rsi
callq 0x5cb2
movq %rax, %r13
leaq 0xa9e3(%rip), %rax      # 0x10528
movq (%rax), %rdi
movl (%rdi), %eax
cmpq %r12, %r13
jne 0x5ba8
cmpl $0x5, %eax
jl 0x5b6e
movl 0x10(%r14), %r8d
leaq 0x4f0f(%rip), %rdx      # 0xaa6e
pushq $0x5
popq %rsi
movl %ebx, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x6d1a
leaq 0x28(%r14), %rdi
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x35a0
movq 0x50(%r14), %rax
movups 0x28(%r14), %xmm0
movups %xmm0, 0x40(%rax)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdi
callq 0x561e
testl %eax, %eax
js 0x5c32
andq $0x0, 0x50(%r14)
jmp 0x5c6a
cmpl $0x5, %eax
jl 0x5bc7
movl 0x10(%r14), %r8d
leaq 0x4f0b(%rip), %rdx      # 0xaac3
pushq $0x5
popq %rsi
movl %ebx, %ecx
movq %r13, %r9
xorl %eax, %eax
callq 0x6d1a
testq %r13, %r13
jns 0x5bec
callq 0x3060
xorl %r12d, %r12d
cmpl $0x73, (%rax)
setne %r12b
negl %r12d
jmp 0x5c6a
movq (%r12), %rdi
cmpl $0x2, (%rdi)
jge 0x5c16
xorl %r12d, %r12d
jmp 0x5c6a
pushq $-0x1
popq %r12
cmpl $0x2, %eax
jl 0x5c6a
movl 0x10(%r14), %r8d
leaq 0x4d76(%rip), %rdx      # 0xa97b
pushq $0x2
popq %rsi
movl %ebx, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x6d1a
jmp 0x5c6a
movl 0x10(%r14), %r8d
leaq 0x4cb1(%rip), %rdx      # 0xa8d2
xorl %r12d, %r12d
pushq $0x2
popq %rsi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x6d1a
jmp 0x5c6a
leaq 0xa8ef(%rip), %rax      # 0x10528
movq (%rax), %rdi
cmpl $0x2, (%rdi)
jl 0x5c58
movl 0x10(%r14), %r8d
leaq 0x4e50(%rip), %rdx      # 0xaa9c
pushq $0x2
popq %rsi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x6d1a
movq 0x50(%r14), %rdi
callq 0x6fc6
andq $0x0, 0x50(%r14)
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4bd5(%rip), %rdi      # 0xa858
leaq 0x4bdd(%rip), %rsi      # 0xa867
leaq 0x4be5(%rip), %rcx      # 0xa876
pushq $0x27
jmp 0x5cac
leaq 0x4d80(%rip), %rdi      # 0xaa1c
leaq 0x4bc4(%rip), %rsi      # 0xa867
leaq 0x4bcc(%rip), %rcx      # 0xa876
pushq $0x60
popq %rdx
callq 0x3200