Esempio n. 1
0
        protected void HandleReceive(Packet packet)
        {
            LastReceivedTime = TimeUitls.Now();
            if (packet.IsHeartbeat)
            {
                //Log.Debug($"接收到客户端:{this.RemoteEndPoint}心跳包.", LoggerBllType.System);
                return;
            }

            if (this.NetService.ServiceType == NetServiceType.Server)
            {
                OnReceive?.Invoke(packet);
                return;
            }

            if (packet.IsRpc)
            {
                if (RpcActions.TryRemove(packet.RpcId, out Action <Packet> action))
                {
                    action(packet);
                }
            }
            else
            {
                OnReceive?.Invoke(packet);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 心跳检测
        /// </summary>
        public void CheckHeadbeat()
        {
            var now = TimeUitls.Now();

            if (this.NetService.ServiceType == NetServiceType.Client)
            {
                if (!this.Connected)
                {
                    return;
                }

                var timeSendSpan = now - this.LastSendTime;
                if (timeSendSpan > HeartbeatTime)
                {
                    this.SendHeartbeat();
                }
            }
            else if (this.NetService.ServiceType == NetServiceType.Server)
            {
                var lastCheckSpan = now - this.LastCheckHeadbeatTime;
                if (lastCheckSpan < HeartbeatTime / 2)
                {
                    return;
                }
                LastCheckHeadbeatTime = now;

                var timeSpan = now - this.LastReceivedTime;
                if (timeSpan > HeartbeatTime + 2000) //允许2秒钟网络延迟
                {
                    Log.Debug($"{this.NetService.ProtocalType}客户端:{this.RemoteEndPoint}连接超时,心跳检测断开,心跳时长{timeSpan}.", LoggerBllType.Network);
                    this.Disconnect();
                }
            }
        }
Esempio n. 3
0
 public void SendHeartbeat()
 {
     //Log.Debug($"发送心跳包->{this.RemoteEndPoint}.", LoggerBllType.System);
     this.LastSendTime = TimeUitls.Now();
     this.SendParser.Packet.IsHeartbeat = true;
     this.SendParser.Packet.WriteBuffer();
 }
Esempio n. 4
0
        private void OnSendComplete(object o)
        {
            this.LastSendTime = TimeUitls.Now();
            this.IsSending    = false;

            if (this.NetSocket == null)
            {
                return;
            }

            SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;

            if (e.SocketError != SocketError.Success)
            {
                this.Disconnect();
                return;
            }

            this.SendParser.Buffer.UpdateRead(e.BytesTransferred);
            if (this.SendParser.Buffer.DataSize <= 0)
            {
                return;
            }

            this.StartSend();
        }
Esempio n. 5
0
        private void OnConnectComplete(object o)
        {
            if (this.NetSocket == null)
            {
                return;
            }

            if (!this.NetSocket.Connected)
            {
                return;
            }

            SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;

            if (e.SocketError != SocketError.Success)
            {
                this.OnError?.Invoke(this, SocketError.SocketError);
                return;
            }

            this.LocalEndPoint   = this.NetSocket.LocalEndPoint as IPEndPoint;
            e.RemoteEndPoint     = null;
            this.LastConnectTime = TimeUitls.Now();
            this.Connected       = true;
            OnConnected?.Invoke(this);
        }
Esempio n. 6
0
        public override void Update()
        {
            this.StartSend();

            var now = TimeUitls.Now();

            if (now > this.LastCheckTime)
            {
                KCP.KcpUpdate(this.Kcp, now);
                Service.RemoveUpdate(this.Id);
                this.LastCheckTime = KCP.KcpCheck(this.Kcp, now);
            }
        }
Esempio n. 7
0
 /// <summary>
 /// KCP发送回调函数
 /// </summary>
 /// <param name="bytes"></param>
 /// <param name="count"></param>
 /// <param name="user"></param>
 public void Output(IntPtr bytes, int count, IntPtr user)
 {
     try
     {
         this.LastSendTime = TimeUitls.Now();
         var buffer = this.SendParser.Packet.BodyStream.GetBuffer();
         Marshal.Copy(bytes, buffer, 0, count);
         this.NetSocket.SendTo(buffer, count, SocketFlags.None, this.RemoteEndPoint);
     }
     catch (Exception e)
     {
         Log.Warn(e, LoggerBllType.Network);
         this.Disconnect();
     }
 }
Esempio n. 8
0
        /// <summary>
        /// 开始发送KCP数据包
        /// </summary>
        /// <returns></returns>
        public override void StartSend()
        {
            if (!this.Connected)
            {
                return;
            }
            var now = TimeUitls.Now();

            while (this.SendParser.Buffer.DataSize > 0)
            {
                var offset = this.SendParser.Buffer.FirstReadOffset;
                var length = this.SendParser.Buffer.FirstDataSize;
                length = length > MaxPSize ? MaxPSize : length;
                KCP.KcpSend(this.Kcp, this.SendParser.Buffer.First, offset, length);
                this.SendParser.Buffer.UpdateRead(length);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// 开始连接
        /// </summary>
        /// <returns></returns>
        public override void StartConnecting()
        {
            try
            {
                if (this.Connected)
                {
                    return;
                }

                var now = TimeUitls.Now();
                if (now - this.LastConnectTime < ANetChannel.ReConnectInterval)
                {
                    return;
                }

                this.LastConnectTime = now;

                if (this.NetSocket == null)
                {
                    this.NetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                    {
                        NoDelay = true
                    };

                    this.InArgs             = new SocketAsyncEventArgs();
                    this.OutArgs            = new SocketAsyncEventArgs();
                    this.InArgs.Completed  += OnComplete;
                    this.OutArgs.Completed += OnComplete;
                }

                this.OutArgs.RemoteEndPoint = this.RemoteEndPoint;
                if (this.NetSocket.ConnectAsync(this.OutArgs))
                {
                    return;
                }

                OnConnectComplete(this.OutArgs);
            }
            catch (Exception e)
            {
                Log.Warn(e, LoggerBllType.Network);
            }
        }
Esempio n. 10
0
        public override async void StartConnecting()
        {
            if (this.Connected)
            {
                return;
            }

            var now = TimeUitls.Now();

            if (now - this.LastConnectTime < ReConnectInterval)
            {
                return;
            }

            this.LastConnectTime = now;
            var state = await StartConnectingAsync();

            await this.SyncContext;
            if (state)
            {
                this.Connected = true;
                this.OnConnected?.Invoke(this);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// 模拟TCP三次握手连接服务端
        /// </summary>
        /// <returns></returns>
        public override void StartConnecting()
        {
            try
            {
                var now = TimeUitls.Now();
                if (now - this.LastConnectTime < ANetChannel.ReConnectInterval)
                {
                    return;
                }

                this.LastConnectTime = now;

                if (this.Connected)
                {
                    return;
                }

                ConnectSender.SendSYN(this.SendParser.Packet, this.NetSocket, this.RemoteEndPoint);
            }
            catch (Exception e)
            {
                Log.Warn(e, LoggerBllType.Network);
            }
        }