// File: /workspace/github_cpp_makefile_star2_O3/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 0x9050
cmpl $0x5, %eax
je 0x8f94
cmpl $0x4, %eax
jne 0x915f
movq -0x30(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x915f
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
leaq 0x98(%rbx), %r14
movl (%rax), %esi
movq %r14, %rdi
movl $0x2, %edx
callq 0x70e5
movq 0x48(%rbx), %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x2d0a
testl %eax, %eax
jns 0x8f44
movq 0x70e7(%rip), %rbx      # 0xfff0
leaq 0x3214(%rip), %rsi      # 0xc124
movl $0x20, %edx
movq %rbx, %rdi
callq 0x22e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21d0
movq 0x7085(%rip), %rbx      # 0xffd0
leaq 0x31f3(%rip), %rsi      # 0xc145
movl $0x12, %edx
movq %rbx, %rdi
callq 0x22e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21d0
movq 0x48(%rbx), %rdi
callq 0x3eb1
movq 0x78(%rbx), %rcx
movl 0x12c(%rcx), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
jge 0x8fdd
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rdi
movaps %xmm0, (%rdi)
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl $0x3, %edx
callq 0x70e5
testl %eax, %eax
je 0x8fdd
movq 0x48(%rbx), %rdi
leaq -0x50(%rbp), %rsi
movl $0x10, %edx
callq 0x2d0a
movq 0x8(%r14), %rdi
callq 0x45ce
movq 0x58(%rbx), %rdi
movq -0x30(%rbp), %rsi
callq 0x83ae
movq 0x6fd6(%rip), %r14      # 0xffd0
leaq 0x3169(%rip), %rsi      # 0xc16a
movl $0x1e, %edx
movq %r14, %rdi
callq 0x22e0
movq 0x58(%rbx), %rdi
callq 0x6d1c
movq %r14, %rdi
movl %eax, %esi
callq 0x2400
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21d0
jmp 0x915f
movq 0x8(%r14), %rdi
callq 0x45f0
cmpl $0x3, %eax
jne 0x915f
movq 0x48(%rbx), %rax
movl 0x8(%rax), %r12d
leaq 0x98(%rbx), %r15
movq -0x30(%rbp), %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x7114
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 0x4bf4
testl %eax, %eax
jle 0x915f
movl %eax, %r12d
movq 0x48(%rbx), %rdi
callq 0x3eb1
movq 0x78(%rbx), %rcx
movl 0x12c(%rcx), %ecx
addl %ecx, %ecx
cmpl %ecx, %eax
jge 0x912f
movq 0x48(%rbx), %rdi
addl %r13d, %r12d
movq %r15, %rsi
movl %r12d, %edx
callq 0x2d0a
testl %eax, %eax
jns 0x912f
movl %eax, %r12d
movl $0x11, %edx
movq 0x6f05(%rip), %r13      # 0xfff0
movq %r13, %rdi
leaq 0x3063(%rip), %rsi      # 0xc158
callq 0x22e0
movq %r13, %rdi
movl %r12d, %esi
callq 0x2400
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21d0
movq -0x30(%rbp), %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x7114
movl %eax, %r13d
movq 0x8(%r14), %rdi
cltq
leaq (%r15,%rax), %rsi
movl -0x34(%rbp), %edx
subl %eax, %edx
callq 0x4bf4
movl %eax, %r12d
testl %eax, %eax
jg 0x90ad
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop