// File: xocoder[P]m_kcptun/src/kcp_local.cpp
// Function: local_tcpin_callback(chann_msg_t*)
void
_local_tcpin_callback(chann_msg_t *e) {
   session_unit_t *u = (session_unit_t*)e->opaque;
   tun_local_t *tun = (tun_local_t*)u->opaque;

   switch (e->event) {

      case CHANN_EVENT_RECV: {
         if ( u->connected ) {
            const int mss = tun->kcpout->mss - MKCP_OVERHEAD;
            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 && _local_kcp_can_send_more(tun)) {
                  int kcp_ret = ikcp_send(tun->kcpout, (const char*)tun->buf, chann_ret + offset);
                  if (kcp_ret < 0) {
                     cerr << "Fail to send kcp " << kcp_ret << endl;
                  }
               }
            } while (chann_ret > 0);
         }
         break;
      }

      case CHANN_EVENT_DISCONNECT:  {

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

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

         cout << "local tcp error or disconnect !" << endl;
         break;
      }

      default: {
         break;
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %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, -0x44(%rbp)
subl $0x1, %eax
je 0xdd95
jmp 0xdd84
movl -0x44(%rbp), %eax
subl $0x5, %eax
je 0xde86
jmp 0xdf17
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0xde81
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x8(%rax), %eax
subl $0x18, %eax
movl %eax, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rdi
addq $0x98, %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
callq 0xbd10
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x98, %rsi
movslq -0x2c(%rbp), %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
subl -0x2c(%rbp), %edx
callq 0x7b70
cltq
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jle 0xde72
movq -0x18(%rbp), %rdi
callq 0xdf20
cmpl $0x0, %eax
je 0xde72
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x98, %rsi
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
addq %rcx, %rax
movl %eax, %edx
callq 0x4420
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jge 0xde70
movq 0xa1a6(%rip), %rdi      # 0x17ff0
leaq 0x6133(%rip), %rsi      # 0x13f84
callq 0x32f0
movq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x34f0
movq %rax, %rdi
movq 0xa155(%rip), %rsi      # 0x17fc0
callq 0x3340
jmp 0xde72
jmp 0xde74
cmpq $0x0, -0x28(%rbp)
jg 0xddbc
jmp 0xde81
jmp 0xdf19
movq -0x18(%rbp), %rdi
callq 0xdf20
cmpl $0x0, %eax
je 0xded5
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31c0
leaq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
movl $0x3, %edx
callq 0xbca0
cmpl $0x0, %eax
je 0xded3
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
leaq -0x40(%rbp), %rsi
movl $0x10, %edx
callq 0x4420
jmp 0xded5
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0xe4d0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7290
movq 0xa0d6(%rip), %rdi      # 0x17fd0
leaq 0x6095(%rip), %rsi      # 0x13f96
callq 0x32f0
movq %rax, %rdi
movq 0xa0b0(%rip), %rsi      # 0x17fc0
callq 0x3340
jmp 0xdf19
jmp 0xdf19
addq $0x50, %rsp
popq %rbp
retq
nop