// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
cmpq $0x0, 0x60(%rdi)
movq 0x68(%rdi), %rdi
jne 0x604c
movq 0x30(%rdi), %rax
cmpq 0x10(%rdi), %rax
je 0x611d
callq 0x5676
movq 0x70(%rbx), %rcx
movslq 0x64(%rcx), %rcx
cmpq %rcx, %rax
jae 0x60bd
movq 0x68(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x56be
leaq 0xa4b5(%rip), %rax      # 0x10528
movq (%rax), %r14
xorl %r15d, %r15d
cmpl $0x5, (%r14)
jl 0x610d
movl 0x8(%rbx), %ebp
movl 0x10(%rbx), %r12d
movq 0x68(%rbx), %rdi
callq 0x5676
cmpq $0x1, 0x60(%rbx)
sbbq $-0x1, %rax
leaq 0x4ccc(%rip), %rdx      # 0xad6f
xorl %r15d, %r15d
pushq $0x5
popq %rsi
movq %r14, %rdi
movl %ebp, %ecx
movl %r12d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x6d1a
jmp 0x610d
leaq 0xa464(%rip), %rax      # 0x10528
movq (%rax), %r15
cmpl $0x2, (%r15)
jl 0x60f9
movl 0x8(%rbx), %ebp
movl 0x10(%rbx), %r12d
movq 0x68(%rbx), %rdi
callq 0x5676
leaq 0x4c49(%rip), %rdx      # 0xad2d
pushq $0x2
popq %rsi
movq %r15, %rdi
movl %ebp, %ecx
movl %r12d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x6d1a
movq 0x78(%rbx), %rdi
movq (%rdi), %rax
pushq $0x3
popq %rdx
movq %r14, %rsi
callq *0x38(%rax)
pushq $-0x1
popq %r15
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%r14), %rdi
xorl %esi, %esi
callq 0x35a0
movq 0x10(%r14), %r15
subq 0x8(%r14), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x5f32
cmpq %r15, %rax
jne 0x618c
leaq 0xa3de(%rip), %rax      # 0x10528
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x616d
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %r8d
leaq 0x4b29(%rip), %rdx      # 0xac89
pushq $0x5
popq %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x6d1a
leaq 0x60(%r14), %rdi
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x35a0
movq 0x78(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
xorl %edx, %edx
callq *0x38(%rax)
jmp 0x610d
leaq 0xa395(%rip), %rcx      # 0x10528
movq (%rcx), %rdi
movl (%rdi), %ecx
jae 0x61db
cmpl $0x2, %ecx
jl 0x61be
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %r8d
movq %rax, (%rsp)
leaq 0x4b0c(%rip), %rdx      # 0xacbd
pushq $0x2
popq %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x6d1a
movq %r14, 0x60(%rbx)
movq 0x70(%rbx), %rdi
pushq $0x4
popq %rdx
xorl %r15d, %r15d
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x4d26
jmp 0x610d
cmpl $0x2, %ecx
jl 0x61fb
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %r8d
leaq 0x4b09(%rip), %rdx      # 0xacf7
pushq $0x2
popq %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x6d1a
movq 0x78(%rbx), %rdi
movq (%rdi), %rax
pushq $0x2
jmp 0x6102
nop