// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq %rsi, 0x10(%rsp)
cmpq $0x0, 0x60(%rdi)
jne 0x648c
movq 0x68(%rbx), %rax
movq 0x30(%rax), %rcx
cmpq 0x10(%rax), %rcx
je 0x65c0
movq 0x68(%rbx), %rdi
movq 0x70(%rbx), %rcx
movq 0x48(%rdi), %rax
movq %rax, %rdx
subq 0x28(%rdi), %rdx
shrq $0x3, %rdx
cmpq $0x1, %rax
adcq $-0x1, %rdx
shlq $0x6, %rdx
movq 0x20(%rdi), %r9
movq 0x30(%rdi), %rax
movq %rax, %r8
subq 0x38(%rdi), %r8
sarq $0x3, %r8
subq 0x10(%rdi), %r9
sarq $0x3, %r9
addq %r8, %r9
addq %rdx, %r9
movslq 0x64(%rcx), %rcx
cmpq %rcx, %r9
jae 0x64f0
movq 0x40(%rdi), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x6538
movq %rsi, (%rax)
addq $0x8, 0x30(%rdi)
jmp 0x6542
leaq 0xa011(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x2, (%rdi)
jl 0x6519
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %r8d
leaq 0x4820(%rip), %rdx      # 0xad2d
movl $0x2, %esi
xorl %eax, %eax
callq 0x7310
movq 0x78(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq (%rdi), %rax
movl $0x3, %edx
callq *0x38(%rax)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x6690
leaq 0x10(%rsp), %rsi
callq 0x59f4
leaq 0x9fbf(%rip), %rax      # 0x10508
movq (%rax), %rdi
xorl %r14d, %r14d
cmpl $0x5, (%rdi)
jl 0x6690
movl 0x8(%rbx), %ecx
movq 0x68(%rbx), %rax
movq 0x48(%rax), %rdx
movq %rdx, %rsi
subq 0x28(%rax), %rsi
shrq $0x3, %rsi
cmpq $0x1, %rdx
adcq $-0x1, %rsi
movl 0x10(%rbx), %r8d
shlq $0x6, %rsi
movq 0x20(%rax), %r9
movq 0x30(%rax), %rdx
subq 0x38(%rax), %rdx
subq 0x10(%rax), %r9
sarq $0x3, %rdx
sarq $0x3, %r9
addq %rdx, %r9
addq %rsi, %r9
cmpq $0x1, 0x60(%rbx)
sbbq $-0x1, %r9
leaq 0x47c3(%rip), %rdx      # 0xad6f
xorl %r14d, %r14d
movl $0x5, %esi
xorl %eax, %eax
callq 0x7310
jmp 0x6690
addq $0x30, %rsi
movq %rsi, %rdi
xorl %esi, %esi
callq 0x3570
movq 0x10(%rsp), %rsi
movq 0x10(%rsi), %r14
subq 0x8(%rsi), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x636e
cmpq %r14, %rax
jne 0x663d
leaq 0x9f16(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x6617
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %r8d
leaq 0x4681(%rip), %rdx      # 0xac89
movl $0x5, %esi
movq %r14, %r9
xorl %eax, %eax
callq 0x7310
movq 0x10(%rsp), %rdi
addq $0x60, %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x3570
movq 0x78(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x38(%rax)
jmp 0x6690
leaq 0x9ec4(%rip), %rcx      # 0x10508
movq (%rcx), %rdi
movl (%rdi), %ecx
jae 0x669b
cmpl $0x2, %ecx
jl 0x6671
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %r8d
movq %rax, (%rsp)
leaq 0x465b(%rip), %rdx      # 0xacbd
movl $0x2, %esi
movq %r14, %r9
xorl %eax, %eax
callq 0x7310
movq 0x10(%rsp), %rax
movq %rax, 0x60(%rbx)
movq 0x70(%rbx), %rdi
xorl %r14d, %r14d
movq %rbx, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4f78
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
retq
cmpl $0x2, %ecx
jl 0x66bd
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %r8d
leaq 0x4649(%rip), %rdx      # 0xacf7
movl $0x2, %esi
movq %r14, %r9
xorl %eax, %eax
callq 0x7310
movq 0x78(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq (%rdi), %rax
movl $0x2, %edx
jmp 0x652a
nop