From 13646a5a1685177d6ed2dac3a63b68f6cee8a2be Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 3 Oct 2023 17:18:36 -0400 Subject: [PATCH] refactor: Add WebsocketClient class Basically an ISubject (I/O) wrapper for IWebsocketConnectionGateway --- .../spark_websocket/Core/WebsocketClient.cs | 89 +++++++++++++++++++ .../Core/WebsocketConnectNotification.cs | 11 +++ .../Core/WebsocketConnectionUpdate.cs | 73 +++++++++++++++ .../Core/WebsocketConnectionUpdateType.cs | 9 ++ .../Core/WebsocketDisconnectNotification.cs | 8 ++ 5 files changed, 190 insertions(+) create mode 100644 src/client/spark_websocket/Core/WebsocketClient.cs create mode 100644 src/client/spark_websocket/Core/WebsocketConnectNotification.cs create mode 100644 src/client/spark_websocket/Core/WebsocketConnectionUpdate.cs create mode 100644 src/client/spark_websocket/Core/WebsocketConnectionUpdateType.cs create mode 100644 src/client/spark_websocket/Core/WebsocketDisconnectNotification.cs diff --git a/src/client/spark_websocket/Core/WebsocketClient.cs b/src/client/spark_websocket/Core/WebsocketClient.cs new file mode 100644 index 000000000..3c1d10c71 --- /dev/null +++ b/src/client/spark_websocket/Core/WebsocketClient.cs @@ -0,0 +1,89 @@ +namespace bts.websocket_core; + +using bts; + +using mbts; + +using System; +using System.Reactive.Disposables; +using System.Reactive.Subjects; + +/// +/// Represents websocket client as an I/O model, via the ISubject<TIn, TOut> interface. +/// +public class WebsocketClient : ISubject, WebsocketConnectionUpdate>, IConnectableObservable, IDisposable { + readonly WebsocketServerConfig config; + readonly object lockObj = new(); + + IWebsocketConnectionGateway? gatewayBacking; + + public WebsocketClient(WebsocketServerConfig config, IWebsocketConnectionGateway? gateway = null) { + this.config = config; + gatewayBacking = gateway ?? new WebsocketConnectionGateway(); + } + + IWebsocketConnectionGateway gateway => + /// The gateway is (only) set to null in + gatewayBacking ?? throw new ObjectDisposedException($"{GetType()}"); + + public async Task ConnectAsync() { + try { + await gateway.start_listening(config); + } catch (Exception ex) { + Log.Api.error($"Error running {typeof(IWebsocketConnectionGateway)}: {ex}"); + throw; + } + } + + IDisposable IConnectableObservable.Connect() { + ConnectAsync().ignore(); + return this; + } + + public IDisposable Subscribe(IObserver observer) { + lock (lockObj) { + gateway.Connected += OnConnected; + gateway.Disconnected += OnDisconnected; + gateway.MessageReceived += OnMessageReceived; + } + return Disposable.Create(Dispose); + + void OnConnected(WebsocketClientID id, WebsocketIncomingMessage? message) => + observer.OnNext(WebsocketConnectionUpdate.Create( + id, new WebsocketConnectNotification(id, message))); + + void OnDisconnected(WebsocketClientID id) => + observer.OnNext(WebsocketConnectionUpdate.Create( + id, new WebsocketDisconnectNotification(id))); + + void OnMessageReceived(WebsocketIncomingMessage message) => + observer.OnNext(WebsocketConnectionUpdate.Create( + message.sender_client_id, message)); + + void Dispose() { + lock (lockObj) { + gateway.Connected += OnConnected; + gateway.Disconnected += OnDisconnected; + gateway.MessageReceived += OnMessageReceived; + } + } + } + + public void Dispose() { + GC.SuppressFinalize(this); + lock (lockObj) { + (gatewayBacking as IDisposable)?.Dispose(); + gatewayBacking = null; + } + } + + void IObserver>.OnCompleted() => + Log.Api.warn($"{GetType()} outgoing message stream completed."); + + void IObserver>.OnError(Exception error) => + Log.Api.error($"{GetType()} detected error in outgoing message stream: {error}"); + + void IObserver>.OnNext( + IEnumerable outMessages) => + gateway.send_to_clients(outMessages); +} diff --git a/src/client/spark_websocket/Core/WebsocketConnectNotification.cs b/src/client/spark_websocket/Core/WebsocketConnectNotification.cs new file mode 100644 index 000000000..240983959 --- /dev/null +++ b/src/client/spark_websocket/Core/WebsocketConnectNotification.cs @@ -0,0 +1,11 @@ +namespace bts.websocket_core; + +public class WebsocketConnectNotification { + public WebsocketConnectNotification(WebsocketClientID id, WebsocketIncomingMessage? message = null) { + Id = id; + Message = message; + } + + public WebsocketClientID Id { get; } + public WebsocketIncomingMessage? Message { get; } +} diff --git a/src/client/spark_websocket/Core/WebsocketConnectionUpdate.cs b/src/client/spark_websocket/Core/WebsocketConnectionUpdate.cs new file mode 100644 index 000000000..8beacea87 --- /dev/null +++ b/src/client/spark_websocket/Core/WebsocketConnectionUpdate.cs @@ -0,0 +1,73 @@ +namespace bts.websocket_core; + +using bts.websocket_core.json_rpc; + +public class WebsocketConnectionUpdate { + readonly WebsocketClientID? connection; + readonly WebsocketIncomingMessage? incomingMessage; + readonly WebsocketOutgoingMessage? outgoingMessage; + readonly WebsocketConnectNotification? connect; + readonly WebsocketDisconnectNotification? disconnect; + + WebsocketConnectionUpdate( + WebsocketClientID connection, + WebsocketIncomingMessage? incomingMessage = null, + WebsocketOutgoingMessage? outgoingMessage = null, + WebsocketConnectNotification? connect = null, + WebsocketDisconnectNotification? disconnect = null) { + this.connection = connection; + this.incomingMessage = incomingMessage; + this.outgoingMessage = outgoingMessage; + this.connect = connect; + this.disconnect = disconnect; + } + + public static WebsocketConnectionUpdate Create( + WebsocketClientID connection, WebsocketConnectNotification connect) => + new(connection, connect: connect); + + public static WebsocketConnectionUpdate Create( + WebsocketClientID connection, WebsocketDisconnectNotification disconnect) => + new(connection, disconnect: disconnect); + + public static WebsocketConnectionUpdate Create( + WebsocketClientID connection, WebsocketIncomingMessage incomingMessage) => + new(connection, incomingMessage: incomingMessage); + + public static WebsocketConnectionUpdate Create( + WebsocketClientID connection, WebsocketOutgoingMessage outgoingMessage) => + new(connection, outgoingMessage: outgoingMessage); + + public WebsocketClientID Connection => + connection ?? throw new InvalidOperationException(); + + public WebsocketConnectionUpdateType Type { + get { + if (incomingMessage is not null) { + return WebsocketConnectionUpdateType.IncomingMessage; + } + if (outgoingMessage is not null) { + return WebsocketConnectionUpdateType.OutgoingMessage; + } + if (connect is not null) { + return WebsocketConnectionUpdateType.Connect; + } + if (disconnect is not null) { + return WebsocketConnectionUpdateType.Disconnect; + } + return WebsocketConnectionUpdateType.Unspecified; + } + } + + public WebsocketIncomingMessage IncomingMessage => + incomingMessage ?? throw new InvalidOperationException(); + + public WebsocketOutgoingMessage OutgoingMessage => + outgoingMessage ?? throw new InvalidOperationException(); + + public WebsocketConnectNotification Connect => + connect ?? throw new InvalidOperationException(); + + public WebsocketDisconnectNotification Disconnect => + disconnect ?? throw new InvalidOperationException(); +} diff --git a/src/client/spark_websocket/Core/WebsocketConnectionUpdateType.cs b/src/client/spark_websocket/Core/WebsocketConnectionUpdateType.cs new file mode 100644 index 000000000..ccd9f704b --- /dev/null +++ b/src/client/spark_websocket/Core/WebsocketConnectionUpdateType.cs @@ -0,0 +1,9 @@ +namespace bts.websocket_core; + +public enum WebsocketConnectionUpdateType { + Unspecified, + Connect, + IncomingMessage, + OutgoingMessage, + Disconnect +}; diff --git a/src/client/spark_websocket/Core/WebsocketDisconnectNotification.cs b/src/client/spark_websocket/Core/WebsocketDisconnectNotification.cs new file mode 100644 index 000000000..4449e462f --- /dev/null +++ b/src/client/spark_websocket/Core/WebsocketDisconnectNotification.cs @@ -0,0 +1,8 @@ +namespace bts.websocket_core; + +public class WebsocketDisconnectNotification { + public WebsocketDisconnectNotification(WebsocketClientID id) => + Id = id; + + public WebsocketClientID Id { get; } +}