FunGame-Core/Model/RunTime.cs
milimoe 247090341f
找回密码 第二部分 (#37)
* 常量更新

* 添加枚举:Login_UpdatePassword

* 添加Hashtable反序列化方法

Server那边没有Request对象

---------

Co-authored-by: Yezi <53083103+yeziuku@users.noreply.github.com>
2023-06-30 13:56:23 +08:00

248 lines
7.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>stringIP地址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);
}
}