// File: roberty65[P]bServer/frame/main/Connection.cpp
// Function: beyondy::Async::Connection::sendMessage(beyondy::Async::Message*)
int Connection::sendMessage(Message *msg) 
{
	BPROF_TRACE(BPT_CONN_SENDMESSAGE)
	if (outMsg == NULL && outQueue->empty()) {
		/* send out directly as can as possible */
		gettimeofday(&msg->ts_byte_first, NULL);

		size_t rest = msg->getWptr() - msg->getRptr();
		ssize_t wlen = writeN(msg, rest);

		if ((size_t)wlen == rest) {
			SYSLOG_DEBUG("connection fd=%d flow=%d sendMessage %ld bytes done", fd, flow, (long)rest);

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

			return 0;
		}
		else if ((size_t)wlen < rest) {
			/* send later */
			SYSLOG_ERROR("connection fd=%d flow=%d sendMessage %ld bytes, done=%ld ", fd, flow, (long)rest, (long)wlen);
			outMsg = msg;
			emgr->modifyConnection(this, EVENT_OUT, 0); // watch out-able
			return 0;
		}
		
		SYSLOG_ERROR("connection fd=%d flow=%d sendMessage %ld bytes failed", fd, flow, (long)rest);
		proc->onSent(msg, SS_IO_ERROR);
		return -1;
	}

	// otherwise, outQueue is not empty (so need queue after them)
	// or outMsg is in sending, need queue too
	if (outQueue->size() >= (size_t)emgr->getConnectionOutQueueSize()) {
		SYSLOG_ERROR("connection fd=%d flow=%d sendMessge over-flow, out-queue-size=%ld", fd, flow, (long)outQueue->size());
		proc->onSent(msg, SS_QUEUE_FULL);
		return -1;
	}
	else {
		outQueue->push_back(msg);
	}

	SYSLOG_DEBUG("connection fd=%d flow=%d sendMessge en-outQueue(qsize=%ld) OK",
		fd, flow, (long)(outQueue->size() + (outMsg == NULL ? 0 : 1)));
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, 0x60(%rax)
jne 0xa7f8
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x8800
testb $0x1, %al
jne 0xa639
jmp 0xa7f8
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3630
movq -0x18(%rbp), %rdi
callq 0x8eb0
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
callq 0xac20
movq -0x30(%rbp), %rdi
movq %rax, %rcx
movq -0x38(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0xa440
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0xa707
jmp 0xa68f
leaq 0xff52(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa6d4
movq -0x30(%rbp), %rax
leaq 0xff35(%rip), %rcx      # 0x1a5e8
movq (%rcx), %rdi
movl 0x8(%rax), %ecx
movl 0x10(%rax), %r8d
movq -0x20(%rbp), %r9
movl $0x5, %esi
leaq 0x865b(%rip), %rdx      # 0x12d28
movb $0x0, %al
callq 0xbf40
jmp 0xa6d6
movq -0x18(%rbp), %rdi
addq $0x60, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3630
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x18(%rbp), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x38(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xa94b
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xa78a
jmp 0xa713
leaq 0xfece(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa760
movq -0x30(%rbp), %rax
leaq 0xfeb1(%rip), %rcx      # 0x1a5e8
movq (%rcx), %rdi
movl 0x8(%rax), %ecx
movl 0x10(%rax), %r8d
movq -0x20(%rbp), %r9
movq -0x28(%rbp), %rax
movl $0x2, %esi
leaq 0x8607(%rip), %rdx      # 0x12d5c
movq %rax, (%rsp)
movb $0x0, %al
callq 0xbf40
jmp 0xa762
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rax
movq %rax, 0x60(%rsi)
movq 0x70(%rsi), %rdi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x7b20
movl $0x0, -0x4(%rbp)
jmp 0xa94b
jmp 0xa78c
jmp 0xa78e
leaq 0xfe53(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa7d3
movq -0x30(%rbp), %rax
leaq 0xfe36(%rip), %rcx      # 0x1a5e8
movq (%rcx), %rdi
movl 0x8(%rax), %ecx
movl 0x10(%rax), %r8d
movq -0x20(%rbp), %r9
movl $0x2, %esi
leaq 0x85ca(%rip), %rdx      # 0x12d96
movb $0x0, %al
callq 0xbf40
jmp 0xa7d5
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x18(%rbp), %rsi
movq (%rdi), %rax
movl $0x2, %edx
callq *0x38(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa94b
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x8f20
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, -0x40(%rbp)
movq 0x70(%rax), %rdi
callq 0xac40
movl %eax, %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
cmpq %rcx, %rax
jb 0xa8b3
jmp 0xa82d
leaq 0xfdb4(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x2, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa88e
movq -0x30(%rbp), %rax
leaq 0xfd97(%rip), %rcx      # 0x1a5e8
movq (%rcx), %rcx
movq %rcx, -0x50(%rbp)
movl 0x8(%rax), %ecx
movl %ecx, -0x48(%rbp)
movl 0x10(%rax), %ecx
movl %ecx, -0x44(%rbp)
movq 0x68(%rax), %rdi
callq 0x8f20
movq -0x50(%rbp), %rdi
movl -0x48(%rbp), %ecx
movl -0x44(%rbp), %r8d
movq %rax, %r9
movl $0x2, %esi
leaq 0x8545(%rip), %rdx      # 0x12dcc
movb $0x0, %al
callq 0xbf40
jmp 0xa890
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x18(%rbp), %rsi
movq (%rdi), %rax
movl $0x3, %edx
callq *0x38(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa94b
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x8fd0
jmp 0xa8c6
leaq 0xfd1b(%rip), %rax      # 0x1a5e8
movq (%rax), %rdi
movl $0x5, %esi
callq 0x4b50
cmpl $0x0, %eax
je 0xa942
movq -0x30(%rbp), %rax
leaq 0xfcfe(%rip), %rcx      # 0x1a5e8
movq (%rcx), %rcx
movq %rcx, -0x60(%rbp)
movl 0x8(%rax), %ecx
movl %ecx, -0x58(%rbp)
movl 0x10(%rax), %ecx
movl %ecx, -0x54(%rbp)
movq 0x68(%rax), %rdi
callq 0x8f20
movq -0x60(%rbp), %rdi
movl -0x58(%rbp), %ecx
movl -0x54(%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 0x84d3(%rip), %rdx      # 0x12e0e
movb $0x0, %al
callq 0xbf40
jmp 0xa944
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)