Add SocketObject

This commit is contained in:
Mili 2023-03-09 23:44:40 +08:00
parent 36c15cb062
commit f0362e7257
15 changed files with 84 additions and 146 deletions

View File

@ -1,5 +1,4 @@
using System.Collections; using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Interface.Base namespace Milimoe.FunGame.Core.Interface.Base
{ {
@ -15,7 +14,7 @@ namespace Milimoe.FunGame.Core.Interface.Base
public bool Connected => Instance != null && Instance.Connected; public bool Connected => Instance != null && Instance.Connected;
public bool Receiving { get; } public bool Receiving { get; }
public SocketResult Send(SocketMessageType type, params object[] objs); public SocketResult Send(SocketMessageType type, params object[] objs);
public object[] Receive(); public Library.Common.Network.SocketObject Receive();
public void Close(); public void Close();
public void StartReceiving(Task t); public void StartReceiving(Task t);
} }

View File

@ -37,11 +37,16 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
Instance?.Close(); Instance?.Close();
} }
public object[] Receive() public SocketObject Receive()
{ {
object[] result = SocketManager.Receive(Instance); try
if (result.Length != 3) throw new SocketWrongInfoException(); {
return result; return SocketManager.Receive(Instance);
}
catch
{
throw new SocketWrongInfoException();
}
} }
public SocketResult Send(SocketMessageType type, params object[] objs) public SocketResult Send(SocketMessageType type, params object[] objs)

View File

@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public string JsonString { get; } public string JsonString { get; }
[JsonConstructor] [JsonConstructor]
public JsonObject(SocketMessageType MessageType, Guid Token, object[] Parameters) public JsonObject(SocketMessageType MessageType, Guid Token, params object[] Parameters)
{ {
this.MessageType = MessageType; this.MessageType = MessageType;
this.Token = Token; this.Token = Token;
@ -21,7 +21,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
this.JsonString = JsonSerializer.Serialize(this); this.JsonString = JsonSerializer.Serialize(this);
} }
public static string GetString(SocketMessageType MessageType, Guid Token, object[] Parameters) public static string GetString(SocketMessageType MessageType, Guid Token, params object[] Parameters)
{ {
return new JsonObject(MessageType, Token, Parameters).JsonString; return new JsonObject(MessageType, Token, Parameters).JsonString;
} }

View File

@ -77,7 +77,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
throw new ListeningSocketCanNotSendException(); throw new ListeningSocketCanNotSendException();
} }
public object[] Receive() public SocketObject Receive()
{ {
throw new ListeningSocketCanNotSendException(); throw new ListeningSocketCanNotSendException();
} }

View File

@ -54,16 +54,22 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
return SocketResult.NotSent; return SocketResult.NotSent;
} }
public object[] Receive() public SocketObject Receive()
{ {
object[] result = SocketManager.Receive(); try
if (result.Length != 2) throw new SocketWrongInfoException();
if ((SocketMessageType)result[0] == SocketMessageType.HeartBeat)
{ {
if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1); SocketObject result = SocketManager.Receive();
_HeartBeatFaileds = 0; if (result.SocketType == SocketMessageType.HeartBeat)
{
if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1);
_HeartBeatFaileds = 0;
}
return result;
}
catch
{
throw new SocketWrongInfoException();
} }
return result;
} }
public void CheckHeartBeatFaileds() public void CheckHeartBeatFaileds()

View File

@ -0,0 +1,18 @@
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
public readonly struct SocketObject
{
public SocketMessageType SocketType { get; } = SocketMessageType.Unknown;
public Guid Token { get; } = Guid.Empty;
public object[] Parameters { get; } = Array.Empty<object>();
public SocketObject(SocketMessageType type, Guid token, params object[] parameters)
{
SocketType = type;
Token = token;
Parameters = parameters;
}
}
}

View File

