// File: 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
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x54(%rbp)
subl $0x1, %eax
je 0xe4c7
jmp 0xe408
movl -0x54(%rbp), %eax
subl $0x4, %eax
je 0xe423
jmp 0xe412
movl -0x54(%rbp), %eax
subl $0x5, %eax
je 0xe5bf
jmp 0xe66c
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xe4c2
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x18(%rbp), %rdi
addq $0x98, %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
movl $0x2, %edx
callq 0xbc90
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x98, %rsi
movl -0x1c(%rbp), %edx
callq 0x4410
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0xe4a0
movq 0xab6b(%rip), %rdi      # 0x18ff0
leaq 0x6c23(%rip), %rsi      # 0x150af
callq 0x32d0
movq %rax, %rdi
movq 0xab25(%rip), %rsi      # 0x18fc0
callq 0x3320
movq 0xab29(%rip), %rdi      # 0x18fd0
leaq 0x6c22(%rip), %rsi      # 0x150d0
callq 0x32d0
movq %rax, %rdi
movq 0xab03(%rip), %rsi      # 0x18fc0
callq 0x3320
jmp 0xe66e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72c0
cmpl $0x3, %eax
jne 0xe5ba
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rdi
addq $0x98, %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
callq 0xbd00
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x98, %rsi
movslq -0x34(%rbp), %rax
addq %rax, %rsi
movl -0x24(%rbp), %edx
subl -0x34(%rbp), %edx
callq 0x7b60
cltq
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jle 0xe5a9
movq -0x18(%rbp), %rdi
callq 0xe680
cmpl $0x0, %eax
je 0xe5a9
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x98, %rsi
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
addq %rcx, %rax
movl %eax, %edx
callq 0x4410
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0xe5a7
movq 0xaa6f(%rip), %rdi      # 0x18ff0
leaq 0x6b5b(%rip), %rsi      # 0x150e3
callq 0x32d0
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x34d0
movq %rax, %rdi
movq 0xaa1e(%rip), %rsi      # 0x18fc0
callq 0x3320
jmp 0xe5ab
jmp 0xe5ab
jmp 0xe5ad
cmpq $0x0, -0x30(%rbp)
jg 0xe4f3
jmp 0xe5ba
jmp 0xe66e
movq -0x18(%rbp), %rdi
callq 0xe680
cmpl $0x0, %eax
je 0xe60e
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31b0
leaq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
movl $0x3, %edx
callq 0xbc90
cmpl $0x0, %eax
je 0xe60c
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
leaq -0x50(%rbp), %rsi
movl $0x10, %edx
callq 0x4410
jmp 0xe60e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7280
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0xd450
movq 0xa99d(%rip), %rdi      # 0x18fd0
leaq 0x6abb(%rip), %rsi      # 0x150f5
callq 0x32d0
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0xb510
movq -0x60(%rbp), %rdi
movl %eax, %esi
callq 0x34d0
movq %rax, %rdi
movq 0xa95b(%rip), %rsi      # 0x18fc0
callq 0x3320
jmp 0xe66e
jmp 0xe66e
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)