Before changing Piece[,] to Dictionary<string,Piece>
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -15,127 +12,127 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
{
|
||||
public interface ISocketConnectionManager
|
||||
{
|
||||
Task HandleSocketRequest(HttpContext context);
|
||||
Task BroadcastToAll(IResponse response);
|
||||
//Task BroadcastToGame(string gameName, IResponse response);
|
||||
//Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2);
|
||||
void SubscribeToGame(Session session, string playerName);
|
||||
void SubscribeToBroadcast(WebSocket socket, string playerName);
|
||||
void UnsubscribeFromBroadcastAndGames(string playerName);
|
||||
void UnsubscribeFromGame(string gameName, string playerName);
|
||||
Task BroadcastToPlayers(IResponse response, params string[] playerNames);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retains all active socket connections and provides convenient methods for sending messages to clients.
|
||||
/// </summary>
|
||||
public class SocketConnectionManager : ISocketConnectionManager
|
||||
{
|
||||
/// <summary>Dictionary key is player name.</summary>
|
||||
private readonly ConcurrentDictionary<string, WebSocket> connections;
|
||||
/// <summary>Dictionary key is game name.</summary>
|
||||
private readonly ConcurrentDictionary<string, Session> sessions;
|
||||
private readonly ILogger<SocketConnectionManager> logger;
|
||||
private readonly ISocketCommunicationManager 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;
|
||||
|
||||
public SocketConnectionManager(
|
||||
ILogger<SocketConnectionManager> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
ISocketTokenManager tokenManager,
|
||||
ICreateGameHandler createGameHandler,
|
||||
IJoinByCodeHandler joinByCodeHandler,
|
||||
IJoinGameHandler joinGameHandler,
|
||||
IListGamesHandler listGamesHandler,
|
||||
ILoadGameHandler loadGameHandler,
|
||||
IMoveHandler moveHandler) : base()
|
||||
public SocketConnectionManager(ILogger<SocketConnectionManager> logger)
|
||||
{
|
||||
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;
|
||||
connections = new ConcurrentDictionary<string, WebSocket>();
|
||||
sessions = new ConcurrentDictionary<string, Session>();
|
||||
}
|
||||
|
||||
public async Task HandleSocketRequest(HttpContext context)
|
||||
public void SubscribeToBroadcast(WebSocket socket, string playerName)
|
||||
{
|
||||
var hasToken = context.Request.Query.Keys.Contains("token");
|
||||
if (hasToken)
|
||||
connections.TryAdd(playerName, socket);
|
||||
}
|
||||
|
||||
public void UnsubscribeFromBroadcastAndGames(string playerName)
|
||||
{
|
||||
connections.TryRemove(playerName, out _);
|
||||
foreach (var kvp in sessions)
|
||||
{
|
||||
var oneTimeToken = context.Request.Query["token"][0];
|
||||
var tokenAsGuid = Guid.Parse(oneTimeToken);
|
||||
var userName = tokenManager.GetUsername(tokenAsGuid);
|
||||
if (userName != null)
|
||||
var sessionName = kvp.Key;
|
||||
UnsubscribeFromGame(sessionName, playerName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribes the player from their current game, then subscribes to the new game.
|
||||
/// </summary>
|
||||
public void SubscribeToGame(Session session, string playerName)
|
||||
{
|
||||
// Unsubscribe from any other games
|
||||
foreach (var kvp in sessions)
|
||||
{
|
||||
var gameNameKey = kvp.Key;
|
||||
UnsubscribeFromGame(gameNameKey, playerName);
|
||||
}
|
||||
|
||||
// Subscribe
|
||||
if (connections.TryGetValue(playerName, out var socket))
|
||||
{
|
||||
var s = sessions.GetOrAdd(session.Name, session);
|
||||
s.Subscriptions.TryAdd(playerName, socket);
|
||||
}
|
||||
}
|
||||
|
||||
public void UnsubscribeFromGame(string gameName, string playerName)
|
||||
{
|
||||
if (sessions.TryGetValue(gameName, out var s))
|
||||
{
|
||||
s.Subscriptions.TryRemove(playerName, out _);
|
||||
if (s.Subscriptions.IsEmpty) sessions.TryRemove(gameName, out _);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastToPlayers(IResponse response, params string[] playerNames)
|
||||
{
|
||||
var tasks = new List<Task>(playerNames.Length);
|
||||
foreach (var name in playerNames)
|
||||
{
|
||||
if (connections.TryGetValue(name, out var socket))
|
||||
{
|
||||
var socket = await context.WebSockets.AcceptWebSocketAsync();
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", name, serialized);
|
||||
tasks.Add(socket.SendTextAsync(serialized));
|
||||
|
||||
communicationManager.SubscribeToBroadcast(socket, userName);
|
||||
while (!socket.CloseStatus.HasValue)
|
||||
{
|
||||
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);
|
||||
await listGamesHandler.Handle(req, userName);
|
||||
break;
|
||||
}
|
||||
case ClientAction.CreateGame:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<CreateGameRequest>(message);
|
||||
await createGameHandler.Handle(req, userName);
|
||||
break;
|
||||
}
|
||||
case ClientAction.JoinGame:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<JoinGameRequest>(message);
|
||||
await joinGameHandler.Handle(req, userName);
|
||||
break;
|
||||
}
|
||||
case ClientAction.JoinByCode:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<JoinByCodeRequest>(message);
|
||||
await joinByCodeHandler.Handle(req, userName);
|
||||
break;
|
||||
}
|
||||
case ClientAction.LoadGame:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<LoadGameRequest>(message);
|
||||
await loadGameHandler.Handle(req, userName);
|
||||
break;
|
||||
}
|
||||
case ClientAction.Move:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<MoveRequest>(message);
|
||||
await moveHandler.Handle(req, userName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
logger.LogError(ex.Message);
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
logger.LogInformation($"{nameof(WebSocketException)} in {nameof(SocketCommunicationManager)}.");
|
||||
logger.LogInformation("Probably tried writing to a closed socket.");
|
||||
logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
communicationManager.UnsubscribeFromBroadcastAndGames(userName);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||
return;
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
public Task BroadcastToAll(IResponse response)
|
||||
{
|
||||
var message = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation($"Broadcasting\n{0}", message);
|
||||
var tasks = new List<Task>(connections.Count);
|
||||
foreach (var kvp in connections)
|
||||
{
|
||||
var socket = kvp.Value;
|
||||
tasks.Add(socket.SendTextAsync(message));
|
||||
}
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
//public Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2)
|
||||
//{
|
||||
// if (sessions.TryGetValue(gameName, out var session))
|
||||
// {
|
||||
// var serialized1 = JsonConvert.SerializeObject(forPlayer1);
|
||||
// var serialized2 = JsonConvert.SerializeObject(forPlayer2);
|
||||
// return Task.WhenAll(
|
||||
// session.SendToPlayer1(serialized1),
|
||||
// session.SendToPlayer2(serialized2));
|
||||
// }
|
||||
// return Task.CompletedTask;
|
||||
//}
|
||||
|
||||
//public Task BroadcastToGame(string gameName, IResponse messageForAllPlayers)
|
||||
//{
|
||||
// if (sessions.TryGetValue(gameName, out var session))
|
||||
// {
|
||||
// var serialized = JsonConvert.SerializeObject(messageForAllPlayers);
|
||||
// return session.Broadcast(serialized);
|
||||
// }
|
||||
// return Task.CompletedTask;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user