@ -37,6 +37,9 @@
public const string Chat = "Chat"; public const string Chat = "Chat";
public const string Reg = "Reg"; public const string Reg = "Reg";
public const string CheckReg = "CheckReg"; public const string CheckReg = "CheckReg";
public const string CreateRoom = "CreateRoom";
public const string UpdateRoom = "UpdateRoom";
public const string ChangeRoomSetting = "ChangeRoomSetting";
} }
public class ReflectionSet public class ReflectionSet

View File

@ -61,7 +61,10 @@
QuitRoom, QuitRoom,
Chat, Chat,
Reg, Reg,
CheckReg CheckReg,
CreateRoom,
UpdateRoom,
ChangeRoomSetting
} }
public enum SocketRuntimeType public enum SocketRuntimeType

View File

@ -156,9 +156,9 @@ namespace Milimoe.FunGame.Core.Service
/// 用于客户端接收服务器信息 /// 用于客户端接收服务器信息
/// </summary> /// </summary>
/// <returns>通信类型[0]和参数[1]</returns> /// <returns>通信类型[0]和参数[1]</returns>
internal static object[] Receive() internal static Library.Common.Network.SocketObject Receive()
{ {
object[] result = Array.Empty<object>(); Library.Common.Network.SocketObject result = default;
if (Socket != null) if (Socket != null)
{ {
// 从服务器接收消息 // 从服务器接收消息
@ -170,7 +170,7 @@ namespace Milimoe.FunGame.Core.Service
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg); Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
if (json != null) if (json != null)
{ {
result = new object[] { json.MessageType, json.Parameters }; result = new Library.Common.Network.SocketObject(json.MessageType, json.Token, json.Parameters);
} }
return result; return result;
} }
@ -183,9 +183,9 @@ namespace Milimoe.FunGame.Core.Service
/// </summary> /// </summary>
/// <param name="ClientSocket">客户端Socket</param> /// <param name="ClientSocket">客户端Socket</param>
/// <returns>通信类型[0]、Token[1]和参数[2]</returns> /// <returns>通信类型[0]、Token[1]和参数[2]</returns>
internal static object[] Receive(Socket ClientSocket) internal static Library.Common.Network.SocketObject Receive(Socket ClientSocket)
{ {
object[] result = Array.Empty<object>(); Library.Common.Network.SocketObject result = default;
if (ClientSocket != null) if (ClientSocket != null)
{ {
// 从客户端接收消息 // 从客户端接收消息
@ -197,7 +197,7 @@ namespace Milimoe.FunGame.Core.Service
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg); Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
if (json != null) if (json != null)
{ {
result = new object[] { json.MessageType, json.Token, json.Parameters }; result = new Library.Common.Network.SocketObject(json.MessageType, json.Token, json.Parameters);
} }
return result; return result;
} }
@ -226,6 +226,9 @@ namespace Milimoe.FunGame.Core.Service
SocketMessageType.Chat => SocketSet.Chat, SocketMessageType.Chat => SocketSet.Chat,
SocketMessageType.Reg => SocketSet.Reg, SocketMessageType.Reg => SocketSet.Reg,
SocketMessageType.CheckReg => SocketSet.CheckReg, SocketMessageType.CheckReg => SocketSet.CheckReg,
SocketMessageType.CreateRoom => SocketSet.CreateRoom,
SocketMessageType.UpdateRoom => SocketSet.UpdateRoom,
SocketMessageType.ChangeRoomSetting => SocketSet.ChangeRoomSetting,
_ => SocketSet.Unknown, _ => SocketSet.Unknown,
}; };
} }
@ -241,7 +244,7 @@ namespace Milimoe.FunGame.Core.Service
/// <param name="type">通信类型</param> /// <param name="type">通信类型</param>
/// <param name="objs">参数</param> /// <param name="objs">参数</param>
/// <returns>结果</returns> /// <returns>结果</returns>
internal delegate Task<T> SocketHandler<T>(SocketMessageType type, params object[] objs); internal delegate Task<T> SocketHandler<T>(Library.Common.Network.SocketObject SocketObject);
/// <summary> /// <summary>
/// 异步监听事件 /// 异步监听事件
@ -249,123 +252,23 @@ namespace Milimoe.FunGame.Core.Service
/// <param name="type">通信类型</param> /// <param name="type">通信类型</param>
/// <param name="objs">参数</param> /// <param name="objs">参数</param>
/// <returns>线程</returns> /// <returns>线程</returns>
internal delegate Task SocketHandler(SocketMessageType type, params object[] objs); internal delegate Task SocketHandler(Library.Common.Network.SocketObject SocketObject);
/// <summary>
/// 监听返回值为bool的事件
/// </summary>
internal static event SocketHandler<bool>? SocketReceiveBoolAsync;
/// <summary>
/// 监听返回值为String的事件
/// </summary>
internal static event SocketHandler<string>? SocketReceiveStringAsync;
/// <summary>
/// 监听返回值为object的事件
/// </summary>
internal static event SocketHandler<object>? SocketReceiveObjectAsync;
/// <summary>
/// 监听返回值为int的事件
/// </summary>
internal static event SocketHandler<int>? SocketReceiveIntAsync;
/// <summary>
/// 监听返回值为decimal的事件
/// </summary>
internal static event SocketHandler<decimal>? SocketReceiveDecimalAsync;
/// <summary> /// <summary>
/// 监听没有返回值的事件 /// 监听没有返回值的事件
/// </summary> /// </summary>
internal static event SocketHandler? SocketReceiveAsync; internal static event SocketHandler? SocketReceiveAsync;
/// <summary>
/// 触发异步返回bool事件
/// </summary>
/// <param name="type">通信类型</param>
/// <param name="objs">参数</param>
/// <returns>bool结果</returns>
internal static async Task<bool> OnSocketReceiveBoolAsync(SocketMessageType type, params object[] objs)
{
if (SocketReceiveBoolAsync != null)
{
return await SocketReceiveBoolAsync.Invoke(type, objs);
}
return false;
}
/// <summary>
/// 触发异步返回string事件
/// </summary>
/// <param name="type">通信类型</param>
/// <param name="objs">参数</param>
/// <returns>string结果</returns>
internal static async Task<string> OnSocketReceiveStringAsync(SocketMessageType type, params object[] objs)
{
if (SocketReceiveStringAsync != null)
{
return await SocketReceiveStringAsync.Invoke(type, objs);
}
return "";
}
/// <summary>
/// 触发异步返回object事件
/// </summary>
/// <param name="type">通信类型</param>
/// <param name="objs">参数</param>
/// <returns>object结果</returns>
internal static async Task<object> OnSocketReceiveObjectAsync(SocketMessageType type, params object[] objs)
{
if (SocketReceiveObjectAsync != null)
{
return await SocketReceiveObjectAsync.Invoke(type, objs);
}
return General.EntityInstance;
}
/// <summary>
/// 触发异步返回int事件
/// </summary>
/// <param name="type">通信类型</param>
/// <param name="objs">参数</param>
/// <returns>int结果</returns>
internal static async Task<int> OnSocketReceiveIntAsync(SocketMessageType type, params object[] objs)
{
if (SocketReceiveIntAsync != null)
{
return await SocketReceiveIntAsync.Invoke(type, objs);
}
return -1;
}
/// <summary>
/// 触发异步返回decimal事件
/// </summary>
/// <param name="type">通信类型</param>
/// <param name="objs">参数</param>
/// <returns>decimal结果</returns>
internal static async Task<decimal> OnSocketReceiveDecimalAsync(SocketMessageType type, params object[] objs)
{
if (SocketReceiveDecimalAsync != null)
{
return await SocketReceiveDecimalAsync.Invoke(type, objs);
}
return -1;
}
/// <summary> /// <summary>
/// 触发异步无返回值事件 /// 触发异步无返回值事件
/// </summary> /// </summary>
/// <param name="type">通信类型</param> /// <param name="type">通信类型</param>
/// <param name="objs">参数</param> /// <param name="objs">参数</param>
internal static async Task OnSocketReceiveAsync(SocketMessageType type, params object[] objs) internal static void OnSocketReceiveAsync(Library.Common.Network.SocketObject SocketObject)
{ {
if (SocketReceiveAsync != null) if (SocketReceiveAsync != null)
{ {
await SocketReceiveAsync.Invoke(type, objs); SocketReceiveAsync.Invoke(SocketObject);
} }
} }

