public KChannel(uint conn, uint requestConn, UdpClient socket, IPEndPoint remoteEndPoint, KService kService)//server do this
        {
            this.Id             = conn;
            this.requestConn    = requestConn;
            this.kService       = kService;
            this.socket         = socket;
            this.remoteEndPoint = remoteEndPoint;
            kcphandle handle = new kcphandle();

            handle.Out = buffer => {
                this.socket.Send(buffer.ToArray(), buffer.Length, this.remoteEndPoint);
            };
            kcp = new Kcp(this.Id, handle);
            kcp.NoDelay(1, 10, 2, 1);//fast
            kcp.WndSize(64, 64);
            kcp.SetMtu(512);
            this.isConnected = true;
            ispendingdestory = false;
            lastpingtime     = (uint)TimeHelper.ClientNowSeconds();
            th = new Timerhandler((string s) =>
            {
                if ((uint)TimeHelper.ClientNowSeconds() - lastpingtime > PINGPERIOD * 9)
                {
                    //System.GC.Collect();
                    th.kill = true;
                    disconnect();
                }
                //Console.WriteLine("check ping");
            }, "", PINGPERIOD * 1000 * 10, true);//unit of timer is ms
            Global.GetComponent <Timer>().Add(th);
        }
Beispiel #2
0
 public void Update()
 {
     Task.Run(async() =>
     {
         try
         {
             while (true)
             {
                 await Task.Delay(1);
                 this.TimeNow = (uint)TimeHelper.ClientNowSeconds();
                 for (int i = 0; i < idChannels.Values.Count; i++)
                 {
                     if (idChannels.Values.ToArray()[i].ispendingdestory)
                     {
                         KChannel outkc;
                         idChannels.TryRemove(idChannels.Values.ToArray()[i].Id, out outkc);
                     }
                     else
                     {
                         idChannels.Values.ToArray()[i].Update(TimeNow);
                     }
                 }
             }
         }
         catch (Exception e)
         {
             Logger.log(e.ToString());
         }
     });
 }
 public void HandlePing()
 {
     lastpingtime = (uint)TimeHelper.ClientNowSeconds();
     //Console.WriteLine("ping");
 }