// File: /workspace/github_cpp_makefile_star2_O1/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 0x6079
movl %esi, %ebx
movq %rdi, %r14
leaq 0xa83c(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5ced
movl 0x10(%r14), %r8d
leaq 0x4bcf(%rip), %rdx      # 0xa8ae
movl $0x5, %esi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x7310
cmpq $0x0, 0x50(%r14)
jne 0x5d0c
movl 0x10(%r14), %esi
movl %ebx, %edi
callq 0x7534
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x5f98
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r15
movq 0x50(%r14), %rsi
movq 0x10(%rsi), %rbp
movq %rax, %r12
subq %rbp, %r12
jbe 0x5dfd
leaq 0xa7d4(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5d58
movl 0x10(%r14), %r8d
leaq 0x4bdb(%rip), %rdx      # 0xa922
movl $0x5, %esi
movl %ebx, %ecx
movl %r12d, %r9d
xorl %eax, %eax
callq 0x7310
movq %r14, %rdi
movq %r12, %rsi
callq 0x60b8
movq %rax, %r13
testq %rbp, %rbp
jne 0x5d88
testq %r13, %r13
jle 0x5d88
leaq 0x28(%r14), %rdi
xorl %esi, %esi
callq 0x3570
movq 0x50(%r14), %rax
movups 0x28(%r14), %xmm0
movups %xmm0, 0x30(%rax)
cmpq %r12, %r13
jne 0x5e5a
movq 0x50(%r14), %rsi
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, %r13
movq %rax, %r12
leaq 0xa75c(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl (%rdi), %eax
testq %r13, %r13
jle 0x5fed
cmpl $0x5, %eax
jl 0x5ddb
movl 0x10(%r14), %r8d
leaq 0x4be4(%rip), %rdx      # 0xa9ae
movl $0x5, %esi
movl %ebx, %ecx
movq %r12, %r9
xorl %eax, %eax
callq 0x7310
movq 0x50(%r14), %rdi
movb $0x1, %bpl
subq 0x18(%rdi), %r13
jbe 0x5df8
movq %r13, %rsi
callq 0x9184
testl %eax, %eax
js 0x603a
jmp 0x5e87
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, %r12
movq 0x50(%r14), %rdi
movq %rax, %rsi
subq 0x18(%rdi), %rsi
jbe 0x5e90
callq 0x9184
testl %eax, %eax
jns 0x5e90
leaq 0xa6e1(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $0x5, (%rdi)
jl 0x6028
movl 0x10(%r14), %r8d
leaq 0x4b9f(%rip), %rdx      # 0xa9e3
movl $0x5, %esi
movl %ebx, %ecx
movq %r12, %r9
xorl %eax, %eax
callq 0x7310
jmp 0x6028
leaq 0xa6a7(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5e85
movl 0x10(%r14), %r8d
leaq 0x4adc(%rip), %rdx      # 0xa950
movl $0x5, %esi
movl %ebx, %ecx
movq %r13, %r9
xorl %eax, %eax
callq 0x7310
xorl %ebp, %ebp
testb %bpl, %bpl
je 0x6028
movq 0x50(%r14), %rax
movq 0x10(%rax), %rbp
movq %r12, %r13
subq %rbp, %r13
movq %r12, %rax
subq %r15, %rax
cmpq %rax, %r13
ja 0x6098
leaq 0xa654(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x5ed8
movl 0x10(%r14), %r8d
leaq 0x4b78(%rip), %rdx      # 0xaa3f
movl $0x5, %esi
movl %ebx, %ecx
movq %r13, %r9
xorl %eax, %eax
callq 0x7310
cmpq %rbp, %r12
jne 0x5ee2
xorl %r15d, %r15d
jmp 0x5ef0
movq %r14, %rdi
movq %r13, %rsi
callq 0x60b8
movq %rax, %r15
leaq 0xa611(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl (%rdi), %eax
cmpq %r13, %r15
jne 0x5f5b
cmpl $0x5, %eax
jl 0x5f22
movl 0x10(%r14), %r8d
leaq 0x4b5d(%rip), %rdx      # 0xaa6e
movl $0x5, %esi
movl %ebx, %ecx
movq %r12, %r9
xorl %eax, %eax
callq 0x7310
leaq 0x28(%r14), %rdi
xorl %r13d, %r13d
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 0x5946
testl %eax, %eax
js 0x5fac
movq $0x0, 0x50(%r14)
jmp 0x6028
cmpl $0x5, %eax
jl 0x5f7c
movl 0x10(%r14), %r8d
leaq 0x4b58(%rip), %rdx      # 0xaac3
movl $0x5, %esi
movl %ebx, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x7310
testq %r15, %r15
jns 0x5fa7
callq 0x3060
xorl %r13d, %r13d
cmpl $0x73, (%rax)
setne %r13b
negl %r13d
jmp 0x6028
leaq 0xa569(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x2, (%rdi)
jge 0x600c
xorl %r13d, %r13d
jmp 0x6028
leaq 0xa555(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x2, (%rdi)
jl 0x5fd4
movl 0x10(%r14), %r8d
leaq 0x4ad6(%rip), %rdx      # 0xaa9c
movl $0x2, %esi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x7310
movq 0x50(%r14), %rdi
callq 0x7600
movq $0x0, 0x50(%r14)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x6028
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $0x2, %eax
jl 0x604f
movl 0x10(%r14), %r8d
leaq 0x4978(%rip), %rdx      # 0xa97b
xorl %ebp, %ebp
movl $0x2, %esi
jmp 0x6068
movl 0x10(%r14), %r8d
leaq 0x48bb(%rip), %rdx      # 0xa8d2
xorl %r13d, %r13d
movl $0x2, %esi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x7310
movl %r13d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa4c7(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $0x5, (%rdi)
jge 0x6056
xorl %ebp, %ebp
jmp 0x5e87
movl 0x10(%r14), %r8d
leaq 0x4982(%rip), %rdx      # 0xa9e3
xorl %ebp, %ebp
movl $0x5, %esi
movl %ebx, %ecx
movq %r12, %r9
xorl %eax, %eax
callq 0x7310
jmp 0x5e87
leaq 0x47d8(%rip), %rdi      # 0xa858
leaq 0x47e0(%rip), %rsi      # 0xa867
leaq 0x47e8(%rip), %rcx      # 0xa876
movl $0x27, %edx
callq 0x31f0
leaq 0x497d(%rip), %rdi      # 0xaa1c
leaq 0x47c1(%rip), %rsi      # 0xa867
leaq 0x47c9(%rip), %rcx      # 0xa876
movl $0x60, %edx
callq 0x31f0
nop