mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-22 03:59:35 +08:00
仅使用Controller,删除重复的Model;修改EventArgs的构造方法
This commit is contained in:
parent
ea682b90ff
commit
8db6fd0512
@ -1,8 +1,8 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using Milimoe.FunGame.Core.Controller;
|
||||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
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.Core.Model;
|
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Core.Api.Transmittal
|
namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||||
{
|
{
|
||||||
@ -100,7 +100,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
return GetHashtableJsonObject<T>(Worker.ResultData, key);
|
return GetHashtableJsonObject<T>(Worker.ResultData, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Request : SocketHandlerModel
|
private class Request : SocketHandlerController
|
||||||
{
|
{
|
||||||
public Hashtable RequestData { get; } = new();
|
public Hashtable RequestData { get; } = new();
|
||||||
public Hashtable ResultData => _ResultData;
|
public Hashtable ResultData => _ResultData;
|
||||||
|
@ -1,24 +1,247 @@
|
|||||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||||
|
using Milimoe.FunGame.Core.Api.Utility;
|
||||||
|
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;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Core.Controller
|
namespace Milimoe.FunGame.Core.Controller
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 此类实现服务器连接、断开连接、心跳检测、创建数据请求等功能
|
||||||
|
/// -- 需要继承并实现部分方法 --
|
||||||
|
/// </summary>
|
||||||
public abstract class RunTimeController
|
public abstract class RunTimeController
|
||||||
{
|
{
|
||||||
public abstract bool Connected { get; }
|
/// <summary>
|
||||||
|
/// 与服务器的连接套接字实例
|
||||||
|
/// </summary>
|
||||||
|
public Socket? Socket => _Socket;
|
||||||
|
|
||||||
public abstract Task<ConnectResult> Connect();
|
/// <summary>
|
||||||
|
/// 套接字是否已经连接
|
||||||
|
/// </summary>
|
||||||
|
public bool Connected => _Socket != null && _Socket.Connected;
|
||||||
|
|
||||||
public abstract bool Disconnect();
|
/// <summary>
|
||||||
|
/// 接收服务器信息的线程
|
||||||
|
/// </summary>
|
||||||
|
protected Task? _ReceivingTask;
|
||||||
|
|
||||||
public abstract bool Close(Exception? e = null);
|
/// <summary>
|
||||||
|
/// 用于类内赋值
|
||||||
|
/// </summary>
|
||||||
|
protected Socket? _Socket;
|
||||||
|
|
||||||
public abstract bool Error(Exception e);
|
/// <summary>
|
||||||
|
/// 是否正在接收服务器信息
|
||||||
|
/// </summary>
|
||||||
|
protected bool _IsReceiving;
|
||||||
|
|
||||||
public abstract Task AutoLogin(string Username, string Password, string AutoKey);
|
/// <summary>
|
||||||
|
/// 断开服务器连接
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool Disconnect()
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = _Socket?.Send(SocketMessageType.RunTime_Disconnect, "") == SocketResult.Success;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
WritelnSystemInfo(e.GetErrorInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取服务器地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>string:IP地址;int:端口号</returns>
|
||||||
|
/// <exception cref="FindServerFailedException"></exception>
|
||||||
|
public (string, int) GetServerAddress()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string? ipaddress = (string?)Implement.GetFunGameImplValue(InterfaceType.IClient, InterfaceMethod.RemoteServerIP);
|
||||||
|
if (ipaddress != null)
|
||||||
|
{
|
||||||
|
string[] s = ipaddress.Split(':');
|
||||||
|
if (s != null && s.Length > 1)
|
||||||
|
{
|
||||||
|
return (s[0], Convert.ToInt32(s[1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new FindServerFailedException();
|
||||||
|
}
|
||||||
|
catch (FindServerFailedException e)
|
||||||
|
{
|
||||||
|
WritelnSystemInfo(e.GetErrorInfo());
|
||||||
|
return ("", 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 客户端需要自行实现连接服务器的事务
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>连接结果</returns>
|
||||||
|
public abstract ConnectResult Connect();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关闭所有连接
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
WritelnSystemInfo(e.GetErrorInfo());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出消息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="msg"></param>
|
||||||
public abstract void WritelnSystemInfo(string msg);
|
public abstract void WritelnSystemInfo(string msg);
|
||||||
|
|
||||||
public abstract DataRequest NewDataRequest(DataRequestType RequestType);
|
/// <summary>
|
||||||
|
/// 自定处理异常的方法
|
||||||
|
/// -- 一般放在catch中 --
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
public abstract void Error(Exception e);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 基于本地已连接的Socket创建新的数据请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="RequestType"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ConnectFailedException"></exception>
|
||||||
|
public DataRequest NewDataRequest(DataRequestType RequestType)
|
||||||
|
{
|
||||||
|
if (_Socket != null)
|
||||||
|
{
|
||||||
|
DataRequest request = new(_Socket, RequestType);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
throw new ConnectFailedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始接收服务器信息
|
||||||
|
/// </summary>
|
||||||
|
protected void StartReceiving()
|
||||||
|
{
|
||||||
|
_ReceivingTask = Task.Factory.StartNew(() =>
|
||||||
|
{
|
||||||
|
Thread.Sleep(100);
|
||||||
|
_IsReceiving = true;
|
||||||
|
while (Connected)
|
||||||
|
{
|
||||||
|
Receiving();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
_Socket?.StartReceiving(_ReceivingTask);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取服务器已发送的信息为SocketObject数组
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected SocketObject[] GetServerMessage()
|
||||||
|
{
|
||||||
|
if (_Socket != null && _Socket.Connected)
|
||||||
|
{
|
||||||
|
return _Socket.ReceiveArray();
|
||||||
|
}
|
||||||
|
return Array.Empty<SocketObject>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 具体接收服务器信息以及处理信息的方法
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
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.RunTime_Connect:
|
||||||
|
if (!SocketHandler_Connect(ServerMessage)) return SocketMessageType.Unknown;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SocketMessageType.RunTime_Disconnect:
|
||||||
|
SocketHandler_Disconnect(ServerMessage);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SocketMessageType.RunTime_HeartBeat:
|
||||||
|
if (_Socket != null && _Socket.Connected)
|
||||||
|
{
|
||||||
|
SocketHandler_HeartBeat(ServerMessage);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SocketMessageType.Unknown:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// 报错中断服务器连接
|
||||||
|
Error(e);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 连接服务器的处理方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ServerMessage"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected abstract bool SocketHandler_Connect(SocketObject ServerMessage);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 与服务器断开连接的处理方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ServerMessage"></param>
|
||||||
|
protected abstract void SocketHandler_Disconnect(SocketObject ServerMessage);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 心跳检测处理方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ServerMessage"></param>
|
||||||
|
protected abstract void SocketHandler_HeartBeat(SocketObject ServerMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,79 @@
|
|||||||
namespace Milimoe.FunGame.Core.Controller
|
using Milimoe.FunGame.Core.Interface.Base;
|
||||||
|
using Milimoe.FunGame.Core.Library.Common.Architecture;
|
||||||
|
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||||
|
using Milimoe.FunGame.Core.Service;
|
||||||
|
|
||||||
|
namespace Milimoe.FunGame.Core.Controller
|
||||||
{
|
{
|
||||||
public abstract class SocketHandlerController
|
/// <summary>
|
||||||
|
/// <para>继承 AsyncAwaiter 用法:</para>
|
||||||
|
/// <para>1、调用Socket.Send()前,请设置为等待状态:SetWorking();</para>
|
||||||
|
/// <para>2、调用Socket.Send() == Success后,请等待任务完成:WaitForWorkDone();</para>
|
||||||
|
/// <para>3、在其他任何地方修改Working状态,均会使任务终止。</para>
|
||||||
|
/// </summary>
|
||||||
|
public class SocketHandlerController : AsyncAwaiter<SocketObject>, ISocketHandler, IDisposable
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 重写此方法并调用Model的Dispose方法,否则将无法正常将监听Socket的事件移除!
|
/// 接收到的SocketObject实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract void Dispose();
|
protected override SocketObject Work { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Socket
|
||||||
|
/// </summary>
|
||||||
|
private readonly Socket _Socket;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 继承请调用base构造
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="socket">Socket</param>
|
||||||
|
public SocketHandlerController(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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,17 +1,18 @@
|
|||||||
namespace Milimoe.FunGame.Core.Library.Common.Event
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
|
|
||||||
|
namespace Milimoe.FunGame.Core.Library.Common.Event
|
||||||
{
|
{
|
||||||
public class ConnectEventArgs : GeneralEventArgs
|
public class ConnectEventArgs : GeneralEventArgs
|
||||||
{
|
{
|
||||||
public string ServerIP { get; set; } = "127.0.0.1";
|
public string ServerIP { get; set; } = "127.0.0.1";
|
||||||
public int ServerPort { get; set; } = 22222;
|
public int ServerPort { get; set; } = 22222;
|
||||||
|
public ConnectResult ConnectResult { get; set; } = ConnectResult.Success;
|
||||||
|
|
||||||
public ConnectEventArgs(params object[]? objs)
|
public ConnectEventArgs(string ip = "", int port = 0, ConnectResult result = ConnectResult.Success)
|
||||||
{
|
{
|
||||||
if (objs != null)
|
if (ip.Trim() != "") ServerIP = ip;
|
||||||
{
|
if (port != 0) ServerPort = port;
|
||||||
if (objs.Length > 0) ServerIP = (string)objs[0];
|
ConnectResult = result;
|
||||||
if (objs.Length > 1) ServerPort = (int)objs[1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,11 @@
|
|||||||
public string Password { get; set; } = "";
|
public string Password { get; set; } = "";
|
||||||
public string AutoKey { get; set; } = "";
|
public string AutoKey { get; set; } = "";
|
||||||
|
|
||||||
public LoginEventArgs(params object[]? objs)
|
public LoginEventArgs(string username = "", string password = "", string autokey = "")
|
||||||
{
|
{
|
||||||
if (objs != null)
|
if (username.Trim() != "") Username = username;
|
||||||
{
|
if (password.Trim() != "") Password = password;
|
||||||
if (objs.Length > 0) Username = (string)objs[0];
|
if (autokey.Trim() != "") AutoKey = autokey;
|
||||||
if (objs.Length > 1) Password = (string)objs[1];
|
|
||||||
if (objs.Length > 2) AutoKey = (string)objs[2];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,11 @@
|
|||||||
public string Password { get; set; } = "";
|
public string Password { get; set; } = "";
|
||||||
public string Email { get; set; } = "";
|
public string Email { get; set; } = "";
|
||||||
|
|
||||||
public RegisterEventArgs(params object[]? objs)
|
public RegisterEventArgs(string username = "", string password = "", string email = "")
|
||||||
{
|
{
|
||||||
if (objs != null)
|
if (username.Trim() != "") Username = username;
|
||||||
{
|
if (password.Trim() != "") Password = password;
|
||||||
if (objs.Length > 0) Username = (string)objs[0];
|
if (email.Trim() != "") Email = email;
|
||||||
if (objs.Length > 1) Password = (string)objs[1];
|
|
||||||
if (objs.Length > 2) Email = (string)objs[2];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
247
Model/RunTime.cs
247
Model/RunTime.cs
@ -1,247 +0,0 @@
|
|||||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
|
||||||
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
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 此类实现服务器连接、断开连接、心跳检测、创建数据请求等功能
|
|
||||||
/// -- 需要继承并实现部分方法 --
|
|
||||||
/// </summary>
|
|
||||||
public abstract class RunTime
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 与服务器的连接套接字实例
|
|
||||||
/// </summary>
|
|
||||||
public Socket? Socket => _Socket;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 套接字是否已经连接
|
|
||||||
/// </summary>
|
|
||||||
public bool Connected => _Socket != null && _Socket.Connected;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 接收服务器信息的线程
|
|
||||||
/// </summary>
|
|
||||||
protected Task? _ReceivingTask;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 用于类内赋值
|
|
||||||
/// </summary>
|
|
||||||
protected Socket? _Socket;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 是否正在接收服务器信息
|
|
||||||
/// </summary>
|
|
||||||
protected bool _IsReceiving;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 是否拥有一个控制器
|
|
||||||
/// </summary>
|
|
||||||
protected RunTimeController? _Controller;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 断开服务器连接
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public bool Disconnect()
|
|
||||||
{
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
result = _Socket?.Send(SocketMessageType.RunTime_Disconnect, "") == SocketResult.Success;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_Controller?.WritelnSystemInfo(e.GetErrorInfo());
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取服务器地址
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>string:IP地址;int:端口号</returns>
|
|
||||||
/// <exception cref="FindServerFailedException"></exception>
|
|
||||||
public (string, int) GetServerAddress()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string? ipaddress = (string?)Implement.GetFunGameImplValue(InterfaceType.IClient, InterfaceMethod.RemoteServerIP);
|
|
||||||
if (ipaddress != null)
|
|
||||||
{
|
|
||||||
string[] s = ipaddress.Split(':');
|
|
||||||
if (s != null && s.Length > 1)
|
|
||||||
{
|
|
||||||
return (s[0], Convert.ToInt32(s[1]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new FindServerFailedException();
|
|
||||||
}
|
|
||||||
catch (FindServerFailedException e)
|
|
||||||
{
|
|
||||||
_Controller?.WritelnSystemInfo(e.GetErrorInfo());
|
|
||||||
return ("", 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 客户端需要自行实现连接服务器的事务
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>支持异步</returns>
|
|
||||||
public abstract Task<ConnectResult> Connect();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 关闭所有连接
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 自定处理异常的方法
|
|
||||||
/// -- 一般放在catch中 --
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="e"></param>
|
|
||||||
public abstract void Error(Exception e);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 基于本地已连接的Socket创建新的数据请求
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="RequestType"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
/// <exception cref="ConnectFailedException"></exception>
|
|
||||||
public DataRequest NewDataRequest(DataRequestType RequestType)
|
|
||||||
{
|
|
||||||
if (_Socket != null)
|
|
||||||
{
|
|
||||||
DataRequest request = new(_Socket, RequestType);
|
|
||||||
return request;
|
|
||||||
}
|
|
||||||
throw new ConnectFailedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 开始接收服务器信息
|
|
||||||
/// </summary>
|
|
||||||
protected void StartReceiving()
|
|
||||||
{
|
|
||||||
_ReceivingTask = Task.Factory.StartNew(() =>
|
|
||||||
{
|
|
||||||
Thread.Sleep(100);
|
|
||||||
_IsReceiving = true;
|
|
||||||
while (Connected)
|
|
||||||
{
|
|
||||||
Receiving();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
_Socket?.StartReceiving(_ReceivingTask);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取服务器已发送的信息为SocketObject数组
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected SocketObject[] GetServerMessage()
|
|
||||||
{
|
|
||||||
if (_Socket != null && _Socket.Connected)
|
|
||||||
{
|
|
||||||
return _Socket.ReceiveArray();
|
|
||||||
}
|
|
||||||
return Array.Empty<SocketObject>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 具体接收服务器信息以及处理信息的方法
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
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.RunTime_Connect:
|
|
||||||
if (!SocketHandler_Connect(ServerMessage)) return SocketMessageType.Unknown;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SocketMessageType.RunTime_Disconnect:
|
|
||||||
SocketHandler_Disconnect(ServerMessage);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SocketMessageType.RunTime_HeartBeat:
|
|
||||||
if (_Socket != null && _Socket.Connected)
|
|
||||||
{
|
|
||||||
SocketHandler_HeartBeat(ServerMessage);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SocketMessageType.Unknown:
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
// 报错中断服务器连接
|
|
||||||
Error(e);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 连接服务器的处理方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ServerMessage"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected abstract bool SocketHandler_Connect(SocketObject ServerMessage);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 与服务器断开连接的处理方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ServerMessage"></param>
|
|
||||||
protected abstract void SocketHandler_Disconnect(SocketObject ServerMessage);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 心跳检测处理方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ServerMessage"></param>
|
|
||||||
protected abstract void SocketHandler_HeartBeat(SocketObject ServerMessage);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,79 +0,0 @@
|
|||||||
using Milimoe.FunGame.Core.Interface.Base;
|
|
||||||
using Milimoe.FunGame.Core.Library.Common.Architecture;
|
|
||||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
|
||||||
using Milimoe.FunGame.Core.Service;
|
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Core.Model
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// <para>继承 AsyncAwaiter 用法:</para>
|
|
||||||
/// <para>1、调用Socket.Send()前,请设置为等待状态:SetWorking();</para>
|
|
||||||
/// <para>2、调用Socket.Send() == Success后,请等待任务完成:WaitForWorkDone();</para>
|
|
||||||
/// <para>3、在其他任何地方修改Working状态,均会使任务终止。</para>
|
|
||||||
/// </summary>
|
|
||||||
public class SocketHandlerModel : AsyncAwaiter<SocketObject>, ISocketHandler, IDisposable
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 接收到的SocketObject实例
|
|
||||||
/// </summary>
|
|
||||||
protected override SocketObject Work { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Socket
|
|
||||||
/// </summary>
|
|
||||||
private readonly Socket _Socket;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 继承请调用base构造
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="socket">Socket</param>
|
|
||||||
public SocketHandlerModel(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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user