// File: roberty65[P]bServer/frame/main/Connection.cpp
// Function: beyondy::Async::Connection::onWritable(int)
int Connection::onWritable(int fd) 
{
	BPROF_TRACE(BPT_CONN_WRITABLE)

	assert(this->fd == fd);
	SYSLOG_DEBUG("connection fd=%d flow=%d onWritable. qsize=%ld", fd, flow, (long)(outQueue->size() + (outMsg == NULL ? 0 : 1)));

	if (outMsg == NULL) {
		if (outQueue->empty()) {
			// do not watch OUT event for it
			emgr->modifyConnection(this, 0, EVENT_OUT);
			return 0;
		}

		outMsg = outQueue->front();
		outQueue->pop_front();

		SYSLOG_DEBUG("connection fd=%d flow=%d start to send out msg(len=%ld)", fd, flow, (long)outMsg->getWptr());

		gettimeofday(&outMsg->ts_byte_first, NULL);
	}

	size_t left = outMsg->getWptr() - outMsg->getRptr();
	ssize_t wlen = writeN(outMsg, left);

	if ((size_t)wlen == left) {
		SYSLOG_DEBUG("connection fd=%d flow=%d send msg done(left=%ld)", fd, flow, (long)left);

		gettimeofday(&outMsg->ts_process_end, NULL);
		proc->onSent(outMsg, SS_OK);
		outMsg = NULL;

		// TODO: send more this time?
		return 0;
	}
	else if ((size_t)wlen < left) {
		/* try next time */
		SYSLOG_DEBUG("connection fd=%d flow=%d partially sent: should=%ld, real=%ld", fd, flow, (long)left, (long)wlen);
		return 0;
	}

	SYSLOG_ERROR("connection fd=%d flow=%d send msg(rest=%d) failed", fd, flow, (long)left);
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl 0x8(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0xa121
jmp 0xa140
leaq 0x87b4(%rip), %rdi      # 0x128dc
leaq 0x87bc(%rip), %rsi      # 0x128eb
movl $0x80, %edx
leaq 0x8a57(%rip), %rcx      # 0x12b92
callq 0x3250
jmp 0xa142
leaq 0x1049f(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa1be
movq -0x30(%rbp), %rax
leaq 0x10482(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rcx
movq %rcx, -0x40(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, -0x38(%rbp)
movl 0x10(%rax), %ecx
movl %ecx, -0x34(%rbp)
movq 0x68(%rax), %rdi
callq 0x8f20
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %r8d
movq %rax, %r9
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rsi
movl $0x1, %eax
xorl %edx, %edx
cmpq $0x0, %rsi
cmovel %edx, %eax
cltq
addq %rax, %r9
movl $0x5, %esi
leaq 0x8a13(%rip), %rdx      # 0x12bca
movb $0x0, %al
callq 0xbf40
jmp 0xa1c0
movq -0x30(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0xa29e
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x8800
testb $0x1, %al
jne 0xa1e2
jmp 0xa202
movq -0x30(%rbp), %rsi
movq 0x70(%rsi), %rdi
xorl %edx, %edx
movl $0x4, %ecx
callq 0x7b20
movl $0x0, -0x4(%rbp)
jmp 0xa430
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x8860
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, 0x60(%rax)
movq 0x68(%rax), %rdi
callq 0x8920
leaq 0x103bb(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa287
movq -0x30(%rbp), %rax
leaq 0x1039e(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rcx
movq %rcx, -0x50(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, -0x48(%rbp)
movl 0x10(%rax), %ecx
movl %ecx, -0x44(%rbp)
movq 0x60(%rax), %rdi
callq 0x8eb0
movq -0x50(%rbp), %rdi
movl -0x48(%rbp), %ecx
movl -0x44(%rbp), %r8d
movq %rax, %r9
movl $0x5, %esi
leaq 0x8979(%rip), %rdx      # 0x12bf9
movb $0x0, %al
callq 0xbf40
jmp 0xa289
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rdi
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3630
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x8eb0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, -0x58(%rbp)
movq 0x60(%rax), %rdi
callq 0xac20
movq -0x30(%rbp), %rdi
movq %rax, %rcx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq 0x60(%rdi), %rsi
movq -0x20(%rbp), %rdx
callq 0xa440
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0xa37a
jmp 0xa2f2
leaq 0x102ef(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa337
movq -0x30(%rbp), %rax
leaq 0x102d2(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x20(%rbp), %r9
movl $0x5, %esi
leaq 0x8901(%rip), %rdx      # 0x12c31
movb $0x0, %al
callq 0xbf40
jmp 0xa339
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rdi
addq $0x60, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3630
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
movq 0x60(%rax), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x38(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x60(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xa430
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xa3de
jmp 0xa386
leaq 0x1025b(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa3d3
movq -0x30(%rbp), %rax
leaq 0x1023e(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x20(%rbp), %r9
movq -0x28(%rbp), %rax
movl $0x5, %esi
leaq 0x889a(%rip), %rdx      # 0x12c62
movq %rax, (%rsp)
movb $0x0, %al
callq 0xbf40
jmp 0xa3d5
movl $0x0, -0x4(%rbp)
jmp 0xa430
jmp 0xa3e0
jmp 0xa3e2
leaq 0x101ff(%rip), %rax     # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa427
movq -0x30(%rbp), %rax
leaq 0x101e2(%rip), %rcx     # 0x1a5e8
movq (%rcx), %rdi
movl -0x14(%rbp), %ecx
movl 0x10(%rax), %r8d
movq -0x20(%rbp), %r9
movl $0x2, %esi
leaq 0x8880(%rip), %rdx      # 0x12ca0
movb $0x0, %al
callq 0xbf40
jmp 0xa429
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)