141 lines
4.8 KiB
C#
141 lines
4.8 KiB
C#
using Gameboard.ShogiUI.Sockets.Extensions;
|
|
using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
|
|
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
|
using Gameboard.ShogiUI.Sockets.Models;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Net.WebSockets;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Managers
|
|
{
|
|
public interface ISocketCommunicationManager
|
|
{
|
|
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);
|
|
}
|
|
|
|
public class SocketCommunicationManager : ISocketCommunicationManager
|
|
{
|
|
/// <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<SocketCommunicationManager> logger;
|
|
|
|
public SocketCommunicationManager(ILogger<SocketCommunicationManager> logger)
|
|
{
|
|
this.logger = logger;
|
|
connections = new ConcurrentDictionary<string, WebSocket>();
|
|
sessions = new ConcurrentDictionary<string, Session>();
|
|
}
|
|
|
|
public void SubscribeToBroadcast(WebSocket socket, string playerName)
|
|
{
|
|
connections.TryAdd(playerName, socket);
|
|
}
|
|
|
|
public void UnsubscribeFromBroadcastAndGames(string playerName)
|
|
{
|
|
connections.TryRemove(playerName, out _);
|
|
foreach (var kvp in sessions)
|
|
{
|
|
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 serialized = JsonConvert.SerializeObject(response);
|
|
logger.LogInformation("Response to {0} \n{1}\n", name, serialized);
|
|
tasks.Add(socket.SendTextAsync(serialized));
|
|
|
|
}
|
|
}
|
|
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;
|
|
//}
|
|
}
|
|
}
|