195 lines
8.0 KiB
C#
195 lines
8.0 KiB
C#
using FluentValidation;
|
|
using Gameboard.ShogiUI.Sockets.Extensions;
|
|
using Gameboard.ShogiUI.Sockets.Managers;
|
|
using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
|
|
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.WebSockets;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Services
|
|
{
|
|
public interface ISocketService
|
|
{
|
|
Task HandleSocketRequest(HttpContext context);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Services a single websocket connection. Authenticates the socket connection, accepts messages, and sends messages.
|
|
/// </summary>
|
|
public class SocketService : ISocketService
|
|
{
|
|
private readonly ILogger<SocketService> logger;
|
|
private readonly ISocketConnectionManager communicationManager;
|
|
private readonly ISocketTokenManager tokenManager;
|
|
private readonly ICreateGameHandler createGameHandler;
|
|
private readonly IJoinByCodeHandler joinByCodeHandler;
|
|
private readonly IJoinGameHandler joinGameHandler;
|
|
private readonly IListGamesHandler listGamesHandler;
|
|
private readonly ILoadGameHandler loadGameHandler;
|
|
private readonly IMoveHandler moveHandler;
|
|
private readonly IValidator<CreateGameRequest> createGameRequestValidator;
|
|
private readonly IValidator<JoinByCodeRequest> joinByCodeRequestValidator;
|
|
private readonly IValidator<JoinGameRequest> joinGameRequestValidator;
|
|
private readonly IValidator<ListGamesRequest> listGamesRequestValidator;
|
|
private readonly IValidator<LoadGameRequest> loadGameRequestValidator;
|
|
private readonly IValidator<MoveRequest> moveRequestValidator;
|
|
|
|
public SocketService(
|
|
ILogger<SocketService> logger,
|
|
ISocketConnectionManager communicationManager,
|
|
ISocketTokenManager tokenManager,
|
|
ICreateGameHandler createGameHandler,
|
|
IJoinByCodeHandler joinByCodeHandler,
|
|
IJoinGameHandler joinGameHandler,
|
|
IListGamesHandler listGamesHandler,
|
|
ILoadGameHandler loadGameHandler,
|
|
IMoveHandler moveHandler,
|
|
IValidator<CreateGameRequest> createGameRequestValidator,
|
|
IValidator<JoinByCodeRequest> joinByCodeRequestValidator,
|
|
IValidator<JoinGameRequest> joinGameRequestValidator,
|
|
IValidator<ListGamesRequest> listGamesRequestValidator,
|
|
IValidator<LoadGameRequest> loadGameRequestValidator,
|
|
IValidator<MoveRequest> moveRequestValidator
|
|
) : base()
|
|
{
|
|
this.logger = logger;
|
|
this.communicationManager = communicationManager;
|
|
this.tokenManager = tokenManager;
|
|
this.createGameHandler = createGameHandler;
|
|
this.joinByCodeHandler = joinByCodeHandler;
|
|
this.joinGameHandler = joinGameHandler;
|
|
this.listGamesHandler = listGamesHandler;
|
|
this.loadGameHandler = loadGameHandler;
|
|
this.moveHandler = moveHandler;
|
|
this.createGameRequestValidator = createGameRequestValidator;
|
|
this.joinByCodeRequestValidator = joinByCodeRequestValidator;
|
|
this.joinGameRequestValidator = joinGameRequestValidator;
|
|
this.listGamesRequestValidator = listGamesRequestValidator;
|
|
this.loadGameRequestValidator = loadGameRequestValidator;
|
|
this.moveRequestValidator = moveRequestValidator;
|
|
}
|
|
|
|
public async Task HandleSocketRequest(HttpContext context)
|
|
{
|
|
var hasToken = context.Request.Query.Keys.Contains("token");
|
|
if (hasToken)
|
|
{
|
|
var oneTimeToken = context.Request.Query["token"][0];
|
|
var tokenAsGuid = Guid.Parse(oneTimeToken);
|
|
var userName = tokenManager.GetUsername(tokenAsGuid);
|
|
if (userName != null)
|
|
{
|
|
var socket = await context.WebSockets.AcceptWebSocketAsync();
|
|
|
|
communicationManager.SubscribeToBroadcast(socket, userName);
|
|
while (socket.State == WebSocketState.Open)
|
|
{
|
|
try
|
|
{
|
|
var message = await socket.ReceiveTextAsync();
|
|
if (string.IsNullOrWhiteSpace(message)) continue;
|
|
logger.LogInformation("Request \n{0}\n", message);
|
|
var request = JsonConvert.DeserializeObject<Request>(message);
|
|
if (!Enum.IsDefined(typeof(ClientAction), request.Action))
|
|
{
|
|
await socket.SendTextAsync("Error: Action not recognized.");
|
|
continue;
|
|
}
|
|
switch (request.Action)
|
|
{
|
|
case ClientAction.ListGames:
|
|
{
|
|
var req = JsonConvert.DeserializeObject<ListGamesRequest>(message);
|
|
if (await ValidateRequestAndReplyIfInvalid(socket, listGamesRequestValidator, req))
|
|
{
|
|
await listGamesHandler.Handle(req, userName);
|
|
}
|
|
break;
|
|
}
|
|
case ClientAction.CreateGame:
|
|
{
|
|
var req = JsonConvert.DeserializeObject<CreateGameRequest>(message);
|
|
if (await ValidateRequestAndReplyIfInvalid(socket, createGameRequestValidator, req))
|
|
{
|
|
await createGameHandler.Handle(req, userName);
|
|
}
|
|
break;
|
|
}
|
|
case ClientAction.JoinGame:
|
|
{
|
|
var req = JsonConvert.DeserializeObject<JoinGameRequest>(message);
|
|
if (await ValidateRequestAndReplyIfInvalid(socket, joinGameRequestValidator, req))
|
|
{
|
|
await joinGameHandler.Handle(req, userName);
|
|
}
|
|
break;
|
|
}
|
|
case ClientAction.JoinByCode:
|
|
{
|
|
var req = JsonConvert.DeserializeObject<JoinByCodeRequest>(message);
|
|
if (await ValidateRequestAndReplyIfInvalid(socket, joinByCodeRequestValidator, req))
|
|
{
|
|
await joinByCodeHandler.Handle(req, userName);
|
|
}
|
|
break;
|
|
}
|
|
case ClientAction.LoadGame:
|
|
{
|
|
var req = JsonConvert.DeserializeObject<LoadGameRequest>(message);
|
|
if (await ValidateRequestAndReplyIfInvalid(socket, loadGameRequestValidator, req))
|
|
{
|
|
await loadGameHandler.Handle(req, userName);
|
|
}
|
|
break;
|
|
}
|
|
case ClientAction.Move:
|
|
{
|
|
var req = JsonConvert.DeserializeObject<MoveRequest>(message);
|
|
if (await ValidateRequestAndReplyIfInvalid(socket, moveRequestValidator, req))
|
|
{
|
|
await moveHandler.Handle(req, userName);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (OperationCanceledException ex)
|
|
{
|
|
logger.LogError(ex.Message);
|
|
}
|
|
catch (WebSocketException ex)
|
|
{
|
|
logger.LogInformation($"{nameof(WebSocketException)} in {nameof(SocketConnectionManager)}.");
|
|
logger.LogInformation("Probably tried writing to a closed socket.");
|
|
logger.LogError(ex.Message);
|
|
}
|
|
}
|
|
communicationManager.UnsubscribeFromBroadcastAndGames(userName);
|
|
return;
|
|
}
|
|
}
|
|
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
|
return;
|
|
}
|
|
|
|
public async Task<bool> ValidateRequestAndReplyIfInvalid<TRequest>(WebSocket socket, IValidator<TRequest> validator, TRequest request)
|
|
{
|
|
var results = validator.Validate(request);
|
|
if (!results.IsValid)
|
|
{
|
|
await socket.SendTextAsync(string.Join('\n', results.Errors.Select(_ => _.ErrorMessage).ToString()));
|
|
}
|
|
return results.IsValid;
|
|
}
|
|
}
|
|
}
|