// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movl 0x8(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x98c4
jmp 0x98e3
leaq 0x9011(%rip), %rdi      # 0x128dc
leaq 0x9019(%rip), %rsi      # 0x128eb
movl $0x27, %edx
leaq 0x901c(%rip), %rcx      # 0x128fa
callq 0x3250
jmp 0x98e5
leaq 0x10cfc(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9926
movq -0x58(%rbp), %rax
leaq 0x10cdf(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movl $0x5, %esi
leaq 0x9013(%rip), %rdx      # 0x12932
movb $0x0, %al
callq 0xbf40
jmp 0x9928
movq -0x58(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x99a7
movq -0x58(%rbp), %rax
movl -0x14(%rbp), %edi
movl 0x10(%rax), %esi
callq 0xc2d0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x50(%rax)
cmpq $0x0, 0x50(%rax)
jne 0x99a5
jmp 0x9956
leaq 0x10c8b(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9997
movq -0x58(%rbp), %rax
leaq 0x10c6e(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movl $0x2, %esi
leaq 0x8fc6(%rip), %rdx      # 0x12956
movb $0x0, %al
callq 0xbf40
jmp 0x9999
movl $0x0, -0x4(%rbp)
jmp 0x9f5b
jmp 0x99a7
movq -0x58(%rbp), %rax
movq 0x78(%rax), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq 0x50(%rax), %rdi
callq 0x8eb0
cmpq -0x20(%rbp), %rax
jae 0x9c5b
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, -0x68(%rbp)
movq 0x50(%rax), %rdi
callq 0x8eb0
movq %rax, %rcx
movq -0x68(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x30(%rbp)
movb %al, -0x59(%rbp)
jb 0x9a10
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
setbe %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x9a19
jmp 0x9a1b
jmp 0x9a3a
leaq 0x8f84(%rip), %rdi      # 0x129a6
leaq 0x8ec2(%rip), %rsi      # 0x128eb
movl $0x3a, %edx
leaq 0x8ec5(%rip), %rcx      # 0x128fa
callq 0x3250
jmp 0x9a3c
leaq 0x10ba5(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9a84
movq -0x58(%rbp), %rax
leaq 0x10b88(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x30(%rbp), %rax
movl %eax, %r9d
movl $0x5, %esi
leaq 0x8f44(%rip), %rdx      # 0x129c1
movb $0x0, %al
callq 0xbf40
jmp 0x9a86
movq -0x58(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x9f70
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jle 0x9ad1
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x9ad1
movq -0x58(%rbp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3630
movq -0x58(%rbp), %rcx
movq 0x50(%rcx), %rax
movq 0x28(%rcx), %rdx
movq %rdx, 0x30(%rax)
movq 0x30(%rcx), %rcx
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x9b30
jmp 0x9add
leaq 0x10b04(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9b22
movq -0x58(%rbp), %rax
leaq 0x10ae7(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x38(%rbp), %r9
movl $0x5, %esi
leaq 0x8ed4(%rip), %rdx      # 0x129ef
movb $0x0, %al
callq 0xbf40
jmp 0x9b24
movq -0x38(%rbp), %rax
movl %eax, -0x4(%rbp)
jmp 0x9f5b
movq -0x58(%rbp), %rax
movq 0x78(%rax), %rdi
movq 0x50(%rax), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, -0x28(%rbp)
cmpq $0x1, -0x28(%rbp)
jge 0x9ba2
jmp 0x9b4f
leaq 0x10a92(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9b94
movq -0x58(%rbp), %rax
leaq 0x10a75(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x28(%rbp), %r9
movl $0x2, %esi
leaq 0x8e8d(%rip), %rdx      # 0x12a1a
movb $0x0, %al
callq 0xbf40
jmp 0x9b96
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9f5b
jmp 0x9ba4
leaq 0x10a3d(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9be9
movq -0x58(%rbp), %rax
leaq 0x10a20(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x28(%rbp), %r9
movl $0x5, %esi
leaq 0x8e6b(%rip), %rdx      # 0x12a4d
movb $0x0, %al
callq 0xbf40
jmp 0x9beb
movq -0x58(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0xabd0
cmpl $0x0, %eax
jge 0x9c56
jmp 0x9c03
leaq 0x109de(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9c48
movq -0x58(%rbp), %rax
leaq 0x109c1(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x28(%rbp), %r9
movl $0x5, %esi
leaq 0x8e41(%rip), %rdx      # 0x12a82
movb $0x0, %al
callq 0xbf40
jmp 0x9c4a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9f5b
jmp 0x9ce1
movq -0x58(%rbp), %rax
movq 0x78(%rax), %rdi
movq 0x50(%rax), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq 0x50(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0xabd0
cmpl $0x0, %eax
jge 0x9cdf
jmp 0x9c8c
leaq 0x10955(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9cd1
movq -0x58(%rbp), %rax
leaq 0x10938(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x28(%rbp), %r9
movl $0x5, %esi
leaq 0x8db8(%rip), %rdx      # 0x12a82
movb $0x0, %al
callq 0xbf40
jmp 0x9cd3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9f5b
jmp 0x9ce1
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movq 0x50(%rax), %rdi
callq 0x8eb0
movq %rax, %rcx
movq -0x78(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x40(%rbp)
movb %al, -0x69(%rbp)
jb 0x9d25
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
subq -0x20(%rbp), %rcx
cmpq %rcx, %rax
setbe %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x9d2e
jmp 0x9d30
jmp 0x9d4f
leaq 0x8d84(%rip), %rdi      # 0x12abb
leaq 0x8bad(%rip), %rsi      # 0x128eb
movl $0x60, %edx
leaq 0x8bb0(%rip), %rcx      # 0x128fa
callq 0x3250
jmp 0x9d51
leaq 0x10890(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9d96
movq -0x58(%rbp), %rax
leaq 0x10873(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x40(%rbp), %r9
movl $0x5, %esi
leaq 0x8d4f(%rip), %rdx      # 0x12ade
movb $0x0, %al
callq 0xbf40
jmp 0x9d98
cmpq $0x0, -0x40(%rbp)
jne 0x9da9
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x9dba
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9f70
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jne 0x9ed6
jmp 0x9dd2
leaq 0x1080f(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9e17
movq -0x58(%rbp), %rax
leaq 0x107f2(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x28(%rbp), %r9
movl $0x5, %esi
leaq 0x8cfd(%rip), %rdx      # 0x12b0d
movb $0x0, %al
callq 0xbf40
jmp 0x9e19
movq -0x58(%rbp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3630
movq -0x58(%rbp), %rax
movq 0x50(%rax), %rcx
movq 0x28(%rax), %rdx
movq %rdx, 0x40(%rcx)
movq 0x30(%rax), %rdx
movq %rdx, 0x48(%rcx)
movq 0x58(%rax), %rdi
movq 0x50(%rax), %rsi
callq 0x8e30
cmpl $0x0, %eax
jge 0x9ebe
jmp 0x9e56
leaq 0x1078b(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9e97
movq -0x58(%rbp), %rax
leaq 0x1076e(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movl $0x2, %esi
leaq 0x8cab(%rip), %rdx      # 0x12b3b
movb $0x0, %al
callq 0xbf40
jmp 0x9e99
movq -0x58(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0xc3b0
movq -0x58(%rbp), %rax
movq $0x0, 0x50(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9f5b
movq -0x58(%rbp), %rax
movq $0x0, 0x50(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x9f5b
jmp 0x9ed8
leaq 0x10709(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0x9f1d
movq -0x58(%rbp), %rax
leaq 0x106ec(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x48(%rbp), %r9
movl $0x5, %esi
leaq 0x8c4c(%rip), %rdx      # 0x12b62
movb $0x0, %al
callq 0xbf40
jmp 0x9f1f
cmpq $0x0, -0x48(%rbp)
jge 0x9f42
callq 0x3060
movl (%rax), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
cmpl $0x73, %edx
cmovel %ecx, %eax
movl %eax, -0x84(%rbp)
jmp 0x9f4c
xorl %eax, %eax
movl %eax, -0x84(%rbp)
jmp 0x9f4c
movl -0x84(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)