From ecdb321eb6bf74298043acd137dd4c8cff7de3b9 Mon Sep 17 00:00:00 2001
From: milimoe <110188673+milimoe@users.noreply.github.com>
Date: Tue, 18 Apr 2023 20:28:05 +0800
Subject: [PATCH] Fix #15; Start to Rebuild Architecture. (#16)
---
Api/Transmittal/DataRequest.cs | 2 +-
FunGame.Core.csproj | 4 ----
Library/Common/Architecture/BaseController.cs | 10 ----------
Library/Common/Architecture/BaseModel.cs | 6 +++---
Library/Common/Network/JsonObject.cs | 4 ++--
.../RoomList.cs => Model/RoomListModel.cs | 15 +++++++--------
Others/Others.cs | 6 ------
Service/SocketManager.cs | 19 ++++++++-----------
8 files changed, 21 insertions(+), 45 deletions(-)
delete mode 100644 Library/Common/Architecture/BaseController.cs
rename Library/Common/Architecture/RoomList.cs => Model/RoomListModel.cs (93%)
delete mode 100644 Others/Others.cs
diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs
index 7fb4e84..ea9c809 100644
--- a/Api/Transmittal/DataRequest.cs
+++ b/Api/Transmittal/DataRequest.cs
@@ -42,7 +42,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
return default;
}
- private class Request : BaseModel
+ private class Request : BaseController
{
public Hashtable RequestData { get; } = new();
public Hashtable ResultData { get; } = new();
diff --git a/FunGame.Core.csproj b/FunGame.Core.csproj
index 9d38ced..0d190d8 100644
--- a/FunGame.Core.csproj
+++ b/FunGame.Core.csproj
@@ -28,8 +28,4 @@
-
-
-
-
diff --git a/Library/Common/Architecture/BaseController.cs b/Library/Common/Architecture/BaseController.cs
deleted file mode 100644
index ee8ebf3..0000000
--- a/Library/Common/Architecture/BaseController.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-namespace Milimoe.FunGame.Core.Library.Common.Architecture
-{
- public abstract class BaseController
- {
- ///
- /// 重写此方法并调用Model的Dispose方法,否则将无法正常将监听Socket的事件移除!
- ///
- public abstract void Dispose();
- }
-}
diff --git a/Library/Common/Architecture/BaseModel.cs b/Library/Common/Architecture/BaseModel.cs
index 56afac4..b7977b3 100644
--- a/Library/Common/Architecture/BaseModel.cs
+++ b/Library/Common/Architecture/BaseModel.cs
@@ -4,7 +4,7 @@ using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Library.Common.Architecture
{
- public class BaseModel : ISocketHandler, IDisposable
+ public class BaseController : ISocketHandler, IDisposable
{
///
/// 接收到的SocketObject实例
@@ -25,7 +25,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
/// 继承请调用base构造
///
/// Socket
- public BaseModel(Socket? socket)
+ public BaseController(Socket? socket)
{
if (socket != null)
{
@@ -34,7 +34,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
}
else throw new SocketCreateReceivingException();
}
-
+
///
/// 继承请重写此方法
///
diff --git a/Library/Common/Network/JsonObject.cs b/Library/Common/Network/JsonObject.cs
index 44e83ae..656ff40 100644
--- a/Library/Common/Network/JsonObject.cs
+++ b/Library/Common/Network/JsonObject.cs
@@ -5,7 +5,7 @@ using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
[Serializable]
- public class JsonObject
+ public struct JsonObject
{
public SocketMessageType MessageType { get; } = SocketMessageType.Unknown;
public Guid Token { get; }
@@ -34,7 +34,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
return new JsonObject(MessageType, Token, Parameters).JsonString;
}
- public static JsonObject? GetObject(string JsonString)
+ public static JsonObject GetObject(string JsonString)
{
return JsonConvert.DeserializeObject(JsonString);
}
diff --git a/Library/Common/Architecture/RoomList.cs b/Model/RoomListModel.cs
similarity index 93%
rename from Library/Common/Architecture/RoomList.cs
rename to Model/RoomListModel.cs
index b1408bc..d21aff1 100644
--- a/Library/Common/Architecture/RoomList.cs
+++ b/Model/RoomListModel.cs
@@ -1,11 +1,10 @@
using System.Collections;
-using System.Numerics;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Constant;
-namespace Milimoe.FunGame.Core.Library.Common.Architecture
+namespace Milimoe.FunGame.Core.Model
{
- public class RoomList : IEnumerable
+ public class RoomListModel : IEnumerable
{
private readonly Hashtable _List = new();
private readonly Hashtable _PlayerList = new();
@@ -16,11 +15,11 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
public List ListRoomID => _List.Keys.Cast().ToList();
- public RoomList()
+ public RoomListModel()
{
}
-
+
public Room? this[string RoomID] => GetRoom(RoomID);
public void Clear()
@@ -48,7 +47,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
_List.Remove(RoomID);
_PlayerList.Remove(RoomID);
}
-
+
public void RemoveRoom(Room Room)
{
RemoveRoom(Room.Roomid);
@@ -59,7 +58,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
if (RoomID == "-1" || Player.Id == 0) return;
GetPlayerList(RoomID).Add(Player);
}
-
+
public void QuitRoom(string RoomID, User Player)
{
if (RoomID == "-1" || Player.Id == 0) return;
@@ -117,7 +116,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
public IEnumerator GetEnumerator()
{
- foreach(Room room in ListRoom)
+ foreach (Room room in ListRoom)
{
yield return room;
}
diff --git a/Others/Others.cs b/Others/Others.cs
deleted file mode 100644
index be9d05d..0000000
--- a/Others/Others.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace Milimoe.FunGame.Core.Others
-{
- internal class Others
- {
- }
-}
diff --git a/Service/SocketManager.cs b/Service/SocketManager.cs
index baf77db..c949ad3 100644
--- a/Service/SocketManager.cs
+++ b/Service/SocketManager.cs
@@ -40,6 +40,7 @@ namespace Milimoe.FunGame.Core.Service
IPEndPoint ServerEndPoint = new(IPAddress.Any, Port);
_ServerSocket.Bind(ServerEndPoint);
_ServerSocket.Listen(MaxConnection);
+ _ServerSocket.NoDelay = true;
return _ServerSocket;
}
catch
@@ -61,6 +62,7 @@ namespace Milimoe.FunGame.Core.Service
try
{
Client = ServerSocket.Accept();
+ Client.NoDelay = true;
IPEndPoint? ClientIPEndPoint = (IPEndPoint?)Client.RemoteEndPoint;
ClientIP = (ClientIPEndPoint != null) ? ClientIPEndPoint.ToString() : "Unknown";
return new object[] { ClientIP, Client };
@@ -95,6 +97,7 @@ namespace Milimoe.FunGame.Core.Service
ClientSocket.Connect(ServerEndPoint);
if (ClientSocket.Connected)
{
+ ClientSocket.NoDelay = true;
_Socket = ClientSocket;
return _Socket;
}
@@ -167,11 +170,8 @@ namespace Milimoe.FunGame.Core.Service
if (length > 0)
{
string msg = General.DefaultEncoding.GetString(buffer, 0, length);
- Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
- if (json != null)
- {
- result = new Library.Common.Network.SocketObject(json);
- }
+ Library.Common.Network.JsonObject json = Library.Common.Network.JsonObject.GetObject(msg);
+ result = new Library.Common.Network.SocketObject(json);
// 客户端接收消息,广播ScoketObject到每个UIModel
OnSocketReceive(result);
return result;
@@ -196,17 +196,14 @@ namespace Milimoe.FunGame.Core.Service
if (length > 0)
{
string msg = General.DefaultEncoding.GetString(buffer, 0, length);
- Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
- if (json != null)
- {
- result = new Library.Common.Network.SocketObject(json);
- }
+ Library.Common.Network.JsonObject json = Library.Common.Network.JsonObject.GetObject(msg);
+ result = new Library.Common.Network.SocketObject(json);
return result;
}
}
return result;
}
-
+
///
/// 将通信类型的枚举转换为字符串
///