Example #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);
        }
Example #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);
        }