// File: /workspace/github_cpp_makefile_star2_O1/xocoder[P]m_kcptun/src/kcp_remote.cpp
// Function: remote_tcpout_callback(chann_msg_t*)
static void
_remote_tcpout_callback(chann_msg_t *e) {
   session_unit_t *u = (session_unit_t*)e->opaque;
   tun_remote_t *tun = (tun_remote_t*)u->opaque;

   switch (e->event) {
      
      case CHANN_EVENT_CONNECTED: {
         if ( !u->connected ) {
            u->connected = 1;
            int offset = proto_mark_cmd(tun->buf, u->sid, PROTO_CMD_OPENED);
            int kcp_ret = ikcp_send(tun->kcpin, (const char*)tun->buf, offset);
            if (kcp_ret < 0) {
               cerr << "Fail to send kcp connected state" << endl;
            }
            cout << "Tcp out connected." << endl;
         }
         break;
      }

      case CHANN_EVENT_RECV: {
         if (mnet_chann_state(e->n) == CHANN_STATE_CONNECTED) {
            const int mss = tun->kcpin->mss;
            long chann_ret = 0;
            do {
               int offset = proto_mark_data(tun->buf, u->sid);
               chann_ret = mnet_chann_recv(e->n, &tun->buf[offset], mss - offset);

               if (chann_ret > 0 && _remote_kcp_can_send_more(tun)) {

                  int kcp_ret = ikcp_send(tun->kcpin, (const char*)tun->buf, chann_ret + offset);
                  if (kcp_ret < 0) {
                     cerr << "Fail to send kcp " << kcp_ret << endl;
                  }
               } else {
                  // drain recv data
               }
            } while (chann_ret > 0);
         }
         break;
      }

      case CHANN_EVENT_DISCONNECT:  {

         // send disconnect msg
         if ( _remote_kcp_can_send_more(tun) ) {
            unsigned char buf[16] = { 0 };
            if ( proto_mark_cmd(buf, u->sid, PROTO_CMD_CLOSE) ) {
               ikcp_send(tun->kcpin, (const char*)buf, 16);
            }
         }

         mnet_chann_close(e->n);
         session_destroy(tun->session_lst, u);

         cout << "remote tcp disconnect, remain " << skt_count(tun->session_lst) << endl;
         break;
      }

      default: {
         break;
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
movq 0x18(%rdi), %rax
movq %rax, -0x30(%rbp)
movq 0x18(%rax), %rbx
movl (%rdi), %eax
cmpl $0x1, %eax
je 0x9177
cmpl $0x5, %eax
je 0x90bb
cmpl $0x4, %eax
jne 0x9286
movq -0x30(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x9286
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
leaq 0x98(%rbx), %r14
movl (%rax), %esi
movq %r14, %rdi
movl $0x2, %edx
callq 0x71ad
movq 0x48(%rbx), %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x2cb4
testl %eax, %eax
jns 0x909b
movq 0x6f90(%rip), %rbx      # 0xfff0
leaq 0x30bd(%rip), %rsi      # 0xc124
movl $0x20, %edx
movq %rbx, %rdi
callq 0x22d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21c0
movq 0x6f2e(%rip), %rbx      # 0xffd0
leaq 0x309c(%rip), %rsi      # 0xc145
movl $0x12, %edx
movq %rbx, %rdi
callq 0x22d0
jmp 0x914b
movq 0x48(%rbx), %rdi
callq 0x3ef5
movq 0x78(%rbx), %rcx
movl 0x12c(%rcx), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
jge 0x9104
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rdi
movaps %xmm0, (%rdi)
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl $0x3, %edx
callq 0x71ad
testl %eax, %eax
je 0x9104
movq 0x48(%rbx), %rdi
leaq -0x50(%rbp), %rsi
movl $0x10, %edx
callq 0x2cb4
movq 0x8(%r14), %rdi
callq 0x463a
movq 0x58(%rbx), %rdi
movq -0x30(%rbp), %rsi
callq 0x84da
movq 0x6eaf(%rip), %r14      # 0xffd0
leaq 0x3042(%rip), %rsi      # 0xc16a
movl $0x1e, %edx
movq %r14, %rdi
callq 0x22d0
movq 0x58(%rbx), %rdi
callq 0x6de5
movq %r14, %rdi
movl %eax, %esi
callq 0x23f0
movq %rax, %rbx
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21c0
jmp 0x9286
movq 0x8(%r14), %rdi
callq 0x465c
cmpl $0x3, %eax
jne 0x9286
movq 0x48(%rbx), %rax
movl 0x8(%rax), %r12d
leaq 0x98(%rbx), %r15
movq -0x30(%rbp), %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x71dc
movl %eax, %r13d
movq 0x8(%r14), %rdi
cltq
leaq (%rbx,%rax), %rsi
addq $0x98, %rsi
movl %r12d, -0x34(%rbp)
movl %r12d, %edx
subl %r13d, %edx
callq 0x4c53
testl %eax, %eax
jle 0x9286
movl %eax, %r12d
movq 0x48(%rbx), %rdi
callq 0x3ef5
movq 0x78(%rbx), %rcx
movl 0x12c(%rcx), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
jge 0x9256
movq 0x48(%rbx), %rdi
addl %r13d, %r12d
movq %r15, %rsi
movl %r12d, %edx
callq 0x2cb4
testl %eax, %eax
jns 0x9256
movl %eax, %r12d
movl $0x11, %edx
movq 0x6dde(%rip), %r13      # 0xfff0
movq %r13, %rdi
leaq 0x2f3c(%rip), %rsi      # 0xc158
callq 0x22d0
movq %r13, %rdi
movl %r12d, %esi
callq 0x23f0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21c0
movq -0x30(%rbp), %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x71dc
movl %eax, %r13d
movq 0x8(%r14), %rdi
cltq
leaq (%r15,%rax), %rsi
movl -0x34(%rbp), %edx
subl %eax, %edx
callq 0x4c53
movl %eax, %r12d
testl %eax, %eax
jg 0x91d4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)