Files
spark/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs
T
2023-01-16 03:21:42 -05:00

505 lines
23 KiB
C#

using System.Net.WebSockets;
using System.Text;
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using System.Diagnostics.CodeAnalysis;
namespace bts.websocket_core {
/// <summary>
/// Helper class that contains utility functions
/// </summary>
public class Helpers {
/// <summary>
/// Creates a logger that logs to both console and external log
/// </summary>
/// <param name="base_text">The base text to be logged</param>
/// <returns>A logger function that takes an additional message and logs it along with the base text</returns>
public static Action<string> GetLogger(string base_text) => ((string additional_message) => {
base_text += additional_message;
bts.Log.Gen.info(base_text);
Console.WriteLine(base_text);
});
/// <summary>
/// Asserts that the given object is not null
/// </summary>
/// <param name="obj">The object to be checked</param>
/// <returns>True if the object is not null, False otherwise</returns>
public static bool AssertNotNull([NotNullWhen(true)] object? obj) {
bts.Log.assert(obj != null);
return true;
}
/// <summary>
/// Returns non nullable version of object after asseting that object was not null
/// </summary>
/// <param name="obj">The object to be checked and returned</param>
/// <returns>non nullable version of object</returns>
public static object GetNonNull(object? obj) => AssertNotNull(obj) ? obj : new Object();
}
public enum MessageType {
Message,
Close
}
/// <summary>
/// Represents a connection to a client through a WebSocket.
/// </summary>
public class Connection {
/// <summary>
/// The WebSocket instance associated with this connection.
/// </summary>
public WebSocket Ws { get; }
/// <summary>
/// A queue that holds outgoing messages to be sent to the client.
/// </summary>
public ConcurrentQueue<Tuple<MessageType, string>> OutgoingQueue { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Connection"/> class.
/// </summary>
/// <param name="websocket">The WebSocket associated with this connection.</param>
public Connection(WebSocket websocket) {
Ws = websocket;
OutgoingQueue = new ConcurrentQueue<Tuple<MessageType, string>>();
}
}
class WebSocketConnectionStore {
private ConcurrentDictionary<WebsocketClientID, Connection> Connections { get; init; }
/// <summary>
/// Adds a new connection to the connections dictionary using the given id and connection object
/// </summary>
/// <param name="id">The id to use as the key for the new connection</param>
/// <param name="conn">The connection object to add to the dictionary</param>
public void AddConnection(WebsocketClientID id, Connection conn) {
var check = Connections.TryAdd(id, conn);
if (!check) {
var log_txt = $"WebsocketDataSync.createID client_id: {id.ToString()} Already exits";
bts.Log.Gen.warn(log_txt);
Connections[id] = conn;
}
}
/// <summary>
/// Removes a connection from the connections dictionary using the given id
/// </summary>
/// <param name="id">The id of the connection to remove</param>
public void RemoveConnection(WebsocketClientID id) {
if (Connections.ContainsKey(id)) {
if (Connections.TryRemove(id, out Connection? conn)) {
conn.Ws.Dispose();
}
}
}
/// <summary>
/// Retrieves the Connection object associated with the given id
/// </summary>
/// <param name="id">The id of the connection to retrieve</param>
/// <returns>The Connection object associated with the given id, or null if the id is not found</returns>
public Connection? GetConnection(WebsocketClientID id) {
if (Connections.TryGetValue(id, out var connection)) {
return connection;
}
return null;
}
/// <summary>
/// Retrieves the Connection object associated with the given id
/// </summary>
/// <param name="id">The id of the connection to retrieve</param>
/// <returns>The Connection object associated with the given id, or null if the id is not found</returns>
public WebSocket? GetSocket(WebsocketClientID id) {
if (Connections.TryGetValue(id, out var connection)) {
return connection.Ws;
}
return null;
}
/// <summary>
/// Get the outgoing message queue for a specific client connection identified by the given id.
/// </summary>
/// <param name="id">The unique identifier for the client connection.</param>
/// <returns>A concurrent queue of message tuples if the connection is found, null otherwise.</returns>
public ConcurrentQueue<Tuple<MessageType, string>>? GetSocketQueue(WebsocketClientID id) {
if (Connections.TryGetValue(id, out var connection)) {
return connection.OutgoingQueue;
}
return null;
}
/// <summary>
/// Check if the store contains a connection with the specified id.
/// </summary>
/// <param name="id">The unique identifier for the client connection.</param>
/// <returns>True if the connection is found, false otherwise.</returns>
public bool ContainsKey(WebsocketClientID id) => Connections.ContainsKey(id);
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketConnectionStore"/> class.
/// </summary>
public WebSocketConnectionStore() {
Connections = new ConcurrentDictionary<WebsocketClientID, Connection>();
}
}
class WebSocketHandler {
private static bool IsSocketStateOpen(WebSocket? ws) => ws != null && ws.State != WebSocketState.Closed && ws.State != WebSocketState.Aborted && ws.State != WebSocketState.None;
private static bool IsClosedProperly(WebSocket ws) => ws.CloseStatus == WebSocketCloseStatus.NormalClosure;
/// <summary>
/// GetConnectedWebSocket attempts to retrieve a connected WebSocket from the WebSocketConnectionStore using the provided client ID.
/// If the socket is not found or is not in an open state, it logs an error message and calls the HandleDisconnection method on the
/// WebsocketConnectionGateway.
/// </summary>
/// <param name="id">The client ID to use to retrieve the WebSocket from the WebSocketConnectionStore.</param>
/// <param name="error_message">The error message to log if the WebSocket is not found or not open.</param>
/// <returns>The connected WebSocket or null if it was not found or not open.</returns>
private WebSocket? GetConnectedWebSocket(WebsocketClientID id, string error_message) {
var log = Helpers.GetLogger(error_message);
var socket = Connections.GetSocket(id);
if (IsSocketStateOpen(socket)) {
return socket;
}
if (socket != null) {
log(" failed as Socket closed");
} else {
log(" failed as could not find socket in dictionary");
}
gtwy.HandleDisconnection(id, error_message);
return null;
}
/// <summary>
/// Determines if a WebSocket connection needs to be disconnected
/// </summary>
/// <param name="id">The unique identifier of the WebSocket client</param>
/// <param name="conn">The Connection object of the WebSocket client</param>
/// <param name="error_message">The error message to be logged in case of improper connection closure</param>
/// <returns>True if the connection needs to be disconnected, False otherwise</returns>
public bool ShouldDisconnect(WebsocketClientID id, Connection conn, string error_message) {
var log = Helpers.GetLogger(error_message);
var socket = conn.Ws;
if (IsSocketStateOpen(socket)) {
return true;
}
if (!IsClosedProperly(socket)) {
log(" closed improperly by other thread");
gtwy.NotifyDisconnection(id);
}
return false;
}
private WebsocketConnectionGateway gtwy;
private WebSocketConnectionStore? connections = null;
private WebSocketConnectionStore Connections {
get {
return (WebSocketConnectionStore)Helpers.GetNonNull(connections);
}
set {
bts.Log.assert(connections == null);
connections = value;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketHandler"/> class
/// </summary>
/// <param name="gateway">Websocket connection gateway</param>
/// <param name="conns">Connections dictionary</param>
public WebSocketHandler(WebsocketConnectionGateway gateway, WebSocketConnectionStore conns) {
gtwy = gateway;
Connections = conns;
}
/// <summary>
/// Processes a new WebSocket connection asynchronously by first handling the connection with the gateway,
/// then running two tasks concurrently: one for processing incoming requests and one for processing messages in the queue.
/// </summary>
/// <param name="ws">The WebSocket connection to be processed</param>
/// <returns>A task that represents the asynchronous operation</returns>
public async Task ProcessWebSocketConnectionAsync(WebSocket ws) {
var id = await gtwy.HandleConnection(new Connection(ws), null);
var pushTask = Task.Run(() => ProcessWebSocketQueueAsync(id));
await ProcessWebSocketRequestAsync(id);
await pushTask;
}
/// <summary>
/// Adds a message to the queue of a specific websocket client.
/// </summary>
/// <param name="id">The unique identifier of the client</param>
/// <param name="mes">The message to be added to the queue</param>
public void AddToQueue(WebsocketClientID id, Tuple<MessageType, string> mes) {
var que = Connections.GetSocketQueue(id);
if (que == null) {
return;
}
que.Enqueue(mes);
}
/// <summary>
/// Processes the messages in the WebSocket queue for a specific client.
/// will keep running until socket is open
/// </summary>
/// <param name="id">The unique identifier of the client</param>
private async Task ProcessWebSocketQueueAsync(WebsocketClientID id) {
// Get the socket for the client
var socket = GetConnectedWebSocket(id, "WebSocketHandler.ProcessWebSocketQueueAsync could not process");
// Get the queue for the client
var que = Connections.GetSocketQueue(id);
// If either the socket or the queue is null, return
if (socket == null || que == null) {
return;
}
do {
// Try to dequeue a message from the queue
if (!que.TryDequeue(out var message)) {
// If the queue is empty, wait for 100 milliseconds before trying again
await Task.Delay(TimeSpan.FromMilliseconds(100));
continue;
}
// If the message is a close request
if (message.Item1 == MessageType.Close) {
// Send a disconnect request to the client
await SendDisconnectRequestAsync(id, message.Item2);
// Notify the gateway of the client's disconnection
gtwy.NotifyDisconnection(id);
break;
}
// If the message is a regular message
else if (message.Item1 == MessageType.Message) {
// Send the message to the client
await SendWebSocketRequestAsync(id, message.Item2);
}
} while (IsSocketStateOpen(socket));
}
/// <summary>
/// Sends a disconnect request to a websocket client, with an optional message.
/// </summary>
/// <param name="id">The unique identifier of the client to send the request to</param>
/// <param name="message">An optional message for the logger</param>
private async Task SendDisconnectRequestAsync(WebsocketClientID id, string message) {
var log = Helpers.GetLogger($"WebSocketHandler.SendDisconnectRequestAsync tried closing client_id: {id} with message: {message}");
// Get the socket of the client
var sckt = Connections.GetSocket(id);
if (sckt == null) {
log(" failed as could not find socket in dictionary");
return;
}
try {
// send a close message to the socket, using the close status 1000, which is a normal closure status
await sckt.CloseAsync((WebSocketCloseStatus)1000, "", CancellationToken.None);
log(" done succesfully");
} catch (WebSocketException ex) {
log($" failed as client didnt respond, Exception: {ex.Message}");
}
}
/// <summary>
/// recieves messages from a specific websocket client identified by its id. keeps running until socket remains open
/// </summary>
/// <param name="id">The unique identifier of the client</param>
private async Task ProcessWebSocketRequestAsync(WebsocketClientID id) {
// Get the socket for the given client ID
var socket = GetConnectedWebSocket(id, "WebSocketHandler.ProcessWebSocketRequestAsync could not process");
// If the socket is null, return
if (socket == null) {
return;
}
try {
// Create a buffer to receive the incoming message
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[256]);
WebSocketReceiveResult result;
string request = "";
do {
// Receive the message
result = await socket.ReceiveAsync(buffer, CancellationToken.None);
// If the message is a close request, handle the disconnection and return
if (result.MessageType == WebSocketMessageType.Close) {
var log_txt = $"WebSocketHandler.ProcessWebSocketRequestAsync close request";
gtwy.HandleDisconnection(id, log_txt);
return;
}
if (buffer.Array != null)// added because of warning
{
// Append the received message to the request string
request += Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
} else {
bts.Log.assert(false);//this should never happen
}
// If the message is the end of the message, handle the message and reset the request string
if (result.EndOfMessage) {
gtwy.HandleMessage(new WebsocketIncomingMessage(id, request));
request = "";
}
} while (IsSocketStateOpen(socket));
} catch (WebSocketException ex) {
var ex_str = ex.ToString();
var log_txt = $"WebsocketGateway.ProcessWebSocketRequestAsync connection closed Error: {ex_str}";
gtwy.HandleDisconnection(id, log_txt);
} catch (OperationCanceledException) {//not sure about this, read that this should just be ignored
}
}
/// <summary>
/// Sends a message to a specific websocket client identified by its id.
/// </summary>
/// <param name="id">The unique identifier of the client to receive the message</param>
/// <param name="message">The message to be sent</param>
public async Task SendWebSocketRequestAsync(WebsocketClientID id, string message) {
// Get the socket of the client
var socket = GetConnectedWebSocket(id, "WebSocketHandler.SendWebSocketRequestAsync could not send Request");
if (socket == null) {
return;
}
try {
// Send the message
await socket.SendAsync(Encoding.ASCII.GetBytes(message), WebSocketMessageType.Text, true, CancellationToken.None);
} catch (WebSocketException ex) {
// Handle the exception and disconnect the client
var ex_str = ex.ToString();
var log_txt = $"WebSocketHandler.SendWebSocketRequestAsync connection closed client_id: {id} Error: {ex_str}";
gtwy.HandleDisconnection(id, log_txt);
}
}
}
public class WebsocketConnectionGateway : IWebsocketConnectionGateway {
private WebApplication? app = null;
private WebsocketServerParams? _server_params = null;
private WebSocketConnectionStore? connections = null;
private WebSocketConnectionStore Connections {
get {
return (WebSocketConnectionStore)Helpers.GetNonNull(connections);
}
set {
bts.Log.assert(connections == null);
connections = value;
}
}
private IWebsocketDataSync? _sync = null;
private IWebsocketDataSync Sync {
get {
return (IWebsocketDataSync)Helpers.GetNonNull(_sync);
}
set {
bts.Log.assert(_sync == null);
_sync = value;
}
}
private WebSocketHandler? _webSocketHandler = null;
private WebSocketHandler _WebSocketHandler {
get {
return (WebSocketHandler)Helpers.GetNonNull(_webSocketHandler);
}
set {
bts.Log.assert(_webSocketHandler == null);
_webSocketHandler = value;
}
}
/// <summary>
/// Initializes the websocket gateway with a sync object and creates a new connections dictionary and a new WebSocketHandler.
/// </summary>
/// <param name="sync">The object that synchronizes the data between the websocket server and other parts of the system</param>
public void init(IWebsocketDataSync sync) {
Sync = sync;
Connections = new WebSocketConnectionStore();
_WebSocketHandler = new WebSocketHandler(this, Connections);
}
/// <summary>
/// Creates a unique identifier for a websocket client and adds the client's connection to the list of active connections.
/// </summary>
/// <param name="conn">The connection of the client</param>
/// <returns>A unique identifier for the client</returns>
private WebsocketClientID createID(Connection conn) {
var id = new WebsocketClientID();
Connections.AddConnection(id, conn);
return id;
}
/// <summary>
/// forwards an incoming message from a websocket client to sync.
/// </summary>
/// <param name="message">The incoming message to be handled</param>
public void HandleMessage(WebsocketIncomingMessage message) => Sync.handle_client_message(message);
/// <summary>
/// Notifies of a client disconnection and removes the client's connection from the list of active connections.
/// </summary>
/// <param name="id">The unique identifier of the disconnected client</param>
public void NotifyDisconnection(WebsocketClientID id) {
Sync.handle_disconnection(id);
Connections.RemoveConnection(id);
}
/// <summary>
/// Handles a new websocket connection and returns a unique identifier for the client.
/// </summary>
/// <param name="conn">The new connection to be handled</param>
/// <param name="optional_message">An optional message associated with the connection</param>
/// <returns>A unique identifier for the client</returns>
public async Task<WebsocketClientID> HandleConnection(Connection conn, WebsocketIncomingMessage? optional_message) {
var id = createID(conn);
await Task.Run(() => Sync.handle_connection(id, optional_message));
return id;
}
/// <summary>
/// Handles disconnection of a websocket client.
/// </summary>
/// <param name="id">The unique identifier of the client to be disconnected</param>
/// <param name="optional_message">An optional message to add to the logger</param>
public void HandleDisconnection(WebsocketClientID id, string optional_message = "") {
var log = Helpers.GetLogger($"WebsocketGateway.HandleDisconnection tried closing client_id: {id} with message: {optional_message}");
// Get the connection of the client
var conn = Connections.GetConnection(id);
if (conn == null) {
// If the client connection is not found, log the error and notify sync about the disconnection through the Sync object
log(" could not find socket in dictionary");
Sync.handle_disconnection(id);
return;
}
// Check if the connection needs to be disconnected
if (!_WebSocketHandler.ShouldDisconnect(id, conn, $"WebsocketGateway.HandleDisconnection tried closing client_id: {id}")) {
// If the connection does not need to be disconnected, remove it from the Connections dictionary
Connections.RemoveConnection(id);
return;
}
// Add the disconnection message to the WebSocketHandler's message queue
_WebSocketHandler.AddToQueue(id, Tuple.Create(MessageType.Close, ""));
log(" added to que");
}
/// <summary>
/// Starts listening for incoming websocket connections. need to call init before this
/// </summary>
/// <param name="server_params">The parameters for the websocket server includind the URL and endpoint,</param>
public void start_listening(WebsocketServerParams server_params) {
// start listening, assert that init has been called already
bts.Log.assert(_sync != null);
bts.Log.assert(_server_params == null);
bts.Log.assert(app == null);
_server_params = server_params;
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls(_server_params.Uri);
app = builder.Build();
app.UseWebSockets(_server_params.get_ws_options());
app.Map(_server_params.Default_end_point, async context => {
if (context.WebSockets.IsWebSocketRequest) {
using (var ws = await context.WebSockets.AcceptWebSocketAsync()) {
await _WebSocketHandler.ProcessWebSocketConnectionAsync(ws);
}
} else {
context.Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
}
});
app.RunAsync();
}
/// <summary>
/// Sends a message to a list of websocket clients.
/// </summary>
/// <param name="messages">A collection of websocket messages to be sent to the clients</param>
public void send_to_clients(
IEnumerable<WebsocketOutgoingMessage> messages) {
foreach (var om in messages) {
// Add the message to the WebSocketHandler's message queue
_WebSocketHandler.AddToQueue(om.receiver_client_id, Tuple.Create(MessageType.Message, om.message_body));
}
}
}
}