From e944b0232cf16e4b82a44915835dce091b21a695 Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Wed, 31 Jul 2024 11:20:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20HeartBeat=20=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=A4=84=E7=90=86=E5=BF=83=E8=B7=B3=20(#78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Common/Network/HTTPClient.cs | 2 +- Library/Common/Network/Socket.cs | 78 +++------------------------- 2 files changed, 7 insertions(+), 73 deletions(-) diff --git a/Library/Common/Network/HTTPClient.cs b/Library/Common/Network/HTTPClient.cs index 130893c..0138444 100644 --- a/Library/Common/Network/HTTPClient.cs +++ b/Library/Common/Network/HTTPClient.cs @@ -10,7 +10,6 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public class HTTPClient : IHTTPClient { public ClientWebSocket? Instance { get; } = null; - public HeartBeat HeartBeat { get; } public SocketRuntimeType Runtime => SocketRuntimeType.Client; public Guid Token { get; } = Guid.Empty; public string ServerAddress { get; } = ""; @@ -19,6 +18,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public string ServerNotice { get; } = ""; private bool _Listening = false; + private readonly HeartBeat HeartBeat; private HTTPClient(ClientWebSocket Instance, string ServerAddress, int ServerPort, params object[] args) { diff --git a/Library/Common/Network/Socket.cs b/Library/Common/Network/Socket.cs index 0539b4c..c410840 100644 --- a/Library/Common/Network/Socket.cs +++ b/Library/Common/Network/Socket.cs @@ -1,11 +1,12 @@ using Milimoe.FunGame.Core.Interface.Sockets; +using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Service; namespace Milimoe.FunGame.Core.Library.Common.Network { - public class Socket : IClientSocket, ISocketHeartBeat + public class Socket : IClientSocket { public System.Net.Sockets.Socket Instance { get; } public SocketRuntimeType Runtime => SocketRuntimeType.Client; @@ -14,25 +15,20 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public int ServerPort { get; } = 0; public string ServerName { get; } = ""; public string ServerNotice { get; } = ""; - public int HeartBeatFaileds => _HeartBeatFaileds; public bool Connected => Instance != null && Instance.Connected; public bool Receiving => _Receiving; - public bool SendingHeartBeat => _SendingHeartBeat; - private Task? SendingHeartBeatTask; private Task? ReceivingTask; - private Task? WaitHeartBeatReply; - + private readonly HeartBeat HeartBeat; private bool _Receiving = false; - private bool _SendingHeartBeat = false; - private int _HeartBeatFaileds = 0; private Socket(System.Net.Sockets.Socket Instance, string ServerAddress, int ServerPort) { this.Instance = Instance; this.ServerAddress = ServerAddress; this.ServerPort = ServerPort; - this.StartSendingHeartBeat(); + HeartBeat = new(this); + HeartBeat.StartSendingHeartBeat(); } public static Socket Connect(string Address, int Port = 22222) @@ -60,14 +56,6 @@ namespace Milimoe.FunGame.Core.Library.Common.Network try { SocketObject[] result = SocketManager.Receive(); - foreach (SocketObject obj in result) - { - if (obj.SocketType == SocketMessageType.HeartBeat) - { - if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1); - _HeartBeatFaileds = 0; - } - } return result; } catch (System.Exception e) @@ -89,78 +77,24 @@ namespace Milimoe.FunGame.Core.Library.Common.Network } } - public void CheckHeartBeatFaileds() - { - if (HeartBeatFaileds >= 3) Close(); - } - public void Close() { - StopSendingHeartBeat(); + HeartBeat.StopSendingHeartBeat(); StopReceiving(); Instance?.Close(); } - public void ResetHeartBeatFaileds() - { - _HeartBeatFaileds = 0; - } - public void StartReceiving(Task t) { _Receiving = true; ReceivingTask = t; } - private void StartSendingHeartBeat() - { - if (!FunGameInfo.FunGame_DebugMode) - { - _SendingHeartBeat = true; - SendingHeartBeatTask = Task.Factory.StartNew(SendHeartBeat); - } - } - private void StopReceiving() { _Receiving = false; ReceivingTask?.Wait(1); ReceivingTask = null; } - - private void StopSendingHeartBeat() - { - _SendingHeartBeat = false; - SendingHeartBeatTask?.Wait(1); - SendingHeartBeatTask = null; - } - - private void SendHeartBeat() - { - Thread.Sleep(100); - while (Connected) - { - if (!SendingHeartBeat) _SendingHeartBeat = true; - // 发送心跳包 - if (Send(SocketMessageType.HeartBeat) == SocketResult.Success) - { - WaitHeartBeatReply = Task.Run(() => - { - Thread.Sleep(4000); - AddHeartBeatFaileds(); - }); - } - else AddHeartBeatFaileds(); - Thread.Sleep(20000); - } - _SendingHeartBeat = false; - } - - private void AddHeartBeatFaileds() - { - // 超过三次没回应心跳,服务器连接失败。 - if (_HeartBeatFaileds++ >= 3) - throw new LostConnectException(); - } } }