using System.Collections;
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.Exception;
namespace Milimoe.FunGame.Core.Controller
{
///
/// 此类实现服务器连接、断开连接、心跳检测、创建数据请求等功能
/// -- 需要继承并实现部分方法 --
///
public abstract class RunTimeController
{
///
/// 与服务器的连接套接字实例
///
public Socket? Socket => _Socket;
///
/// 套接字是否已经连接
///
public bool Connected => _Socket != null && _Socket.Connected;
///
/// 接收服务器信息的线程
///
protected Task? _ReceivingTask;
///
/// 用于类内赋值
///
protected Socket? _Socket;
///
/// 是否正在接收服务器信息
///
protected bool _IsReceiving;
///
/// 断开服务器连接
///
///
public bool Disconnect()
{
bool result = false;
try
{
result = _Socket?.Send(SocketMessageType.Disconnect, "") == SocketResult.Success;
}
catch (Exception e)
{
WritelnSystemInfo(e.GetErrorInfo());
}
return result;
}
///
/// 发送结束游戏反馈
///
///
public bool EndGame()
{
bool result = false;
try
{
result = _Socket?.Send(SocketMessageType.EndGame, "") == SocketResult.Success;
}
catch (Exception e)
{
WritelnSystemInfo(e.GetErrorInfo());
}
return result;
}
///
/// 连接服务器
///
///
///
///
public ConnectResult Connect(string addr, int port)
{
ArrayList ConnectArgs = [];
if (!BeforeConnect(ref addr, ref port, ConnectArgs))
{
return ConnectResult.ConnectFailed;
}
ConnectResult result = ConnectResult.Success;
string msg = "";
string servername = "";
string notice = "";
// 检查服务器IP地址和端口是否正确
if (addr == "" || port <= 0)
{
result = ConnectResult.FindServerFailed;
}
if (result == ConnectResult.Success)
{
// 与服务器建立连接
_Socket?.Close();
_Socket = Socket.Connect(addr, port);
if (_Socket != null && _Socket.Connected)
{
if (_Socket.Send(SocketMessageType.Connect, ConnectArgs.Cast