feat(battle-node): WebSocket endpoint at /socket.io/ + DI extension methods
This commit is contained in:
32
SVSim.BattleNode/Hosting/BattleNodeExtensions.cs
Normal file
32
SVSim.BattleNode/Hosting/BattleNodeExtensions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
// SVSim.BattleNode/Hosting/BattleNodeExtensions.cs
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SVSim.BattleNode.Bridge;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
|
||||
namespace SVSim.BattleNode.Hosting;
|
||||
|
||||
public static class BattleNodeExtensions
|
||||
{
|
||||
public static IServiceCollection AddBattleNode(this IServiceCollection services, Action<BattleNodeOptions>? configure = null)
|
||||
{
|
||||
var options = new BattleNodeOptions();
|
||||
configure?.Invoke(options);
|
||||
services.AddSingleton(options);
|
||||
services.AddSingleton<IBattleSessionStore, InMemoryBattleSessionStore>();
|
||||
services.AddSingleton<IMatchingBridge, MatchingBridge>();
|
||||
services.AddSingleton<BattleNodeWebSocketHandler>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseBattleNode(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseWebSockets();
|
||||
app.Map("/socket.io", branch => branch.Run(async ctx =>
|
||||
{
|
||||
var handler = ctx.RequestServices.GetRequiredService<BattleNodeWebSocketHandler>();
|
||||
await handler.HandleAsync(ctx);
|
||||
}));
|
||||
return app;
|
||||
}
|
||||
}
|
||||
64
SVSim.BattleNode/Hosting/BattleNodeWebSocketHandler.cs
Normal file
64
SVSim.BattleNode/Hosting/BattleNodeWebSocketHandler.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
// SVSim.BattleNode/Hosting/BattleNodeWebSocketHandler.cs
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
using SVSim.BattleNode.Wire;
|
||||
|
||||
namespace SVSim.BattleNode.Hosting;
|
||||
|
||||
public sealed class BattleNodeWebSocketHandler
|
||||
{
|
||||
private readonly IBattleSessionStore _store;
|
||||
private readonly ILogger<BattleNodeWebSocketHandler> _log;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
|
||||
public BattleNodeWebSocketHandler(IBattleSessionStore store, ILoggerFactory loggerFactory)
|
||||
{
|
||||
_store = store;
|
||||
_loggerFactory = loggerFactory;
|
||||
_log = loggerFactory.CreateLogger<BattleNodeWebSocketHandler>();
|
||||
}
|
||||
|
||||
public async Task HandleAsync(HttpContext ctx)
|
||||
{
|
||||
if (!ctx.WebSockets.IsWebSocketRequest)
|
||||
{
|
||||
ctx.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
return;
|
||||
}
|
||||
|
||||
var battleId = ctx.Request.Query["BattleId"].ToString();
|
||||
var encryptedViewerId = ctx.Request.Query["viewerId"].ToString();
|
||||
if (string.IsNullOrEmpty(battleId) || string.IsNullOrEmpty(encryptedViewerId))
|
||||
{
|
||||
ctx.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
return;
|
||||
}
|
||||
|
||||
long viewerId;
|
||||
try
|
||||
{
|
||||
var plain = NodeCrypto.DecryptForNode(encryptedViewerId);
|
||||
viewerId = long.Parse(plain);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogWarning(ex, "viewerId query param failed to decrypt");
|
||||
ctx.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
return;
|
||||
}
|
||||
|
||||
var pending = _store.TryGetPending(battleId);
|
||||
if (pending is null || pending.ViewerId != viewerId)
|
||||
{
|
||||
_log.LogWarning("Unknown battle/viewer pair: {Bid}/{Vid}", battleId, viewerId);
|
||||
ctx.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
return;
|
||||
}
|
||||
|
||||
var ws = await ctx.WebSockets.AcceptWebSocketAsync();
|
||||
_store.RemovePending(battleId);
|
||||
var session = new BattleSession(ws, battleId, viewerId, _loggerFactory.CreateLogger<BattleSession>());
|
||||
await session.RunAsync(ctx.RequestAborted);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user