FunGame-Server/FunGame.WebAPI/Models/RESTfulAPIModel.cs
milimoe ccf75528cb
添加日志级别;添加匿名服务器监听;模组线程安全改进 (#41)
* 添加日志级别;添加匿名服务器监听(不要求客户端安装)

* 修复不同时间多客户端连接游戏模组时可能产生的线程安全问题

* 更新了匿名服务器令牌确认
2025-01-17 18:59:44 +08:00

88 lines
2.9 KiB
C#

using Milimoe.FunGame.Core.Interface.Base;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Server.Model;
using Milimoe.FunGame.Server.Utility;
using Milimoe.FunGame.WebAPI.Architecture;
using Milimoe.FunGame.WebAPI.Controllers;
namespace Milimoe.FunGame.WebAPI.Models
{
public class RESTfulAPIModel(ISocketListener<RESTfulAPI> server, string clientip) : ServerModel<RESTfulAPI>(server, new RESTfulAPI(Guid.NewGuid(), clientip, clientip), false)
{
public Guid LastRequestID { get; set; } = Guid.Empty;
public List<SocketObject> ToBeSent { get; set; } = [];
public override async Task<bool> Send(SocketMessageType type, params object[] objs)
{
if (type == SocketMessageType.Disconnect || type == SocketMessageType.ForceLogout)
{
RemoveUser();
await Close();
return true;
}
if (type != SocketMessageType.HeartBeat)
{
SocketObject obj = new(type, Token, objs);
if (LastRequestID != Guid.Empty)
{
return PostDataController.ResultDatas.TryAdd(LastRequestID, obj);
}
else
{
ToBeSent.Add(obj);
return true;
}
}
return false;
}
public override async Task<bool> SocketMessageHandler(ISocketMessageProcessor socket, SocketObject obj)
{
// 读取收到的消息
SocketMessageType type = obj.SocketType;
Guid token = obj.Token;
string msg = "";
// 验证Token
if (type != SocketMessageType.HeartBeat && token != socket.Token)
{
ServerHelper.WriteLine(GetClientName() + " 使用了非法方式传输消息,服务器拒绝回应 -> [" + SocketSet.GetTypeString(type) + "]");
return false;
}
if (type == SocketMessageType.EndGame)
{
if (NowGamingServer != null && NowGamingServer.IsAnonymous)
{
NowGamingServer.CloseAnonymousServer(this);
}
NowGamingServer = null;
return true;
}
if (type == SocketMessageType.AnonymousGameServer)
{
return await AnonymousGameServerHandler(obj);
}
if (type == SocketMessageType.DataRequest)
{
return await DataRequestHandler(obj);
}
if (type == SocketMessageType.GamingRequest)
{
return await GamingRequestHandler(obj);
}
if (type == SocketMessageType.Gaming)
{
return await GamingMessageHandler(obj);
}
return await Send(type, msg);
}
}
}