// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq 0x18(%rdi), %rax
movq %rax, -0x30(%rbp)
movq 0x18(%rax), %rbx
movl (%rdi), %eax
cmpl $0x1, %eax
je 0x80c6
cmpl $0x5, %eax
je 0x8042
cmpl $0x4, %eax
jne 0x8162
movq -0x30(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x8162
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
leaq 0x98(%rbx), %r14
movl (%rax), %esi
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x69cd
movq 0x48(%rbx), %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x2bdf
testl %eax, %eax
jns 0x8019
movq 0x6feb(%rip), %rdi      # 0xeff0
leaq 0x3118(%rip), %rsi      # 0xb124
callq 0x2260
movq %rax, %rdi
callq 0x2140
movq 0x6fb0(%rip), %rdi      # 0xefd0
leaq 0x311e(%rip), %rsi      # 0xb145
callq 0x2260
movq %rax, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140
movq %rbx, %rdi
callq 0x8171
testl %eax, %eax
je 0x807a
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rdi
movaps %xmm0, (%rdi)
movq -0x30(%rbp), %rax
movl (%rax), %esi
pushq $0x3
popq %rdx
callq 0x69cd
testl %eax, %eax
je 0x807a
movq 0x48(%rbx), %rdi
leaq -0x50(%rbp), %rsi
pushq $0x10
popq %rdx
callq 0x2bdf
movq 0x8(%r15), %rdi
callq 0x42d4
movq 0x58(%rbx), %rdi
movq -0x30(%rbp), %rsi
callq 0x7639
movq 0x6f39(%rip), %rdi      # 0xefd0
leaq 0x30cc(%rip), %rsi      # 0xb16a
callq 0x2260
movq %rax, %r14
movq 0x58(%rbx), %rdi
callq 0x662e
movq %r14, %rdi
movl %eax, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2140
jmp 0x8162
movq 0x8(%r15), %rdi
callq 0x42f6
cmpl $0x3, %eax
jne 0x8162
movq 0x48(%rbx), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
leaq 0x98(%rbx), %r12
movq -0x30(%rbp), %rax
movl (%rax), %esi
movq %r12, %rdi
callq 0x69fc
movl %eax, %r13d
movq 0x8(%r15), %rdi
cltq
leaq (%r12,%rax), %rsi
movl -0x34(%rbp), %edx
subl %eax, %edx
callq 0x4890
testl %eax, %eax
jle 0x8162
movl %eax, %r14d
movq %rbx, %rdi
callq 0x8171
testl %eax, %eax
je 0x80e9
movq 0x48(%rbx), %rdi
addl %r13d, %r14d
movq %r12, %rsi
movl %r14d, %edx
callq 0x2bdf
testl %eax, %eax
jns 0x80e9
movl %eax, %r14d
movq 0x6eaf(%rip), %rdi      # 0xeff0
leaq 0x3010(%rip), %rsi      # 0xb158
callq 0x2260
movq %rax, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2140
jmp 0x80e9
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq