From 267bb162a1d3bd47c221a45a0c10e5f6e274a132 Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Sun, 23 Apr 2023 01:25:16 +0800 Subject: [PATCH] Update Controllers And Models (#18) --- Api/Transmittal/DataRequest.cs | 2 +- Controller/RunTimeController.cs | 23 +++ Library/Common/Architecture/BaseController.cs | 97 +----------- Library/Common/Architecture/BaseModel.cs | 97 ++++++++++++ Model/{RoomListModel.cs => RoomList.cs} | 4 +- Model/RunTime.cs | 140 ++++++++++++++++++ Model/RunTimeModel.cs | 7 - Model/Session.cs | 14 ++ Model/SessionModel.cs | 10 -- 9 files changed, 282 insertions(+), 112 deletions(-) create mode 100644 Controller/RunTimeController.cs create mode 100644 Library/Common/Architecture/BaseModel.cs rename Model/{RoomListModel.cs => RoomList.cs} (97%) create mode 100644 Model/RunTime.cs delete mode 100644 Model/RunTimeModel.cs create mode 100644 Model/Session.cs delete mode 100644 Model/SessionModel.cs diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs index ea9c809..7fb4e84 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 : BaseController + private class Request : BaseModel { public Hashtable RequestData { get; } = new(); public Hashtable ResultData { get; } = new(); diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs new file mode 100644 index 0000000..25b1777 --- /dev/null +++ b/Controller/RunTimeController.cs @@ -0,0 +1,23 @@ +using Milimoe.FunGame.Core.Library.Constant; + +namespace Milimoe.FunGame.Core.Controller +{ + public abstract class RunTimeController + { + public abstract bool Connected { get; } + + public abstract Task GetServerConnection(); + + public abstract Task Connect(); + + public abstract bool Disconnect(); + + public abstract bool Close(Exception? e = null); + + public abstract bool Error(Exception e); + + public abstract Task AutoLogin(string Username, string Password, string AutoKey); + + public abstract void WritelnSystemInfo(string msg); + } +} diff --git a/Library/Common/Architecture/BaseController.cs b/Library/Common/Architecture/BaseController.cs index b7977b3..10fe112 100644 --- a/Library/Common/Architecture/BaseController.cs +++ b/Library/Common/Architecture/BaseController.cs @@ -1,97 +1,10 @@ -using Milimoe.FunGame.Core.Interface.Base; -using Milimoe.FunGame.Core.Library.Common.Network; -using Milimoe.FunGame.Core.Service; - -namespace Milimoe.FunGame.Core.Library.Common.Architecture +namespace Milimoe.FunGame.Core.Library.Common.Architecture { - public class BaseController : ISocketHandler, IDisposable + public abstract class BaseController { /// - /// 接收到的SocketObject实例 + /// 重写此方法并调用Model的Dispose方法,否则将无法正常将监听Socket的事件移除! /// - protected virtual SocketObject Work { get; set; } - - /// - /// 是否处于等待服务器响应的状态 - /// - protected virtual bool Working { get; set; } = false; - - /// - /// Socket - /// - private readonly Socket _Socket; - - /// - /// 继承请调用base构造 - /// - /// Socket - public BaseController(Socket? socket) - { - if (socket != null) - { - _Socket = socket; - socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler)); - } - else throw new SocketCreateReceivingException(); - } - - /// - /// 继承请重写此方法 - /// - /// SocketObject - public virtual void SocketHandler(SocketObject SocketObject) - { - - } - - /// - /// 判断是否已经Disposed - /// - private bool IsDisposed = false; - - /// - /// 公开的Dispose方法 - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// 关闭时 - /// - /// - protected void Dispose(bool Disposing) - { - if (!IsDisposed) - { - if (Disposing) - { - _Socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler), true); - } - } - IsDisposed = true; - } - - /// - /// 调用Socket.Send()前,请设置为等待状态 - /// - protected void SetWorking() - { - Working = true; - Work = default; - } - - /// - /// 调用Socket.Send() == Success后,请等待任务完成 - /// - protected void WaitForWorkDone() - { - while (true) - { - if (!Working) break; - } - } + public abstract void Dispose(); } -} +} \ No newline at end of file diff --git a/Library/Common/Architecture/BaseModel.cs b/Library/Common/Architecture/BaseModel.cs new file mode 100644 index 0000000..f791fdb --- /dev/null +++ b/Library/Common/Architecture/BaseModel.cs @@ -0,0 +1,97 @@ +using Milimoe.FunGame.Core.Interface.Base; +using Milimoe.FunGame.Core.Library.Common.Network; +using Milimoe.FunGame.Core.Service; + +namespace Milimoe.FunGame.Core.Library.Common.Architecture +{ + public class BaseModel : ISocketHandler, IDisposable + { + /// + /// 接收到的SocketObject实例 + /// + protected virtual SocketObject Work { get; set; } + + /// + /// 是否处于等待服务器响应的状态 + /// + protected virtual bool Working { get; set; } = false; + + /// + /// Socket + /// + private readonly Socket _Socket; + + /// + /// 继承请调用base构造 + /// + /// Socket + public BaseModel(Socket? socket) + { + if (socket != null) + { + _Socket = socket; + socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler)); + } + else throw new SocketCreateReceivingException(); + } + + /// + /// 继承请重写此方法 + /// + /// SocketObject + public virtual void SocketHandler(SocketObject SocketObject) + { + + } + + /// + /// 判断是否已经Disposed + /// + private bool IsDisposed = false; + + /// + /// 公开的Dispose方法 + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// 关闭时 + /// + /// + protected void Dispose(bool Disposing) + { + if (!IsDisposed) + { + if (Disposing) + { + _Socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler), true); + } + } + IsDisposed = true; + } + + /// + /// 调用Socket.Send()前,请设置为等待状态 + /// + protected void SetWorking() + { + Working = true; + Work = default; + } + + /// + /// 调用Socket.Send() == Success后,请等待任务完成 + /// + protected void WaitForWorkDone() + { + while (true) + { + if (!Working) break; + } + } + } +} diff --git a/Model/RoomListModel.cs b/Model/RoomList.cs similarity index 97% rename from Model/RoomListModel.cs rename to Model/RoomList.cs index d21aff1..7a5958c 100644 --- a/Model/RoomListModel.cs +++ b/Model/RoomList.cs @@ -4,7 +4,7 @@ using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Model { - public class RoomListModel : IEnumerable + public class RoomList : IEnumerable { private readonly Hashtable _List = new(); private readonly Hashtable _PlayerList = new(); @@ -15,7 +15,7 @@ namespace Milimoe.FunGame.Core.Model public List ListRoomID => _List.Keys.Cast().ToList(); - public RoomListModel() + public RoomList() { } diff --git a/Model/RunTime.cs b/Model/RunTime.cs new file mode 100644 index 0000000..dd0537d --- /dev/null +++ b/Model/RunTime.cs @@ -0,0 +1,140 @@ +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Controller; +using Milimoe.FunGame.Core.Library.Common.Network; +using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Library.Exception; + +namespace Milimoe.FunGame.Core.Model +{ + public abstract class RunTime + { + public abstract Socket? Socket { get; } + public bool Connected => _Socket != null && _Socket.Connected; + + protected Task? _ReceivingTask; + protected Socket? _Socket; + protected bool _IsReceiving; + protected RunTimeController? _Controller; + + public bool Disconnect() + { + bool result = false; + + try + { + result = _Socket?.Send(SocketMessageType.Disconnect, "") == SocketResult.Success; + } + catch (Exception e) + { + _Controller?.WritelnSystemInfo(e.GetErrorInfo()); + } + + return result; + } + + public void Disconnected() + { + Disconnect(); + } + + public abstract void GetServerConnection(); + + public abstract Task Connect(); + + public bool Close() + { + try + { + if (_Socket != null) + { + _Socket.Close(); + _Socket = null; + } + if (_ReceivingTask != null && !_ReceivingTask.IsCompleted) + { + _ReceivingTask.Wait(1); + _ReceivingTask = null; + _IsReceiving = false; + } + } + catch (Exception e) + { + _Controller?.WritelnSystemInfo(e.GetErrorInfo()); + return false; + } + return true; + } + + public abstract void Error(Exception e); + + protected void StartReceiving() + { + _ReceivingTask = Task.Factory.StartNew(() => + { + Thread.Sleep(100); + _IsReceiving = true; + while (Connected) + { + Receiving(); + } + }); + _Socket?.StartReceiving(_ReceivingTask); + } + + protected SocketObject[] GetServerMessage() + { + if (_Socket != null && _Socket.Connected) + { + return _Socket.ReceiveArray(); + } + return Array.Empty(); + } + + protected SocketMessageType Receiving() + { + if (_Socket is null) return SocketMessageType.Unknown; + SocketMessageType result = SocketMessageType.Unknown; + try + { + SocketObject[] ServerMessages = GetServerMessage(); + + foreach (SocketObject ServerMessage in ServerMessages) + { + SocketMessageType type = ServerMessage.SocketType; + object[] objs = ServerMessage.Parameters; + result = type; + switch (type) + { + case SocketMessageType.Connect: + if (!SocketHandler_Connect(ServerMessage)) return SocketMessageType.Unknown; + break; + + case SocketMessageType.Disconnect: + SocketHandler_Disconnect(ServerMessage); + break; + + case SocketMessageType.HeartBeat: + SocketHandler_HeartBeat(ServerMessage); + break; + + case SocketMessageType.Unknown: + default: + break; + } + } + } + catch (Exception e) + { + // 报错中断服务器连接 + Error(e); + } + return result; + } + + protected abstract bool SocketHandler_Connect(SocketObject ServerMessage); + + protected abstract void SocketHandler_Disconnect(SocketObject ServerMessage); + + protected abstract void SocketHandler_HeartBeat(SocketObject ServerMessage); + } +} diff --git a/Model/RunTimeModel.cs b/Model/RunTimeModel.cs deleted file mode 100644 index b6b4f58..0000000 --- a/Model/RunTimeModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Milimoe.FunGame.Core.Model -{ - public class RunTimeModel - { - - } -} diff --git a/Model/Session.cs b/Model/Session.cs new file mode 100644 index 0000000..faf863f --- /dev/null +++ b/Model/Session.cs @@ -0,0 +1,14 @@ +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Constant; + +namespace Milimoe.FunGame.Core.Model +{ + public class Session + { + public Guid SocketToken { get; set; } = Guid.Empty; // SocketToken + public Guid LoginKey { get; set; } = Guid.Empty; // LoginKey + public User LoginUser { get; set; } = General.UnknownUserInstance; // 已登录的用户 + public string LoginUserName { get; set; } = ""; // 已登录用户名 + public Room InRoom { get; set; } = General.HallInstance; // 所处的房间 + } +} diff --git a/Model/SessionModel.cs b/Model/SessionModel.cs deleted file mode 100644 index dbcfdcd..0000000 --- a/Model/SessionModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Milimoe.FunGame.Core.Model -{ - public class SessionModel - { - public SessionModel() - { - - } - } -}