Ejemplo n.º 1
0
        public void HandleRecv(byte[] date, int offset, int length)
        {
            if (this.IsDisposed)
            {
                return;
            }

            this.isConnected = true;

            Kcp.KcpInput(this.kcp, date, offset, length);
            this.GetService().AddToUpdateNextTime(0, this.Id);

            while (true)
            {
                if (this.IsDisposed)
                {
                    return;
                }
                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);
            }
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
0
        public override void Dispose()
        {
            bool IsDisposeLocal = this.IsDispose;

            base.Dispose();
            this.IsDispose = true;
            if (IsDisposeLocal)
            {
                return;
            }

            this.OnError(ErrorCode.ERR_SocketError);

            try
            {
                if (this.Error == ErrorCode.ERR_Success)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        this.Disconnect();
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }

            if (this.kcp != IntPtr.Zero)
            {
                Kcp.KcpRelease(this.kcp);
                this.kcp = IntPtr.Zero;
            }
            this.socket = null;
            this.memoryStream.Dispose();
            kService = null;
        }
Ejemplo n.º 5
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.LocalConn      = localConn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            this.kcp            = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
            Kcp.KcpSetoutput(
                this.kcp,
                (bytes, len, k, user) =>
            {
                KService.Output(bytes, len, user);
                return(len);
            }
                );
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);
            this.isConnected           = true;
            this.isRecvFirstKcpMessage = false;
            this.lastRecvTime          = kService.TimeNow;
        }
Ejemplo n.º 6
0
        public override void Send(MemoryStream stream)
        {
            // 检查等待发送的消息,如果超出两倍窗口大小,应该断开连接
            if (Kcp.KcpWaitsnd(this.kcp) > 256 * 2)
            {
                this.OnError(ErrorCode.ERR_KcpWaitSendSizeTooLarge);
                return;
            }

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

            byte[] bytes;
            if (this.isConnected)
            {
                bytes = stream.GetBuffer();
            }
            else
            {
                bytes = new byte[size];
                Array.Copy(stream.GetBuffer(), stream.Position, bytes, 0, size);
            }

            Send(bytes, 0, size);
        }
Ejemplo n.º 7
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);
            }
        }