// File: /workspace/github_cpp_makefile_star2_O3/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 0x5f88
movl %esi, %ebx
movq %rdi, %r14
leaq 0xa8dc(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5c4d
movl 0x10(%r14), %r8d
leaq 0x4c6f(%rip), %rdx      # 0xa8ae
movl $0x5, %esi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x71fc
cmpq $0x0, 0x50(%r14)
jne 0x5c6c
movl 0x10(%r14), %esi
movl %ebx, %edi
callq 0x7420
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x5eaa
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 0x5d4a
leaq 0xa874(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5cb8
movl 0x10(%r14), %r8d
leaq 0x4c7b(%rip), %rdx      # 0xa922
movl $0x5, %esi
movl %ebx, %ecx
movl %r15d, %r9d
xorl %eax, %eax
callq 0x71fc
movq %r14, %rdi
movq %r15, %rsi
callq 0x5fc6
movq %rax, %r12
testq %rbp, %rbp
jne 0x5ce8
testq %r12, %r12
jle 0x5ce8
leaq 0x28(%r14), %rdi
xorl %esi, %esi
callq 0x3570
movq 0x50(%r14), %rax
movups 0x28(%r14), %xmm0
movups %xmm0, 0x30(%rax)
cmpq %r15, %r12
jne 0x5e7d
movq 0x50(%r14), %rsi
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, %r12
movq %rax, %r15
leaq 0xa7fc(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl (%rdi), %eax
testq %r12, %r12
jle 0x5f31
cmpl $0x5, %eax
jl 0x5d3b
movl 0x10(%r14), %r8d
leaq 0x4c84(%rip), %rdx      # 0xa9ae
movl $0x5, %esi
movl %ebx, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x71fc
movq 0x50(%r14), %rdi
subq 0x18(%rdi), %r12
jbe 0x5d71
movq %r12, %rsi
jmp 0x5d64
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, %r15
movq 0x50(%r14), %rdi
movq %rax, %rsi
subq 0x18(%rdi), %rsi
jbe 0x5d71
callq 0x9080
testl %eax, %eax
js 0x5ec5
movq 0x50(%r14), %rax
movq 0x10(%rax), %rbp
movq %r15, %r12
subq %rbp, %r12
movq %r15, %rax
subq %r13, %rax
cmpq %rax, %r12
ja 0x5fa7
leaq 0xa773(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5db9
movl 0x10(%r14), %r8d
leaq 0x4c97(%rip), %rdx      # 0xaa3f
movl $0x5, %esi
movl %ebx, %ecx
movq %r12, %r9
xorl %eax, %eax
callq 0x71fc
cmpq %rbp, %r15
jne 0x5dc3
xorl %r13d, %r13d
jmp 0x5dd1
movq %r14, %rdi
movq %r12, %rsi
callq 0x5fc6
movq %rax, %r13
leaq 0xa730(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl (%rdi), %eax
cmpq %r12, %r13
jne 0x5e40
cmpl $0x5, %eax
jl 0x5e03
movl 0x10(%r14), %r8d
leaq 0x4c7c(%rip), %rdx      # 0xaa6e
movl $0x5, %esi
movl %ebx, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x71fc
leaq 0x28(%r14), %rdi
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x3570
movq 0x50(%r14), %rax
movups 0x28(%r14), %xmm0
movups %xmm0, 0x40(%rax)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdi
callq 0x589a
testl %eax, %eax
js 0x5ef0
movq $0x0, 0x50(%r14)
jmp 0x5f76
cmpl $0x5, %eax
jl 0x5e61
movl 0x10(%r14), %r8d
leaq 0x4c73(%rip), %rdx      # 0xaac3
movl $0x5, %esi
movl %ebx, %ecx
movq %r13, %r9
xorl %eax, %eax
callq 0x71fc
testq %r13, %r13
jns 0x5ebd
callq 0x3060
xorl %r12d, %r12d
cmpl $0x73, (%rax)
setne %r12b
negl %r12d
jmp 0x5f76
leaq 0xa684(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5f76
movl 0x10(%r14), %r8d
leaq 0x4ab5(%rip), %rdx      # 0xa950
movl $0x5, %esi
movl %ebx, %ecx
movq %r12, %r9
jmp 0x5f51
leaq 0xa657(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x2, (%rdi)
jge 0x5f5a
xorl %r12d, %r12d
jmp 0x5f76
leaq 0xa63c(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x5, (%rdi)
jl 0x5f76
movl 0x10(%r14), %r8d
leaq 0x4afa(%rip), %rdx      # 0xa9e3
movl $0x5, %esi
jmp 0x5f4c
leaq 0xa611(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x2, (%rdi)
jl 0x5f18
movl 0x10(%r14), %r8d
leaq 0x4b92(%rip), %rdx      # 0xaa9c
movl $0x2, %esi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x71fc
movq 0x50(%r14), %rdi
callq 0x74ec
movq $0x0, 0x50(%r14)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x5f76
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x2, %eax
jl 0x5f76
movl 0x10(%r14), %r8d
leaq 0x4a34(%rip), %rdx      # 0xa97b
movl $0x2, %esi
movl %ebx, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x71fc
jmp 0x5f76
movl 0x10(%r14), %r8d
leaq 0x496d(%rip), %rdx      # 0xa8d2
xorl %r12d, %r12d
movl $0x2, %esi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x71fc
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x48c9(%rip), %rdi      # 0xa858
leaq 0x48d1(%rip), %rsi      # 0xa867
leaq 0x48d9(%rip), %rcx      # 0xa876
movl $0x27, %edx
callq 0x31f0
leaq 0x4a6e(%rip), %rdi      # 0xaa1c
leaq 0x48b2(%rip), %rsi      # 0xa867
leaq 0x48ba(%rip), %rcx      # 0xa876
movl $0x60, %edx
callq 0x31f0