checkpoint

This commit is contained in:
2021-11-10 18:46:29 -06:00
parent 2a3b7b32b4
commit 20f44c8b90
26 changed files with 519 additions and 407 deletions

View File

@@ -1,5 +1,4 @@
using FluentValidation;
using Gameboard.ShogiUI.Sockets.Controllers;
using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.Managers;
using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
@@ -34,7 +33,6 @@ namespace Gameboard.ShogiUI.Sockets.Services
private readonly IGameboardManager gameboardManager;
private readonly ISocketTokenCache tokenManager;
private readonly IJoinByCodeHandler joinByCodeHandler;
private readonly IJoinGameHandler joinGameHandler;
private readonly IValidator<JoinByCodeRequest> joinByCodeRequestValidator;
private readonly IValidator<JoinGameRequest> joinGameRequestValidator;
@@ -45,7 +43,6 @@ namespace Gameboard.ShogiUI.Sockets.Services
IGameboardManager gameboardManager,
ISocketTokenCache tokenManager,
IJoinByCodeHandler joinByCodeHandler,
IJoinGameHandler joinGameHandler,
IValidator<JoinByCodeRequest> joinByCodeRequestValidator,
IValidator<JoinGameRequest> joinGameRequestValidator
) : base()
@@ -56,85 +53,68 @@ namespace Gameboard.ShogiUI.Sockets.Services
this.gameboardManager = gameboardManager;
this.tokenManager = tokenManager;
this.joinByCodeHandler = joinByCodeHandler;
this.joinGameHandler = joinGameHandler;
this.joinByCodeRequestValidator = joinByCodeRequestValidator;
this.joinGameRequestValidator = joinGameRequestValidator;
}
public async Task HandleSocketRequest(HttpContext context)
{
string? userName = null;
var user = await gameboardManager.ReadUser(context.User);
if (user?.WebSessionId != null)
if (!context.Request.Query.Keys.Contains("token"))
{
// Guest account
userName = tokenManager.GetUsername(user.WebSessionId.Value);
}
else if (context.Request.Query.Keys.Contains("token"))
{
// Microsoft account
var token = Guid.Parse(context.Request.Query["token"][0]);
userName = tokenManager.GetUsername(token);
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return;
}
var token = Guid.Parse(context.Request.Query["token"][0]);
var userName = tokenManager.GetUsername(token);
if (string.IsNullOrEmpty(userName))
{
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return;
}
else
{
var socket = await context.WebSockets.AcceptWebSocketAsync();
var socket = await context.WebSockets.AcceptWebSocketAsync();
communicationManager.SubscribeToBroadcast(socket, userName);
while (socket.State == WebSocketState.Open)
communicationManager.SubscribeToBroadcast(socket, userName);
while (socket.State == WebSocketState.Open)
{
try
{
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 (request == null || !Enum.IsDefined(typeof(ClientAction), request.Action))
{
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.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;
}
}
await socket.SendTextAsync("Error: Action not recognized.");
continue;
}
catch (OperationCanceledException ex)
switch (request.Action)
{
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);
case ClientAction.JoinByCode:
{
var req = JsonConvert.DeserializeObject<JoinByCodeRequest>(message);
if (req != null && await ValidateRequestAndReplyIfInvalid(socket, joinByCodeRequestValidator, req))
{
await joinByCodeHandler.Handle(req, userName);
}
break;
}
default:
await socket.SendTextAsync($"Received your message with action {request.Action}, but did no work.");
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;
}
}
@@ -144,11 +124,7 @@ namespace Gameboard.ShogiUI.Sockets.Services
if (!results.IsValid)
{
var errors = string.Join('\n', results.Errors.Select(_ => _.ErrorMessage));
var message = JsonConvert.SerializeObject(new Response
{
Error = errors
});
await socket.SendTextAsync(message);
await socket.SendTextAsync(errors);
}
return results.IsValid;
}

View File

@@ -5,6 +5,5 @@ namespace Gameboard.ShogiUI.Sockets.Services.Utility
public class Response : IResponse
{
public string Action { get; set; }
public string Error { get; set; }
}
}