diff --git a/FunGame.Core/Api/Utility/Factory.cs b/FunGame.Core/Api/Utility/Factory.cs
index f8642b0..cf5b50a 100644
--- a/FunGame.Core/Api/Utility/Factory.cs
+++ b/FunGame.Core/Api/Utility/Factory.cs
@@ -16,11 +16,11 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// Entity类
/// 构造函数的参数
///
- public static object? GetInstance(params object[]? objs)
+ public static T? GetInstance(params object[]? objs)
{
- if (!IsEntity()) return null;
+ if (!IsEntity()) return default;
object? instance = null;
- if (objs is null || objs.Length == 0) return instance;
+ if (objs is null || objs.Length == 0) return (T?)instance;
if (typeof(T) == typeof(Entity.User))
{
instance = Api.Factory.UserFactory.GetInstance("Mili");
@@ -29,7 +29,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
{
}
- return instance;
+ return (T?)instance;
}
///
@@ -41,11 +41,11 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// Entity类
/// 构造函数的参数
///
- public static object New(params object[]? objs)
+ public static T New(params object[]? objs)
{
object instance = General.EntityInstance;
- if (!IsEntity()) return instance;
- if (objs is null || objs.Length == 0) return instance;
+ if (!IsEntity()) return (T)instance;
+ if (objs is null || objs.Length == 0) return (T)instance;
if (typeof(T) == typeof(Entity.User))
{
instance = Api.Factory.UserFactory.GetInstance("Mili");
@@ -54,7 +54,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
{
}
- return instance;
+ return (T)instance;
}
///
diff --git a/FunGame.Core/Interface/Base/ISocket.cs b/FunGame.Core/Interface/Base/ISocket.cs
new file mode 100644
index 0000000..7f4c775
--- /dev/null
+++ b/FunGame.Core/Interface/Base/ISocket.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Milimoe.FunGame.Core.Library.Common.Network;
+using Milimoe.FunGame.Core.Library.Constant;
+
+namespace Milimoe.FunGame.Core.Interface.Base
+{
+ public interface ISocket
+ {
+ public System.Net.Sockets.Socket Instance { get; }
+ public int Runtime { get; }
+ public string ServerIP { get; }
+ public int ServerPort { get; }
+ public string ServerName { get; }
+ public string ServerNotice { get; }
+ public int HeartBeatFaileds { get; }
+ public bool Connected
+ {
+ get
+ {
+ return Instance != null && Instance.Connected;
+ }
+ }
+ public bool Receiving { get; }
+ public bool SendingHeartBeat { get; }
+ public SocketResult Send(SocketMessageType type, params object[] objs);
+ public object[] Receive();
+ public void Close();
+ public void StartReceiving(Task t);
+ }
+}
diff --git a/FunGame.Core/Library/Common/Network/ClientSocket.cs b/FunGame.Core/Library/Common/Network/ClientSocket.cs
new file mode 100644
index 0000000..b617695
--- /dev/null
+++ b/FunGame.Core/Library/Common/Network/ClientSocket.cs
@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Milimoe.FunGame.Core.Interface.Base;
+using Milimoe.FunGame.Core.Library.Constant;
+using Milimoe.FunGame.Core.Service;
+
+namespace Milimoe.FunGame.Core.Library.Common.Network
+{
+ public class ClientSocket : ISocket
+ {
+ public System.Net.Sockets.Socket Instance { get; }
+ public int Runtime { get; } = (int)SocketRuntimeType.Server;
+ public string ServerIP { get; } = "";
+ public int ServerPort { get; } = 0;
+ public string ServerName { get; } = "";
+ public string ServerNotice { get; } = "";
+ public string ClientIP { get; } = "";
+ public string ClientName { get; private set; } = "";
+ public int HeartBeatFaileds { get; private set; } = 0;
+ public bool Connected
+ {
+ get
+ {
+ return Instance != null && Instance.Connected;
+ }
+ }
+ public bool Receiving { get; private set; } = false;
+ public bool SendingHeartBeat { get; private set; } = false;
+
+ private Task? ReceivingTask;
+
+ public ClientSocket(System.Net.Sockets.Socket Instance, int ServerPort, string ClientIP, string ClientName)
+ {
+ this.Instance= Instance;
+ this.ServerPort = ServerPort;
+ this.ClientIP = ClientIP;
+ this.ClientName = ClientName;
+ }
+
+ public void Close()
+ {
+ StopReceiving();
+ Instance?.Close();
+ }
+
+ public object[] Receive()
+ {
+ object[] result = SocketManager.Receive(Instance);
+ if (result.Length != 2) throw new System.Exception("收到错误的返回信息。");
+ return result;
+ }
+
+ public SocketResult Send(SocketMessageType type, params object[] objs)
+ {
+ if (Instance != null)
+ {
+ if (SocketManager.Send(Instance, type, objs) == SocketResult.Success)
+ {
+ return SocketResult.Success;
+ }
+ else return SocketResult.Fail;
+ }
+ return SocketResult.NotSent;
+ }
+
+ public void StartReceiving(Task t)
+ {
+ Receiving = true;
+ ReceivingTask = t;
+ }
+
+ public void StopReceiving()
+ {
+ Receiving = false;
+ ReceivingTask?.Wait(1);
+ ReceivingTask = null;
+ }
+ }
+}
diff --git a/FunGame.Core/Library/Common/Network/JsonObject.cs b/FunGame.Core/Library/Common/Network/JsonObject.cs
new file mode 100644
index 0000000..1c8ce9b
--- /dev/null
+++ b/FunGame.Core/Library/Common/Network/JsonObject.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using Milimoe.FunGame.Core.Library.Constant;
+
+namespace Milimoe.FunGame.Core.Library.Common.Network
+{
+ [Serializable]
+ internal class JsonObject
+ {
+ internal SocketMessageType MessageType { get; } = SocketMessageType.Unknown;
+ internal object[] Parameters { get; }
+ internal string JsonString { get; }
+
+ internal JsonObject(SocketMessageType MessageType, object[] Parameters)
+ {
+ this.MessageType = MessageType;
+ this.Parameters = Parameters;
+ this.JsonString = JsonSerializer.Serialize(this);
+ }
+
+ internal static string GetString(SocketMessageType MessageType, object[] Parameters)
+ {
+ return new JsonObject(MessageType, Parameters).JsonString;
+ }
+
+ internal static JsonObject? GetObject(string JsonString)
+ {
+ return JsonSerializer.Deserialize(JsonString);
+ }
+ }
+}
diff --git a/FunGame.Core/Library/Common/Network/ServerSocket.cs b/FunGame.Core/Library/Common/Network/ServerSocket.cs
new file mode 100644
index 0000000..7df6131
--- /dev/null
+++ b/FunGame.Core/Library/Common/Network/ServerSocket.cs
@@ -0,0 +1,77 @@
+using Milimoe.FunGame.Core.Interface.Base;
+using Milimoe.FunGame.Core.Library.Constant;
+using Milimoe.FunGame.Core.Service;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Milimoe.FunGame.Core.Library.Common.Network
+{
+ public class ServerSocket : ISocket
+ {
+ public System.Net.Sockets.Socket Instance { get; }
+ public int Runtime { get; } = (int)SocketRuntimeType.Server;
+ public string ServerIP { get; } = "";
+ public int ServerPort { get; } = 0;
+ public string ServerName { get; } = "";
+ public string ServerNotice { get; } = "";
+ public int HeartBeatFaileds { get; private set; } = 0;
+ public bool Connected
+ {
+ get
+ {
+ return Instance != null && Instance.Connected;
+ }
+ }
+ public bool Receiving { get; private set; } = false;
+ public bool SendingHeartBeat { get; private set; } = false;
+
+ private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort)
+ {
+ this.Instance = Instance;
+ this.ServerPort = ServerPort;
+ }
+
+ public ServerSocket StartListening(int Port = 22222, int MaxConnection = 20)
+ {
+ System.Net.Sockets.Socket? socket = SocketManager.StartListening(Port, MaxConnection);
+ if (socket != null) return new ServerSocket(socket, Port);
+ else throw new System.Exception("无法创建监听,请重新启动服务器再试。");
+ }
+
+ public ClientSocket Accept()
+ {
+ object[] result = SocketManager.Accept();
+ if (result != null && result.Length == 2)
+ {
+ string ClientIP = (string)result[0];
+ System.Net.Sockets.Socket Client = (System.Net.Sockets.Socket)result[1];
+ return new ClientSocket(Client, ServerPort, ClientIP, ClientIP);
+ }
+ throw new System.Exception("无法获取客户端信息。");
+ }
+
+ public SocketResult Send(SocketMessageType type, params object[] objs)
+ {
+ throw new System.Exception("监听Socket不能用于发送信息。");
+ }
+
+ public object[] Receive()
+ {
+ throw new System.Exception("监听Socket不能用于接收信息。");
+ }
+
+ public void Close()
+ {
+ Instance?.Close();
+ }
+
+ public void StartReceiving(Task t)
+ {
+ throw new System.Exception("监听Socket不能用于接收信息。");
+ }
+ }
+}
diff --git a/FunGame.Core/Library/Common/Network/Socket.cs b/FunGame.Core/Library/Common/Network/Socket.cs
index c8b0086..14da2ab 100644
--- a/FunGame.Core/Library/Common/Network/Socket.cs
+++ b/FunGame.Core/Library/Common/Network/Socket.cs
@@ -12,7 +12,7 @@ using Milimoe.FunGame.Core.Interface.Base;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
- public class Socket
+ public class Socket : ISocket
{
public System.Net.Sockets.Socket Instance { get; }
public int Runtime { get; } = (int)SocketRuntimeType.Client;
@@ -49,12 +49,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
if (socket != null) return new Socket(socket, IP, Port);
else throw new System.Exception("连接到服务器失败。");
}
-
- public SocketResult Send(SocketMessageType type, string msg = "")
+
+ public SocketResult Send(SocketMessageType type, params object[] objs)
{
if (Instance != null)
{
- if (SocketManager.Send(type, msg) == SocketResult.Success)
+ if (SocketManager.Send(type, objs) == SocketResult.Success)
{
return SocketResult.Success;
}
@@ -63,10 +63,11 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
return SocketResult.NotSent;
}
- public string[] Receive()
+ public object[] Receive()
{
- string[] result = SocketManager.Receive();
- if (result[0] == SocketSet.HeartBeat)
+ object[] result = SocketManager.Receive();
+ if (result.Length != 2) throw new System.Exception("收到错误的返回信息。");
+ if ((string)result[0] == SocketSet.HeartBeat)
{
if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1);
HeartBeatFaileds = 0;
diff --git a/FunGame.Core/Service/SocketManager.cs b/FunGame.Core/Service/SocketManager.cs
index f7700e6..6ad909c 100644
--- a/FunGame.Core/Service/SocketManager.cs
+++ b/FunGame.Core/Service/SocketManager.cs
@@ -7,33 +7,94 @@ using Milimoe.FunGame.Core.Interface.Base;
using System.Collections;
using System.Net.Sockets;
using System.Net;
+using System.Text.Json;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Service
{
internal class SocketManager
{
+ ///
+ /// 客户端专用Socket
+ ///
internal static Socket? Socket { get; private set; } = null;
+ ///
+ /// 服务器端专用Socket
+ ///
+ internal static Socket? ServerSocket { get; private set; } = null;
+
+ ///
+ /// 创建服务器监听Socket
+ ///
+ /// 监听端口号
+ /// 最大连接数量
+ /// 服务器端专用Socket
+ internal static Socket? StartListening(int Port = 22222, int MaxConnection = 20)
+ {
+ try
+ {
+ ServerSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ IPEndPoint ServerEndPoint = new(IPAddress.Any, Port);
+ ServerSocket.Bind(ServerEndPoint);
+ ServerSocket.Listen(MaxConnection);
+ return ServerSocket;
+ }
+ catch
+ {
+ ServerSocket?.Close();
+ }
+ return null;
+ }
+
+ ///
+ /// 创建一个监听到客户端Socket
+ ///
+ /// 客户端IP地址[0]和客户端Socket[1]
+ internal static object[] Accept()
+ {
+ if (ServerSocket is null) return Array.Empty