diff --git a/src/client/spark_api_server/Program.cs b/src/client/spark_api_server/Program.cs index 62979316a..86769c9a1 100644 --- a/src/client/spark_api_server/Program.cs +++ b/src/client/spark_api_server/Program.cs @@ -1,11 +1,14 @@ namespace spark_api_server { + using bts; + using System; using System.Threading; using System.Threading.Tasks; class Program { - static int Main(string[] args) { + static async Task Main(string[] args) { Console.WriteLine("Starting spark_api_server... "); + var cancellationTokenSource = new CancellationTokenSource(); var sync = new bts.websocket_core.WebsocketDataSync(); var gwy = new bts.websocket_core.WebsocketConnectionGateway(); @@ -13,10 +16,16 @@ gwy.init(sync); sync.init(gwy); - gwy.start_listening(prms); + var listen = Task.Run(async () => { + Log.Api.info("Starting websocket server..."); + try { + await gwy.start_listening(prms); + Log.Api.info("Websocket server stopped without error"); + } catch (Exception ex) { + Log.Api.error($"Websocket server threw ex: {ex}"); + } - - var cancellationTokenSource = new CancellationTokenSource(); + }); var task = Task.Run(() => { while (!cancellationTokenSource.IsCancellationRequested) { // Do something that can be interrupted @@ -26,7 +35,7 @@ Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); cancellationTokenSource.Cancel(); - task.Wait(); + await Task.WhenAny(task, listen); return 0; } } diff --git a/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs b/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs index fb57fb186..0ca59ad42 100644 --- a/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs +++ b/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs @@ -19,7 +19,7 @@ namespace bts.websocket_core { // This function tells the class to start listening to client connections. // All the parameters (ports to open, security parameters etc) are // passed through the WebsocketServerParams arguments - public void start_listening(WebsocketServerParams server_params); + public Task start_listening(WebsocketServerParams server_params); // Messages are sent to the client through this public void send_to_clients( IEnumerable messages); diff --git a/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs b/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs index 461408a53..d88c773fd 100644 --- a/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs +++ b/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs @@ -9,334 +9,6 @@ namespace bts.websocket_core { public enum MessageType { Message, Close - } - /// - /// Represents a connection to a client through a WebSocket. - /// - public class Connection { - /// - /// The WebSocket instance associated with this connection. - /// - public WebSocket Ws { get; } - /// - /// A queue that holds outgoing messages to be sent to the client. - /// - public ConcurrentQueue> OutgoingQueue { get; } - /// - /// Initializes a new instance of the class. - /// - /// The WebSocket associated with this connection. - public Connection(WebSocket websocket) { - Ws = websocket; - OutgoingQueue = new ConcurrentQueue>(); - } - } - - - /// - /// Stores all the alive connection - /// Additions and removals done by gateway - /// websockethandler can access but not change anything - /// - class WebSocketConnectionStore { - private ConcurrentDictionary Connections { get; init; } - /// - /// Adds a new connection to the connections dictionary using the given id and connection object - /// - /// The id to use as the key for the new connection - /// The connection object to add to the dictionary - 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; - } - } - /// - /// Removes a connection from the connections dictionary using the given id - /// - /// The id of the connection to remove - public void RemoveConnection(WebsocketClientID id) { - if (Connections.ContainsKey(id)) { - if (Connections.TryRemove(id, out Connection? conn)) { - conn.Ws.Dispose(); - } - } - } - /// - /// Retrieves the Connection object associated with the given id - /// - /// The id of the connection to retrieve - /// The Connection object associated with the given id, or null if the id is not found - public Connection? GetConnection(WebsocketClientID id) { - if (Connections.TryGetValue(id, out var connection)) { - return connection; - } - return null; - } - /// - /// Retrieves the Connection object associated with the given id - /// - /// The id of the connection to retrieve - /// The Connection object associated with the given id, or null if the id is not found - public WebSocket? GetSocket(WebsocketClientID id) { - if (Connections.TryGetValue(id, out var connection)) { - return connection.Ws; - } - return null; - } - /// - /// Get the outgoing message queue for a specific client connection identified by the given id. - /// - /// The unique identifier for the client connection. - /// A concurrent queue of message tuples if the connection is found, null otherwise. - public ConcurrentQueue>? GetSocketQueue(WebsocketClientID id) { - if (Connections.TryGetValue(id, out var connection)) { - return connection.OutgoingQueue; - } - return null; - } - /// - /// Check if the store contains a connection with the specified id. - /// - /// The unique identifier for the client connection. - /// True if the connection is found, false otherwise. - public bool ContainsKey(WebsocketClientID id) => Connections.ContainsKey(id); - /// - /// Initializes a new instance of the class. - /// - public WebSocketConnectionStore() { - Connections = new ConcurrentDictionary(); - } - } - /// - /// is controlled by gateway: - /// once a new connection is made the gateway passes the socket to this and this - /// handles everything: - /// 1. keeps a queue of messages to send for each client (gateway can add to the queue), - /// 2. keeps listening for messages from clients (sends the gateway the messages), - /// 3. handles if client drop/wants to disconnect (informs gateway in either case), - /// 4. handles sending disconnect messages to client (gateway can add disconnect message to que) - /// can access the store but not change anything in it, - /// - 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; - /// - /// 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. - /// - /// The client ID to use to retrieve the WebSocket from the WebSocketConnectionStore. - /// The error message to log if the WebSocket is not found or not open. - /// The connected WebSocket or null if it was not found or not open. - private WebSocket? GetConnectedWebSocket(WebsocketClientID id, string error_message) { - LoggerContext logger = new(Log.Gen, error_message, ""); - var socket = Connections.GetSocket(id); - if (IsSocketStateOpen(socket)) { - return socket; - } - if (socket != null) { - logger.warn(" failed as Socket closed"); - } else { - logger.warn(" failed as could not find socket in dictionary"); - } - gtwy.HandleDisconnection(id, error_message); - return null; - } - /// - /// Determines if a WebSocket connection needs to be disconnected - /// - /// The unique identifier of the WebSocket client - /// The Connection object of the WebSocket client - /// The error message to be logged in case of improper connection closure - /// True if the connection needs to be disconnected, False otherwise - public bool ShouldDisconnect(WebsocketClientID id, Connection conn, string error_message) { - LoggerContext logger = new(Log.Gen, error_message, ""); - var socket = conn.Ws; - if (IsSocketStateOpen(socket)) { - return true; - } - if (!IsClosedProperly(socket)) { - logger.info(" closed improperly by other thread"); - gtwy.NotifyDisconnection(id); - } - return false; - } - private WebsocketConnectionGateway gtwy; - - private WebSocketConnectionStore? connections = null; - private WebSocketConnectionStore Connections { - get { - return (WebSocketConnectionStore)Log.notnull(connections); - } - set { - bts.Log.assert(connections == null); - connections = value; - } - } - /// - /// Initializes a new instance of the class - /// - /// Websocket connection gateway - /// Connections dictionary - public WebSocketHandler(WebsocketConnectionGateway gateway, WebSocketConnectionStore conns) { - gtwy = gateway; - Connections = conns; - } - /// - /// 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. - /// - /// The WebSocket connection to be processed - /// A task that represents the asynchronous operation - 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; - } - - /// - /// Adds a message to the queue of a specific websocket client. - /// - /// The unique identifier of the client - /// The message to be added to the queue - public void AddToQueue(WebsocketClientID id, Tuple mes) { - var que = Connections.GetSocketQueue(id); - if (que == null) { - return; - } - que.Enqueue(mes); - } - /// - /// Processes the messages in the WebSocket queue for a specific client. - /// will keep running until socket is open - /// - /// The unique identifier of the client - 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)); - } - /// - /// Sends a disconnect request to a websocket client, with an optional message. - /// - /// The unique identifier of the client to send the request to - /// An optional message for the logger - private async Task SendDisconnectRequestAsync(WebsocketClientID id, string message) { - LoggerContext logger = new(Log.Gen, $"WebSocketHandler.SendDisconnectRequestAsync tried closing client_id: {id} with message: {message}", ""); - // Get the socket of the client - var sckt = Connections.GetSocket(id); - if (sckt == null) { - logger.info(" 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); - logger.info(" done succesfully"); - } catch (WebSocketException ex) { - logger.info($" failed as client didnt respond, Exception: {ex.Message}"); - } - } - /// - /// recieves messages from a specific websocket client identified by its id. keeps running until socket remains open - /// - /// The unique identifier of the client - 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 buffer = new ArraySegment(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 - } - } - /// - /// Sends a message to a specific websocket client identified by its id. - /// - /// The unique identifier of the client to receive the message - /// The message to be sent - 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); - } - } - - } /// /// gateway between sync and socket logic @@ -346,49 +18,339 @@ namespace bts.websocket_core { /// 4. updates store and sync about any changes in connections (when told by handler) /// public class WebsocketConnectionGateway : IWebsocketConnectionGateway { + /// + /// Represents a connection to a client through a WebSocket. + /// + public class Connection { + /// + /// The WebSocket instance associated with this connection. + /// + public WebSocket Ws { get; } + /// + /// A queue that holds outgoing messages to be sent to the client. + /// + public ConcurrentQueue> OutgoingQueue { get; } = new(); + /// + /// Initializes a new instance of the class. + /// + /// The WebSocket associated with this connection. + public Connection(WebSocket websocket) { + Ws = websocket; + } + } + + + /// + /// wrapper for concurrent Dictionary + /// Stores all the alive connection + /// Additions and removals done by gateway + /// websockethandler can access but not change anything + /// + class WebSocketConnectionStore { + private ConcurrentDictionary Connections { get; } = new(); + /// + /// Adds a new connection to the connections dictionary using the given id and connection object + /// + /// The id to use as the key for the new connection + /// The connection object to add to the dictionary + public void AddConnection(WebsocketClientID id, Connection conn) { + if (!Connections.TryAdd(id, conn)) { + var log_txt = $"WebsocketDataSync.createID client_id: {id.ToString()} Already exits"; + bts.Log.Gen.warn(log_txt); + Connections[id] = conn; + } + } + /// + /// Removes a connection from the connections dictionary using the given id + /// + /// The id of the connection to remove + public void RemoveConnection(WebsocketClientID id) { + if (Connections.TryRemove(id, out Connection? conn)) { + conn.Ws.Dispose(); + } + } + /// + /// Retrieves the Connection object associated with the given id + /// + /// The id of the connection to retrieve + /// The Connection object associated with the given id, or null if the id is not found + public Connection? GetConnection(WebsocketClientID id) => Connections.GetValueOrDefault(id); + /// + /// Retrieves the Connection object associated with the given id + /// + /// The id of the connection to retrieve + /// The Connection object associated with the given id, or null if the id is not found + public WebSocket? GetSocket(WebsocketClientID id) => Connections.GetValueOrDefault(id)?.Ws; + /// + /// Get the outgoing message queue for a specific client connection identified by the given id. + /// + /// The unique identifier for the client connection. + /// A concurrent queue of message tuples if the connection is found, null otherwise. + public ConcurrentQueue>? GetSocketQueue(WebsocketClientID id) => + Connections.GetValueOrDefault(id)?.OutgoingQueue; + /// + /// Check if the store contains a connection with the specified id. + /// + /// The unique identifier for the client connection. + /// True if the connection is found, false otherwise. + public bool ContainsKey(WebsocketClientID id) => Connections.ContainsKey(id); + } + + /// + /// is controlled by gateway: + /// once a new connection is made the gateway passes the socket to this and this + /// handles everything: + /// 1. keeps a queue of messages to send for each client (gateway can add to the queue), + /// 2. keeps listening for messages from clients (sends the gateway the messages), + /// 3. handles if client drop/wants to disconnect (informs gateway in either case), + /// 4. handles sending disconnect messages to client (gateway can add disconnect message to que) + /// can access the store but not change anything in it, + /// + 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; + /// + /// 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. + /// + /// The client ID to use to retrieve the WebSocket from the WebSocketConnectionStore. + /// The error message to log if the WebSocket is not found or not open. + /// The connected WebSocket or null if it was not found or not open. + private WebSocket? GetConnectedWebSocket(WebsocketClientID id, string error_message) { + LoggerContext logger = new(Log.Gen, error_message, ""); + var socket = Connections.GetSocket(id); + if (IsSocketStateOpen(socket)) { + return socket; + } + if (socket != null) { + logger.warn(" failed as Socket closed"); + } else { + logger.warn(" failed as could not find socket in dictionary"); + } + gtwy.HandleDisconnection(id, error_message); + return null; + } + /// + /// Determines if a WebSocket connection needs to be disconnected + /// + /// The unique identifier of the WebSocket client + /// The Connection object of the WebSocket client + /// The error message to be logged in case of improper connection closure + /// True if the connection needs to be disconnected, False otherwise + public bool ShouldDisconnect(WebsocketClientID id, Connection conn, string error_message) { + LoggerContext logger = new(Log.Gen, error_message, ""); + var socket = conn.Ws; + if (IsSocketStateOpen(socket)) { + return true; + } + if (!IsClosedProperly(socket)) { + logger.info(" closed improperly by other thread"); + gtwy.NotifyDisconnection(id); + } + return false; + } + private WebsocketConnectionGateway gtwy; + + private WebSocketConnectionStore? connections = null; + private WebSocketConnectionStore Connections { + get => Log.notnull(connections); + set { + bts.Log.isnull(connections); + connections = value; + } + } + /// + /// Initializes a new instance of the class + /// + /// Websocket connection gateway + /// Connections dictionary + public WebSocketHandler(WebsocketConnectionGateway gateway, WebSocketConnectionStore conns) { + gtwy = gateway; + Connections = conns; + } + /// + /// 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. + /// + /// The WebSocket connection to be processed + /// A task that represents the asynchronous operation + 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; + } + + /// + /// Adds a message to the queue of a specific websocket client. + /// + /// The unique identifier of the client + /// The message to be added to the queue + /// True if succesfull otherwise fasle + public bool AddToQueue(WebsocketClientID id, Tuple mes) { + var que = Connections.GetSocketQueue(id); + if (que == null) { + return false; + } + que.Enqueue(mes); + return true; + } + /// + /// Processes the messages in the WebSocket queue for a specific client. + /// will keep running until socket is open + /// + /// The unique identifier of the client + 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; + } + while (IsSocketStateOpen(socket)) { + // 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); + } + } + } + /// + /// Sends a disconnect request to a websocket client, with an optional message. + /// + /// The unique identifier of the client to send the request to + /// An optional message for the logger + private async Task SendDisconnectRequestAsync(WebsocketClientID id, string message) { + LoggerContext logger = new(Log.Gen, $"WebSocketHandler.SendDisconnectRequestAsync tried closing client_id: {id} with message: {message}", ""); + // Get the socket of the client + var sckt = Connections.GetSocket(id); + if (sckt == null) { + logger.info(" 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); + logger.info(" done succesfully"); + } catch (WebSocketException ex) { + logger.info($" failed as client didnt respond, Exception: {ex.Message}"); + } + } + /// + /// recieves messages from a specific websocket client identified by its id. keeps running until socket remains open + /// + /// The unique identifier of the client + 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 buffer = new ArraySegment(new byte[256]); + WebSocketReceiveResult result; + string request = ""; + while (IsSocketStateOpen(socket)) { + // 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 = ""; + } + } + + } 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 + } + } + /// + /// Sends a message to a specific websocket client identified by its id. + /// + /// The unique identifier of the client to receive the message + /// The message to be sent + 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); + } + } + + + } + private WebApplication? app = null; private WebsocketServerParams? _server_params = null; - private WebSocketConnectionStore? connections = null; - private WebSocketConnectionStore Connections { - get { - return Log.notnull(connections); - } - set { - bts.Log.assert(connections == null); - connections = value; - } - } + private WebSocketConnectionStore Connections { get; } = new(); private IWebsocketDataSync? _sync = null; private IWebsocketDataSync Sync { - get { - return Log.notnull(_sync); - } + get => Log.notnull(_sync); set { - bts.Log.assert(_sync == null); + bts.Log.isnull(_sync); _sync = value; } } - private WebSocketHandler? _webSocketHandler = null; - private WebSocketHandler _WebSocketHandler { - get { - return Log.notnull(_webSocketHandler); - } - set { - bts.Log.assert(_webSocketHandler == null); - _webSocketHandler = value; - } - } - /// - /// Initializes the websocket gateway with a sync object and creates a new connections dictionary and a new WebSocketHandler. + /// creates a new connections dictionary and a new WebSocketHandler. + /// + private WebSocketHandler _WebSocketHandler { get; } + public WebsocketConnectionGateway() { + _WebSocketHandler = new WebSocketHandler(this, Connections); + } + /// + /// Initializes the websocket gateway with a sync object /// /// The object that synchronizes the data between the websocket server and other parts of the system public void init(IWebsocketDataSync sync) { Sync = sync; - Connections = new WebSocketConnectionStore(); - _WebSocketHandler = new WebSocketHandler(this, Connections); } /// /// Creates a unique identifier for a websocket client and adds the client's connection to the list of active connections. @@ -450,18 +412,18 @@ namespace bts.websocket_core { return; } // Add the disconnection message to the WebSocketHandler's message queue - _WebSocketHandler.AddToQueue(id, Tuple.Create(MessageType.Close, "")); - logger.info(" added to que"); + if (_WebSocketHandler.AddToQueue(id, Tuple.Create(MessageType.Close, ""))) + logger.info(" added to que"); } /// /// Starts listening for incoming websocket connections. need to call init before this /// /// The parameters for the websocket server includind the URL and endpoint, - public void start_listening(WebsocketServerParams server_params) { + public Task 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); + bts.Log.notnull(_sync); + bts.Log.isnull(_server_params); + bts.Log.isnull(app); _server_params = server_params; @@ -479,7 +441,7 @@ namespace bts.websocket_core { context.Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; } }); - app.RunAsync(); + return app.RunAsync(); } /// @@ -490,7 +452,8 @@ namespace bts.websocket_core { IEnumerable 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)); + if (!_WebSocketHandler.AddToQueue(om.receiver_client_id, Tuple.Create(MessageType.Message, om.message_body))) + break; } } } diff --git a/src/client/spark_websocket/Core/WebsocketDataSync.cs b/src/client/spark_websocket/Core/WebsocketDataSync.cs index e22de44b9..bf44ab8a4 100644 --- a/src/client/spark_websocket/Core/WebsocketDataSync.cs +++ b/src/client/spark_websocket/Core/WebsocketDataSync.cs @@ -8,127 +8,115 @@ namespace bts.websocket_core { using System.Collections.Concurrent; - /// - /// class to manage thread safe client store, will change this to blocking collection - /// - class ClientsStore { - private ConcurrentDictionary Clients { get; init; } - private ConcurrentDictionary Requests { get; init; } - public ClientsStore() { - Clients = new ConcurrentDictionary(); - Requests = new ConcurrentDictionary(); - } - /// - /// adds client to dictionary is thread safe - /// - /// - public void AddClient(WebsocketClientID client_id) { - if (Clients.TryAdd(client_id, new ClientState())) { - return; - } - var log_txt = $"ClientsStore.AddClient client_id: {client_id.ToString()} Already exits"; - bts.Log.Gen.warn(log_txt); - Clients[client_id] = new ClientState(); - } - /// - /// removes client from dictionary is thread safe - /// - /// - public void RemoveClient(WebsocketClientID client_id) { - if (Clients.TryRemove(client_id, out ClientState state)) { - state.Dispose(); - } - } - public IEnumerator> GetEnumerator() { - return Clients.GetEnumerator(); - } - public ClientState this[WebsocketClientID clientId] { - get { - if (Clients.TryGetValue(clientId, out var clientState)) { - return clientState; - } else { - throw new ArgumentException($"{nameof(clientId)} not found in the Clients dictionary"); - } - } - set { - Clients.AddOrUpdate(clientId, value, (key, old_value) => value); - } - } - public int Count() { - return Clients.Count; - } - } - /// - /// Immutable to store the state of the client - /// - public readonly struct ClientState : IDisposable { - - private readonly Dictionary _requests; - public ClientState() { - _requests = new Dictionary(); - } - public ClientState(Dictionary requests) { - _requests = new Dictionary(requests); - } - public ClientState Add(IUniqueID id, IWebsocketRequestHandler? dis) { - var newIds = new Dictionary(_requests); - newIds.TryAdd(id, dis); - return new ClientState(newIds); - } - - public void Dispose() { - foreach (var dis in _requests.Values) { - if (dis != null) { - dis.Dispose(); - } - } - } - - public ClientState Remove(IUniqueID id) { - var newIds = new Dictionary(_requests); - newIds.TryPopValue(id, out IWebsocketRequestHandler? dis); - if (dis != null) { - dis.Dispose(); - } - return new ClientState(newIds); - } - public ClientState RemoveAll() { - Dispose(); - return new ClientState(); - } - } /// /// This does all the bookkeeping for connections and data, and passes /// it over to the business layer CURRENT VERSION IS JUST TO SHOW HOW TO USE GATEWAY /// public class WebsocketDataSync : IWebsocketDataSync { + /// + /// class to manage thread safe client store, will change this to blocking collection + /// + class ClientsStore { + private ConcurrentDictionary Clients { get; } = new(); + private ConcurrentDictionary Requests { get; } = new(); + + /// + /// adds client to dictionary is thread safe + /// + /// + public void AddClient(WebsocketClientID client_id) { + if (Clients.TryAdd(client_id, new ClientState())) { + return; + } + var log_txt = $"ClientsStore.AddClient client_id: {client_id.ToString()} Already exits"; + bts.Log.Gen.warn(log_txt); + Clients[client_id] = new ClientState(); + } + /// + /// removes client from dictionary is thread safe + /// + /// + public void RemoveClient(WebsocketClientID client_id) { + if (Clients.TryRemove(client_id, out ClientState state)) { + state.Dispose(); + } + } + public IEnumerator> GetEnumerator() => Clients.GetEnumerator(); + public ClientState this[WebsocketClientID clientId] { + get => Clients.TryGetValue( + clientId, + out var clientState) ? + clientState : + throw new ArgumentException($"{nameof(clientId)} not found in the Clients dictionary"); + + set => Clients.AddOrUpdate( + clientId, + value, + (key, old_value) => value); + } + public int Count() => Clients.Count; + } + /// + /// Immutable to store the state of the client + /// + public readonly struct ClientState : IDisposable { + + private readonly Dictionary _requests = new(); + public ClientState() { + + } + public ClientState(Dictionary requests) { + _requests = new Dictionary(requests); + } + public ClientState Add(IUniqueID id, IWebsocketRequestHandler? dis) { + var newIds = new Dictionary(_requests); + newIds.TryAdd(id, dis); + return new ClientState(newIds); + } + + public void Dispose() { + foreach (var dis in _requests.Values) { + if (dis != null) { + dis.Dispose(); + } + } + } + + public ClientState Remove(IUniqueID id) { + var newIds = new Dictionary(_requests); + newIds.TryPopValue(id, out IWebsocketRequestHandler? dis); + if (dis != null) { + dis.Dispose(); + } + return new ClientState(newIds); + } + public ClientState RemoveAll() { + Dispose(); + return new ClientState(); + } + } + IncrementalMarketDataClient? mdClient; IncrementalMarketDataClient MdClient { - get { - return Log.notnull(mdClient); - } + get => Log.notnull(mdClient); set { - bts.Log.assert(mdClient == null); + Log.isnull(mdClient); mdClient = value; } } MDSource? _md_source; MDSource MD_Source { - get { - return Log.notnull(_md_source); - } + get => Log.notnull(_md_source); set { - bts.Log.assert(_md_source == null); + Log.isnull(_md_source); _md_source = value; } } WebsocketConnectionGateway? gateway; WebsocketConnectionGateway Gateway { - get { - return Log.notnull(gateway); - } + get => Log.notnull(gateway); set { - bts.Log.assert(gateway == null); + Log.isnull(gateway); gateway = value; } } @@ -137,11 +125,9 @@ namespace bts.websocket_core { /// private ClientsStore? clients = null; private ClientsStore Clients { - get { - return Log.notnull(clients); - } + get => Log.notnull(clients); set { - bts.Log.assert(clients == null); + Log.isnull(clients); clients = value; } }