Пример #1
0
        public override void Send(MemoryStream stream)
        {
            if (this.kcp != IntPtr.Zero)
            {
                // 检查等待发送的消息,如果超出两倍窗口大小,应该断开连接
                if (Kcp.KcpWaitsnd(this.kcp) > 256 * 2)
                {
                    this.OnError(ErrorCode.ERR_KcpWaitSendSizeTooLarge);
                    return;
                }
            }

            ushort size = (ushort)(stream.Length - stream.Position);

            byte[] bytes = stream.GetBuffer();
            Send(bytes, 0, size);
        }
Пример #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, 32, 32);
            Kcp.KcpSetmtu(this.kcp, 470);

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

            Connect();
        }
Пример #3
0
        public void HandleRecv(byte[] date, int offset, int length)
        {
            this.isConnected = true;

            Kcp.KcpInput(this.kcp, date, offset, length);

            while (true)
            {
                int n = Kcp.KcpPeeksize(this.kcp);
                if (n < 0)
                {
                    return;
                }
                if (n == 0)
                {
                    this.OnError((int)SocketError.NetworkReset);
                    return;
                }

                byte[] buffer = this.memoryStream.GetBuffer();
                this.memoryStream.SetLength(n);
                this.memoryStream.Seek(0, SeekOrigin.Begin);
                int count = Kcp.KcpRecv(this.kcp, buffer, ushort.MaxValue);
                if (n != count)
                {
                    return;
                }
                if (count <= 0)
                {
                    return;
                }

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

                this.OnRead(this.memoryStream);
            }
        }
Пример #4
0
        public void Update()
        {
            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;
//				}
            }

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


            if (this.kcp != IntPtr.Zero)
            {
                uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow);
            }
        }
Пример #5
0
 private void KcpSend(byte[] buffers, int length)
 {
     Kcp.KcpSend(this.kcp, buffers, length);
 }