mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-23 04:29:36 +08:00
Update Controllers And Models (#18)
This commit is contained in:
parent
b08d62d6f8
commit
267bb162a1
@ -42,7 +42,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Request : BaseController
|
private class Request : BaseModel
|
||||||
{
|
{
|
||||||
public Hashtable RequestData { get; } = new();
|
public Hashtable RequestData { get; } = new();
|
||||||
public Hashtable ResultData { get; } = new();
|
public Hashtable ResultData { get; } = new();
|
||||||
|
23
Controller/RunTimeController.cs
Normal file
23
Controller/RunTimeController.cs
Normal file
@ -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<bool> GetServerConnection();
|
||||||
|
|
||||||
|
public abstract Task<ConnectResult> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,97 +1,10 @@
|
|||||||
using Milimoe.FunGame.Core.Interface.Base;
|
namespace Milimoe.FunGame.Core.Library.Common.Architecture
|
||||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
|
||||||
using Milimoe.FunGame.Core.Service;
|
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Core.Library.Common.Architecture
|
|
||||||
{
|
{
|
||||||
public class BaseController : ISocketHandler, IDisposable
|
public abstract class BaseController
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 接收到的SocketObject实例
|
/// 重写此方法并调用Model的Dispose方法,否则将无法正常将监听Socket的事件移除!
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual SocketObject Work { get; set; }
|
public abstract void Dispose();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 是否处于等待服务器响应的状态
|
|
||||||
/// </summary>
|
|
||||||
protected virtual bool Working { get; set; } = false;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Socket
|
|
||||||
/// </summary>
|
|
||||||
private readonly Socket _Socket;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 继承请调用base构造
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="socket">Socket</param>
|
|
||||||
public BaseController(Socket? socket)
|
|
||||||
{
|
|
||||||
if (socket != null)
|
|
||||||
{
|
|
||||||
_Socket = socket;
|
|
||||||
socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler));
|
|
||||||
}
|
|
||||||
else throw new SocketCreateReceivingException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 继承请重写此方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="SocketObject">SocketObject</param>
|
|
||||||
public virtual void SocketHandler(SocketObject SocketObject)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 判断是否已经Disposed
|
|
||||||
/// </summary>
|
|
||||||
private bool IsDisposed = false;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 公开的Dispose方法
|
|
||||||
/// </summary>
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
Dispose(true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 关闭时
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="Disposing"></param>
|
|
||||||
protected void Dispose(bool Disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
if (Disposing)
|
|
||||||
{
|
|
||||||
_Socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 调用Socket.Send()前,请设置为等待状态
|
|
||||||
/// </summary>
|
|
||||||
protected void SetWorking()
|
|
||||||
{
|
|
||||||
Working = true;
|
|
||||||
Work = default;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 调用Socket.Send() == Success后,请等待任务完成
|
|
||||||
/// </summary>
|
|
||||||
protected void WaitForWorkDone()
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (!Working) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
97
Library/Common/Architecture/BaseModel.cs
Normal file
97
Library/Common/Architecture/BaseModel.cs
Normal file
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 接收到的SocketObject实例
|
||||||
|
/// </summary>
|
||||||
|
protected virtual SocketObject Work { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否处于等待服务器响应的状态
|
||||||
|
/// </summary>
|
||||||
|
protected virtual bool Working { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Socket
|
||||||
|
/// </summary>
|
||||||
|
private readonly Socket _Socket;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 继承请调用base构造
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="socket">Socket</param>
|
||||||
|
public BaseModel(Socket? socket)
|
||||||
|
{
|
||||||
|
if (socket != null)
|
||||||
|
{
|
||||||
|
_Socket = socket;
|
||||||
|
socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler));
|
||||||
|
}
|
||||||
|
else throw new SocketCreateReceivingException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 继承请重写此方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="SocketObject">SocketObject</param>
|
||||||
|
public virtual void SocketHandler(SocketObject SocketObject)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断是否已经Disposed
|
||||||
|
/// </summary>
|
||||||
|
private bool IsDisposed = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公开的Dispose方法
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关闭时
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Disposing"></param>
|
||||||
|
protected void Dispose(bool Disposing)
|
||||||
|
{
|
||||||
|
if (!IsDisposed)
|
||||||
|
{
|
||||||
|
if (Disposing)
|
||||||
|
{
|
||||||
|
_Socket.BindEvent(new SocketManager.SocketReceiveHandler(SocketHandler), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IsDisposed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用Socket.Send()前,请设置为等待状态
|
||||||
|
/// </summary>
|
||||||
|
protected void SetWorking()
|
||||||
|
{
|
||||||
|
Working = true;
|
||||||
|
Work = default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用Socket.Send() == Success后,请等待任务完成
|
||||||
|
/// </summary>
|
||||||
|
protected void WaitForWorkDone()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (!Working) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ using Milimoe.FunGame.Core.Library.Constant;
|
|||||||
|
|
||||||
namespace Milimoe.FunGame.Core.Model
|
namespace Milimoe.FunGame.Core.Model
|
||||||
{
|
{
|
||||||
public class RoomListModel : IEnumerable
|
public class RoomList : IEnumerable
|
||||||
{
|
{
|
||||||
private readonly Hashtable _List = new();
|
private readonly Hashtable _List = new();
|
||||||
private readonly Hashtable _PlayerList = new();
|
private readonly Hashtable _PlayerList = new();
|
||||||
@ -15,7 +15,7 @@ namespace Milimoe.FunGame.Core.Model
|
|||||||
|
|
||||||
public List<string> ListRoomID => _List.Keys.Cast<string>().ToList();
|
public List<string> ListRoomID => _List.Keys.Cast<string>().ToList();
|
||||||
|
|
||||||
public RoomListModel()
|
public RoomList()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
140
Model/RunTime.cs
Normal file
140
Model/RunTime.cs
Normal file
@ -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<ConnectResult> 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<SocketObject>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +0,0 @@
|
|||||||
namespace Milimoe.FunGame.Core.Model
|
|
||||||
{
|
|
||||||
public class RunTimeModel
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
14
Model/Session.cs
Normal file
14
Model/Session.cs
Normal file
@ -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; // 所处的房间
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
namespace Milimoe.FunGame.Core.Model
|
|
||||||
{
|
|
||||||
public class SessionModel
|
|
||||||
{
|
|
||||||
public SessionModel()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user