Esempio n. 1
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.LocalConn      = localConn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            this.kcp            = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));

            SetOutput();
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);
            this.lastRecvTime = kService.TimeNow;
            this.createTime   = kService.TimeNow;
            this.Accept();
        }
Esempio n. 2
0
        public void HandleConnnect(uint remoteConn)
        {
            if (this.isConnected)
            {
                return;
            }

            this.RemoteConn = remoteConn;

            this.kcp = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
            SetOutput();
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);

            this.isConnected  = true;
            this.lastRecvTime = this.GetService().TimeNow;

            HandleSend();
        }
Esempio n. 3
0
 public void SetOutput()
 {
     // 跟上一行一样写法,pc跟linux会出错, 保存防止被GC
     kcpOutput = KcpOutput;
     Kcp.KcpSetoutput(this.kcp, kcpOutput);
 }
Esempio n. 4
0
        public void Update()
        {
            if (this.IsDisposed)
            {
                return;
            }

            uint timeNow = this.GetService().TimeNow;

            // 如果还没连接上,发送连接请求
            if (!this.isConnected)
            {
                // 10秒没连接上则报错
                if (timeNow - this.createTime > 10 * 1000)
                {
                    this.OnError(ErrorCode.ERR_KcpCantConnect);
                    return;
                }

                if (timeNow - this.lastRecvTime < 500)
                {
                    return;
                }

                switch (ChannelType)
                {
                case ChannelType.Accept:
                    this.Accept();
                    break;

                case ChannelType.Connect:
                    this.Connect();
                    break;
                }

                return;
            }

            // 超时断开连接
            //if (timeNow - this.lastRecvTime > 40 * 1000)
            //{
            //	this.OnError(ErrorCode.ERR_KcpChannelTimeout);
            //	return;
            //}

            try
            {
                Kcp.KcpUpdate(this.kcp, timeNow);
            }
            catch (Exception e)
            {
                Log.Error(e);
                this.OnError(ErrorCode.ERR_SocketError);
                return;
            }


            if (this.kcp != IntPtr.Zero)
            {
                uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow);
                this.GetService().AddToUpdateNextTime(nextUpdateTime, this.Id);
            }
        }