Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        public void HandleConnnect(uint id)//client do this
        {
            if (this.isConnected)
            {
                return;
            }
            this.Id = id;
            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;
            th.kill          = true;
            th = new Timerhandler((string s) =>
            {
                cacheBytes.WriteTo(0, KcpProtocalType.PING);
                cacheBytes.WriteTo(4, this.Id);
                //Log.Debug($"client connect: {this.Conn}");
                this.socket.Send(cacheBytes, 8, this.remoteEndPoint);
                //Logger.log("ping");
            }, "", PINGPERIOD * 1000, true);
            Global.GetComponent <Timer>().Add(th);
        }
Esempio n. 3
0
 public KChannel(uint requestConn, UdpClient socket, IPEndPoint remoteEndPoint)//client do this
 {
     this.requestConn    = requestConn;
     this.socket         = socket;
     this.remoteEndPoint = remoteEndPoint;
     th = new Timerhandler((string s) => {
         cacheBytes.WriteTo(0, KcpProtocalType.SYN);
         cacheBytes.WriteTo(4, this.requestConn);
         //Log.Debug($"client connect: {this.Conn}");
         this.socket.Send(cacheBytes, 8, this.remoteEndPoint);
     }, "", 200, true);
     Global.GetComponent <Timer>().Add(th);
 }
Esempio n. 4
0
        public void Begin()
        {
            th = new Timerhandler((string s) => {
#if RTT
                string str = TimeHelper.Now().ToString();
                ////////////////////////////////////////////////////////////
                //ASCIIEncoding asen = new ASCIIEncoding();
                UTF8Encoding utf8 = new UTF8Encoding();
                byte[] buffer     = utf8.GetBytes(str);
                channel.Send(ref buffer);
                //Console.WriteLine(s);
                //Logger.log(ts);
#else
#endif
            }, ts, 1000, true);
            // Global.GetComponent<Timer>().Add(th);
        }
Esempio n. 5
0
        public virtual void Update(uint delta)
        {
            int x = 9;

            if (th == null)
            {
                th = new Timerhandler((string s) => {
                    Console.WriteLine("k1 ct" + s);
                    Console.WriteLine("x " + x);
                    if (counter++ == 5)
                    {
                        th.kill = true;
                    }
                }, "ct timer event", 1000, true);
                Global.GetComponent <Timer>().Add(th);
            }
        }