View File

@ -3,11 +3,11 @@ using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Model; using Milimoe.FunGame.Desktop.Model;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Desktop.UI; using Milimoe.FunGame.Desktop.UI;
using Milimoe.FunGame.Desktop.Library.Interface; using Milimoe.FunGame.Core.Library.Common.Network;
namespace Milimoe.FunGame.Desktop.Controller namespace Milimoe.FunGame.Desktop.Controller
{ {
public class RegisterController : ISocketCallBack public class RegisterController
{ {
private readonly Register Register; private readonly Register Register;
private readonly RegisterModel RegModel; private readonly RegisterModel RegModel;

View File

@ -1,9 +0,0 @@
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Desktop.Library.Interface
{
public interface ISocketCallBack
{
public void SocketHandler(SocketMessageType type, params object[]? objs);
}
}

View File

@ -0,0 +1,9 @@
using Milimoe.FunGame.Core.Library.Common.Network;
namespace Milimoe.FunGame.Desktop.Library.Interface
{
public interface ISocketHandler
{
public void SocketHandler(SocketObject SocketObject);
}
}

View File

@ -1,6 +1,7 @@
using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Desktop.Controller;
@ -296,13 +297,13 @@ namespace Milimoe.FunGame.Desktop.Model
Socket?.StartReceiving(ReceivingTask); Socket?.StartReceiving(ReceivingTask);
} }
private object[] GetServerMessage() private SocketObject GetServerMessage()
{ {
if (Socket != null && Socket.Connected) if (Socket != null && Socket.Connected)
{ {
return Socket.Receive(); return Socket.Receive();
} }
return new object[2] { SocketMessageType.Unknown, Array.Empty<object>() }; return new SocketObject(SocketMessageType.Unknown, Guid.Empty, Array.Empty<object>());
} }
private SocketMessageType Receiving() private SocketMessageType Receiving()
@ -311,9 +312,9 @@ namespace Milimoe.FunGame.Desktop.Model
SocketMessageType result = SocketMessageType.Unknown; SocketMessageType result = SocketMessageType.Unknown;
try try
{ {
object[] ServerMessage = GetServerMessage(); SocketObject ServerMessage = GetServerMessage();
SocketMessageType type = (SocketMessageType)ServerMessage[0]; SocketMessageType type = ServerMessage.SocketType;
object[] objs = (object[])ServerMessage[1]; object[] objs = ServerMessage.Parameters;
result = type; result = type;
switch (type) switch (type)
{ {

View File

@ -8,7 +8,7 @@ using Milimoe.FunGame.Desktop.UI;
namespace Milimoe.FunGame.Desktop.Model namespace Milimoe.FunGame.Desktop.Model
{ {
public class RegisterModel : ISocketCallBack public class RegisterModel
{ {
private readonly Register Register; private readonly Register Register;

View File

@ -10,7 +10,7 @@ using Milimoe.FunGame.Desktop.Library.Interface;
namespace Milimoe.FunGame.Desktop.UI namespace Milimoe.FunGame.Desktop.UI
{ {
public partial class Register : BaseReg, ISocketCallBack public partial class Register : BaseReg
{ {
public bool CheckReg { get; set; } = false; public bool CheckReg { get; set; } = false;
public RegisterEventArgs EventArgs { get; set; } = new RegisterEventArgs(); public RegisterEventArgs EventArgs { get; set; } = new RegisterEventArgs();