From b08d62d6f8cae75b233d122c807d2f0d2e8beec0 Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Fri, 21 Apr 2023 01:06:23 +0800 Subject: [PATCH] Fixed #15 (#17) * Fix #15; Start to Rebuild Architecture. * Fixed #15 --- Interface/Base/IClientSocket.cs | 1 + .../{BaseModel.cs => BaseController.cs} | 0 Library/Common/Network/ClientSocket.cs | 12 ++++ Library/Common/Network/JsonObject.cs | 26 ++++++++- Library/Common/Network/Socket.cs | 21 +++++++ Model/RunTimeModel.cs | 7 +++ Model/SessionModel.cs | 10 ++++ Service/SocketManager.cs | 57 ++++++++++++++++++- 8 files changed, 132 insertions(+), 2 deletions(-) rename Library/Common/Architecture/{BaseModel.cs => BaseController.cs} (100%) create mode 100644 Model/RunTimeModel.cs create mode 100644 Model/SessionModel.cs diff --git a/Interface/Base/IClientSocket.cs b/Interface/Base/IClientSocket.cs index 1125594..af748ea 100644 --- a/Interface/Base/IClientSocket.cs +++ b/Interface/Base/IClientSocket.cs @@ -8,6 +8,7 @@ namespace Milimoe.FunGame.Core.Interface.Base public void StartReceiving(Task t); public SocketResult Send(SocketMessageType type, params object[] objs); public Library.Common.Network.SocketObject Receive(); + public Library.Common.Network.SocketObject[] ReceiveArray(); public void BindEvent(Delegate Method, bool Remove = false); } } diff --git a/Library/Common/Architecture/BaseModel.cs b/Library/Common/Architecture/BaseController.cs similarity index 100% rename from Library/Common/Architecture/BaseModel.cs rename to Library/Common/Architecture/BaseController.cs diff --git a/Library/Common/Network/ClientSocket.cs b/Library/Common/Network/ClientSocket.cs index 7c3b70e..8143681 100644 --- a/Library/Common/Network/ClientSocket.cs +++ b/Library/Common/Network/ClientSocket.cs @@ -49,6 +49,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network throw new SocketWrongInfoException(); } } + + public SocketObject[] ReceiveArray() + { + try + { + return SocketManager.ReceiveArray(Instance); + } + catch + { + throw new SocketWrongInfoException(); + } + } public SocketResult Send(SocketMessageType type, params object[] objs) { diff --git a/Library/Common/Network/JsonObject.cs b/Library/Common/Network/JsonObject.cs index 656ff40..9ac096b 100644 --- a/Library/Common/Network/JsonObject.cs +++ b/Library/Common/Network/JsonObject.cs @@ -38,6 +38,30 @@ namespace Milimoe.FunGame.Core.Library.Common.Network { return JsonConvert.DeserializeObject(JsonString); } + + public static JsonObject[] GetObjects(string JsonString) + { + List jsons = new(); + + JsonSerializer serializer = new(); + JsonTextReader reader = new(new StringReader(JsonString)) + { + SupportMultipleContent = true + }; + + while (true) + { + if (!reader.Read()) + { + break; + } + + JsonObject json = serializer.Deserialize(reader); + + jsons.Add(json); + } + + return jsons.ToArray(); + } } - } diff --git a/Library/Common/Network/Socket.cs b/Library/Common/Network/Socket.cs index 6a85115..45765e6 100644 --- a/Library/Common/Network/Socket.cs +++ b/Library/Common/Network/Socket.cs @@ -71,6 +71,27 @@ namespace Milimoe.FunGame.Core.Library.Common.Network throw new SocketWrongInfoException(); } } + + public SocketObject[] ReceiveArray() + { + try + { + SocketObject[] result = SocketManager.ReceiveArray(); + foreach (SocketObject obj in result) + { + if (obj.SocketType == SocketMessageType.HeartBeat) + { + if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1); + _HeartBeatFaileds = 0; + } + } + return result; + } + catch + { + throw new SocketWrongInfoException(); + } + } public void BindEvent(Delegate Method, bool Remove = false) { diff --git a/Model/RunTimeModel.cs b/Model/RunTimeModel.cs new file mode 100644 index 0000000..b6b4f58 --- /dev/null +++ b/Model/RunTimeModel.cs @@ -0,0 +1,7 @@ +namespace Milimoe.FunGame.Core.Model +{ + public class RunTimeModel + { + + } +} diff --git a/Model/SessionModel.cs b/Model/SessionModel.cs new file mode 100644 index 0000000..dbcfdcd --- /dev/null +++ b/Model/SessionModel.cs @@ -0,0 +1,10 @@ +namespace Milimoe.FunGame.Core.Model +{ + public class SessionModel + { + public SessionModel() + { + + } + } +} diff --git a/Service/SocketManager.cs b/Service/SocketManager.cs index c949ad3..944318a 100644 --- a/Service/SocketManager.cs +++ b/Service/SocketManager.cs @@ -179,7 +179,7 @@ namespace Milimoe.FunGame.Core.Service } return result; } - + /// /// 用于服务器接收客户端信息 /// @@ -204,6 +204,61 @@ namespace Milimoe.FunGame.Core.Service return result; } + /// + /// 用于客户端接收服务器信息(数组版) + /// + /// SocketObjects + internal static Library.Common.Network.SocketObject[] ReceiveArray() + { + List result = new(); + if (Socket != null) + { + // 从服务器接收消息 + byte[] buffer = new byte[General.SocketByteSize]; + int length = Socket.Receive(buffer); + if (length > 0) + { + string msg = General.DefaultEncoding.GetString(buffer, 0, length); + Library.Common.Network.JsonObject[] jsons = Library.Common.Network.JsonObject.GetObjects(msg); + foreach (Library.Common.Network.JsonObject json in jsons) + { + // 客户端接收消息,广播ScoketObject到每个UIModel + Library.Common.Network.SocketObject obj = new(json); + result.Add(obj); + OnSocketReceive(obj); + } + } + } + return result.ToArray(); + } + + /// + /// 用于服务器接收客户端信息(数组版) + /// + /// 客户端Socket + /// SocketObjects + internal static Library.Common.Network.SocketObject[] ReceiveArray(Socket ClientSocket) + { + List result = new(); + if (ClientSocket != null) + { + // 从客户端接收消息 + byte[] buffer = new byte[General.SocketByteSize]; + int length = ClientSocket.Receive(buffer); + if (length > 0) + { + string msg = General.DefaultEncoding.GetString(buffer, 0, length); + Library.Common.Network.JsonObject[] jsons = Library.Common.Network.JsonObject.GetObjects(msg); + foreach (Library.Common.Network.JsonObject json in jsons) + { + Library.Common.Network.SocketObject so = new(json); + result.Add(so); + } + } + } + return result.ToArray(); + } + /// /// 将通信类型的枚举转换为字符串 ///