// File: xocoder[P]m_kcptun/src/kcp_local.cpp
// Function: local_network_runloop(tun_local_t*)
static void
_local_network_runloop(tun_local_t *tun) {

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

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


      if (ikcp_peeksize(tun->kcpout) > 0) {
         int ret = 0;
         do {
            ret = ikcp_recv(tun->kcpout, (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 ) {
                     if (pr.ptype == PROTO_TYPE_DATA)
                     {
                        int chann_ret = mnet_chann_send(u->tcp, pr.u.data, pr.data_length);
                        if (pr.u.data && pr.data_length>0 && chann_ret<0) {
                           cerr << "ikcp recv then fail to send " << chann_ret << endl;
                        }
                     }
                     else if (pr.ptype == PROTO_TYPE_CTRL) {
                        if (pr.u.cmd == PROTO_CMD_OPENED) {
                           u->connected = 1;
                           cout << "remote tcp connected" << endl;
                        }
                        else if (pr.u.cmd == PROTO_CMD_CLOSE)
                        {
                           mnet_chann_close(u->tcp);
                           session_destroy(tun->session_lst, u);
                           cout << "close tcp with sid " << pr.sid << endl;
                        }
                     }
                  }
               }
               else
               {
                  cerr << "ikcp recv invalid proto " << endl;
               }
            }
         } 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 $0x400, -0xc(%rbp)      # imm = 0x400
jl 0xd64d
movl $0x0, -0xc(%rbp)
movl $0x1, %edi
callq 0x9a80
callq 0x9a50
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 0xae50
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x42d0
cmpl $0x0, %eax
jle 0xd82f
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 0x3f70
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jle 0xd821
movq -0x8(%rbp), %rdi
addq $0x98, %rdi
movl -0x10(%rbp), %esi
leaq -0x28(%rbp), %rdx
callq 0xbc00
cmpl $0x0, %eax
je 0xd7fd
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xe480
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xd7fb
cmpl $0x2, -0x28(%rbp)
jne 0xd75d
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x18(%rbp), %edx
callq 0x7d70
movl %eax, -0x34(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xd758
cmpl $0x0, -0x18(%rbp)
jle 0xd758
cmpl $0x0, -0x34(%rbp)
jge 0xd758
movq 0xa8be(%rip), %rdi      # 0x17ff0
leaq 0x68f3(%rip), %rsi      # 0x1402c
callq 0x32f0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x34f0
movq %rax, %rdi
movq 0xa86d(%rip), %rsi      # 0x17fc0
callq 0x3340
jmp 0xd7f9
cmpl $0x1, -0x28(%rbp)
jne 0xd7f7
movzbl -0x20(%rbp), %eax
cmpl $0x2, %eax
jne 0xd79f
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
movq 0xa84e(%rip), %rdi      # 0x17fd0
leaq 0x68c0(%rip), %rsi      # 0x14049
callq 0x32f0
movq %rax, %rdi
movq 0xa828(%rip), %rsi      # 0x17fc0
callq 0x3340
jmp 0xd7f5
movzbl -0x20(%rbp), %eax
cmpl $0x3, %eax
jne 0xd7f3
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7290
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0xe4d0
movq 0xa803(%rip), %rdi      # 0x17fd0
leaq 0x688a(%rip), %rsi      # 0x1405e
callq 0x32f0
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x3440
movq %rax, %rdi
movq 0xa7d2(%rip), %rsi      # 0x17fc0
callq 0x3340
jmp 0xd7f5
jmp 0xd7f7
jmp 0xd7f9
jmp 0xd7fb
jmp 0xd81f
movq 0xa7ec(%rip), %rdi      # 0x17ff0
leaq 0x6867(%rip), %rsi      # 0x14072
callq 0x32f0
movq %rax, %rdi
movq 0xa7a6(%rip), %rsi      # 0x17fc0
callq 0x3340
jmp 0xd821
jmp 0xd823
cmpl $0x0, -0x10(%rbp)
jg 0xd692
jmp 0xd82f
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x82f0
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xd633
nopw (%rax,%rax)