refactor: Add WebsocketClient class

Basically an ISubject (I/O) wrapper for IWebsocketConnectionGateway
This commit is contained in:
John Colvin committed 2024-09-19 20:02:09 +00:00
1 parent 9a1e51eacf
commit 13646a5a16
5 files changed
+190

No files matched your search

@@ -0,0 +1,89 @@
namespace bts.websocket_core;
using bts;
using mbts;
using System;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
/// <summary>
/// Represents websocket client as an I/O model, via the <c>ISubject&lt;TIn, TOut&gt;</c> interface.
/// </summary>
public class WebsocketClient : ISubject<IEnumerable<WebsocketOutgoingMessage>, WebsocketConnectionUpdate>, IConnectableObservable<WebsocketConnectionUpdate>, 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 <see cref="Dispose"/>
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<WebsocketConnectionUpdate>.Connect() {
ConnectAsync().ignore();
return this;
}
public IDisposable Subscribe(IObserver<WebsocketConnectionUpdate> 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<IEnumerable<WebsocketOutgoingMessage>>.OnCompleted() =>
Log.Api.warn($"{GetType()} outgoing message stream completed.");
void IObserver<IEnumerable<WebsocketOutgoingMessage>>.OnError(Exception error) =>
Log.Api.error($"{GetType()} detected error in outgoing message stream: {error}");
void IObserver<IEnumerable<WebsocketOutgoingMessage>>.OnNext(
IEnumerable<WebsocketOutgoingMessage> outMessages) =>
gateway.send_to_clients(outMessages);
}
@@ -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; }
}
@@ -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();
}
@@ -0,0 +1,9 @@
namespace bts.websocket_core;
public enum WebsocketConnectionUpdateType {
Unspecified,
Connect,
IncomingMessage,
OutgoingMessage,
Disconnect
};
@@ -0,0 +1,8 @@
namespace bts.websocket_core;
public class WebsocketDisconnectNotification {
public WebsocketDisconnectNotification(WebsocketClientID id) =>
Id = id;
public WebsocketClientID Id { get; }
}