Snowflake/RPG.Services.MUIP/Handlers/ApiHandler.cs

53 lines
1.9 KiB
C#

using Google.Protobuf;
using RPG.Network.Proto;
using RPG.Services.Core.Network;
using RPG.Services.Core.Network.Command;
namespace RPG.Services.MUIP.Handlers;
internal static class ApiHandler
{
private const string LogCategoryName = "MuipHttpApi";
private const string GmTalkSessionIdQueryKey = "s_id";
private const string GmTalkCommandQueryKey = "cmd";
private static ILogger? _logger;
public static IResult OnGmTalk(HttpContext ctx, ServiceBox services, ILoggerFactory loggerFactory)
{
_logger ??= loggerFactory.CreateLogger(LogCategoryName);
IQueryCollection query = ctx.Request.Query;
if (!query.ContainsKey(GmTalkSessionIdQueryKey) || !query.ContainsKey(GmTalkCommandQueryKey))
return Results.BadRequest();
string? sessionIdTxt = query[GmTalkSessionIdQueryKey];
string? cmd = query[GmTalkCommandQueryKey];
if (cmd == null || sessionIdTxt == null)
return Results.BadRequest();
if (!ulong.TryParse(sessionIdTxt, out ulong sessionId))
return Results.BadRequest();
_logger.LogInformation("Received GM Talk request via HTTP, session id: {id}, cmd: {cmd}", sessionId, cmd);
SendServiceCommand(services, ServiceCommandType.GmtalkByMuip, new CmdGmtalkByMuip
{
SessionId = sessionId,
Msg = cmd
}, RPGServiceType.Gameserver);
return Results.Ok("Request forwarded to Gameserver");
}
private static void SendServiceCommand<TBody>(ServiceBox services, ServiceCommandType commandType, TBody body, RPGServiceType target) where TBody : IMessage<TBody>
{
ServiceCommand command = new(services.CurrentType, commandType, body.ToByteArray());
byte[] buffer = GC.AllocateUninitializedArray<byte>(command.Body.Length + 7);
ServiceCommandEncoder.EncodeCommand(command, buffer);
services.SendToService(target, buffer);
}
}