refactor: Add WebsocketGatewayObservable
An IObservable wrapper for IWebsocketConnectionGateway
This commit is contained in:
1 parent
09a57631c3
commit
71eab08be1
5 files changed
+214
No files matched your search
@@ -0,0 +1,14 @@
|
||||
namespace Bts.Websocket;
|
||||
|
||||
using 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;
|
||||
|
||||
using bts.websocket_core;
|
||||
|
||||
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;
|
||||
|
||||
public enum WebsocketConnectionUpdateType {
|
||||
Unspecified,
|
||||
Connect,
|
||||
IncomingMessage,
|
||||
OutgoingMessage,
|
||||
Disconnect
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Bts.Websocket;
|
||||
|
||||
using bts.websocket_core;
|
||||
|
||||
public class WebsocketDisconnectNotification {
|
||||
public WebsocketDisconnectNotification(WebsocketClientID id) =>
|
||||
Id = id;
|
||||
|
||||
public WebsocketClientID Id { get; }
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
namespace Bts.Websocket;
|
||||
|
||||
using bts;
|
||||
using bts.websocket_core;
|
||||
|
||||
using mbts;
|
||||
|
||||
using System;
|
||||
using System.Reactive.Subjects;
|
||||
|
||||
/// <summary>
|
||||
/// Represents an I/O model that provides websocket requests from clients as output, and accepts
|
||||
/// data/notifications to send back to the clients as input. This class doesn't interpret clients'
|
||||
/// requests; it doesn't subscribe to data on their behalf. It emits those requests as output so
|
||||
/// someone else can.
|
||||
/// </summary>
|
||||
public class WebsocketGatewayObservable : IConnectableObservable<WebsocketConnectionUpdate>, ISubject<IEnumerable<WebsocketOutgoingMessage>, WebsocketConnectionUpdate>, IDisposable {
|
||||
readonly WebsocketServerConfig config;
|
||||
readonly object lockObj = new();
|
||||
IWebsocketConnectionGateway? gatewayBacking;
|
||||
|
||||
public WebsocketGatewayObservable(WebsocketServerConfig config)
|
||||
: this(new WebsocketConnectionGateway(), config) { }
|
||||
|
||||
public WebsocketGatewayObservable(
|
||||
IWebsocketConnectionGateway gateway, WebsocketServerConfig config) {
|
||||
gatewayBacking = gateway;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
IWebsocketConnectionGateway gateway =>
|
||||
/// The gateway is (only) set to null in <see cref="Dispose"/>
|
||||
gatewayBacking ?? throw new ObjectDisposedException($"{GetType()}");
|
||||
|
||||
public IDisposable Connect() {
|
||||
IWebsocketConnectionGateway? gw;
|
||||
lock (lockObj) {
|
||||
gw = gateway;
|
||||
ConnectAsync().ignore();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ConnectAsync() {
|
||||
var gw = gateway;
|
||||
try {
|
||||
await gw.start_listening(config);
|
||||
} catch (Exception ex) {
|
||||
Log.Api.error($"Error running {typeof(IWebsocketConnectionGateway)}: {ex}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCompleted() =>
|
||||
Log.Api.warn($"{GetType()} outgoing message stream completed.");
|
||||
|
||||
public void OnError(Exception error) =>
|
||||
Log.Api.error($"{GetType()} detected error in outgoing message stream: {error}");
|
||||
|
||||
public void OnNext(IEnumerable<WebsocketOutgoingMessage> outMessages) =>
|
||||
gateway.send_to_clients(outMessages);
|
||||
|
||||
public IDisposable Subscribe(IObserver<WebsocketConnectionUpdate> observer) {
|
||||
lock (lockObj) {
|
||||
return new Subscription(observer, gateway);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
lock (lockObj) {
|
||||
(gatewayBacking as IDisposable)?.Dispose();
|
||||
gatewayBacking = null;
|
||||
}
|
||||
}
|
||||
|
||||
class Subscription : IDisposable {
|
||||
readonly IObserver<WebsocketConnectionUpdate> observer;
|
||||
readonly IWebsocketConnectionGateway gateway;
|
||||
|
||||
public Subscription(
|
||||
IObserver<WebsocketConnectionUpdate> observer, IWebsocketConnectionGateway gateway) {
|
||||
this.observer = observer;
|
||||
this.gateway = gateway;
|
||||
gateway.Connected += onConnected;
|
||||
gateway.Disconnected += onDisconnected;
|
||||
gateway.MessageReceived += onMessageReceived;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
gateway.Connected -= onConnected;
|
||||
gateway.Disconnected -= onDisconnected;
|
||||
gateway.MessageReceived -= onMessageReceived;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user