// File: xocoder[P]m_kcptun/src/kcp_remote.cpp
// Function: remote_network_runloop(tun_remote_t*)
static void
_remote_network_runloop(tun_remote_t *tun) {

   for (int i=0;;i++) {
      if (i >= 4096) {
         i=0; mtime_sleep(1);
      }

      tun->ti = mtime_current();
      tmr_update_lst(tun->tmr, tun->ti);


      if (ikcp_peeksize(tun->kcpin) > 0) {
         int ret = 0;
         do {
            ret = ikcp_recv(tun->kcpin, (char*)tun->buf, MKCP_BUF_SIZE);
            if (ret > 0) {

               proto_t pr;
               if ( proto_probe(tun->buf, ret, &pr) )
               {
                  session_unit_t *u = session_find_sid(tun->session_lst, pr.sid);
                  if (u &&
                      pr.ptype == PROTO_TYPE_DATA &&
                      mnet_chann_state(u->tcp) == CHANN_STATE_CONNECTED &&
                      mnet_chann_cached(u->tcp) < tun->conf->tcp_cache)
                  {
                     int chann_ret = mnet_chann_send(u->tcp, pr.u.data, pr.data_length);
                     if (chann_ret < 0) {
                        cerr << "ikcp recv then fail to send: " << chann_ret << endl;
                     }
                  }
                  else if (u &&
                           pr.ptype == PROTO_TYPE_CTRL &&
                           pr.u.cmd == PROTO_CMD_CLOSE)
                  {
                     mnet_chann_close(u->tcp);
                     // destroy session in tcp callback
                  }
                  else if (pr.ptype == PROTO_TYPE_CTRL &&
                           pr.u.cmd == PROTO_CMD_OPEN)
                  {
                     _tcpout_open_and_connect(tun, pr.sid);
                  }
                  else if (pr.u.cmd != PROTO_CMD_RESET) {
                     cerr << "invalid proto cmd" << endl;
                  }
               }
               else {
                  cerr << "ikcp recv invalid proto " << endl;
                  break;
               }
            }
         } while (ret > 0);
      }

      mnet_poll( 1000 );        // micro seconds
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
cmpl $0x1000, -0xc(%rbp)     # imm = 0x1000
jl 0xd81d
movl $0x0, -0xc(%rbp)
movl $0x1, %edi
callq 0x9a70
callq 0x9a40
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rsi
callq 0xae40
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x42c0
cmpl $0x0, %eax
jle 0xd9f5
movl $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x8(%rbp), %rsi
addq $0x98, %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x3f60
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jle 0xd9e7
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
movl -0x10(%rbp), %esi
leaq -0x28(%rbp), %rdx
callq 0xbbf0
cmpl $0x0, %eax
je 0xd9c1
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xd400
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xd94c
cmpl $0x2, -0x28(%rbp)
jne 0xd94c
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72c0
cmpl $0x3, %eax
jne 0xd94c
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x81c0
movq -0x8(%rbp), %rcx
movq 0x78(%rcx), %rcx
cmpl 0x154(%rcx), %eax
jge 0xd94c
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x18(%rbp), %edx
callq 0x7d60
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0xd94a
movq 0xb6cc(%rip), %rdi      # 0x18ff0
leaq 0x76e2(%rip), %rsi      # 0x1500d
callq 0x32d0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x34d0
movq %rax, %rdi
movq 0xb67b(%rip), %rsi      # 0x18fc0
callq 0x3320
jmp 0xd9bf
cmpq $0x0, -0x30(%rbp)
je 0xd971
cmpl $0x1, -0x28(%rbp)
jne 0xd971
movzbl -0x20(%rbp), %eax
cmpl $0x3, %eax
jne 0xd971
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7280
jmp 0xd9bd
cmpl $0x1, -0x28(%rbp)
jne 0xd98e
movzbl -0x20(%rbp), %eax
cmpl $0x1, %eax
jne 0xd98e
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0xe290
jmp 0xd9bb
movzbl -0x20(%rbp), %eax
cmpl $0x4, %eax
je 0xd9b9
movq 0xb652(%rip), %rdi      # 0x18ff0
leaq 0x7686(%rip), %rsi      # 0x1502b
callq 0x32d0
movq %rax, %rdi
movq 0xb60c(%rip), %rsi      # 0x18fc0
callq 0x3320
jmp 0xd9bb
jmp 0xd9bd
jmp 0xd9bf
jmp 0xd9e5
movq 0xb628(%rip), %rdi      # 0x18ff0
leaq 0x766e(%rip), %rsi      # 0x1503d
callq 0x32d0
movq %rax, %rdi
movq 0xb5e2(%rip), %rsi      # 0x18fc0
callq 0x3320
jmp 0xd9f3
jmp 0xd9e7
jmp 0xd9e9
cmpl $0x0, -0x10(%rbp)
jg 0xd862
jmp 0xd9f5
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x82e0
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xd803
nopl (%rax)