diff --git a/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs b/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs index 19c73dc6d..70a4d784d 100644 --- a/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs +++ b/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs @@ -67,7 +67,7 @@ namespace mbts.utils { throw new ArgumentException($"Must be greater than {value}.", name); } - public static void GreaterThanEqualTo(T argument, T value, string name) where T : IComparable { + public static void GreaterThanEqualTo(T argument, T value, [CallerArgumentExpression(nameof(argument))] string name = "") where T : IComparable { if (argument.CompareTo(value) < 0) throw new ArgumentException($"Must be greater than {value}.", name); } diff --git a/src/client/spark_websocket/Core/WebsocketClientID.cs b/src/client/spark_websocket/Core/WebsocketClientID.cs index 2e80c4ea4..ca83ac1bb 100644 --- a/src/client/spark_websocket/Core/WebsocketClientID.cs +++ b/src/client/spark_websocket/Core/WebsocketClientID.cs @@ -1,3 +1,5 @@ +using bts.websocket_core.json_rpc; + using System; using System.Collections.Generic; using System.Reflection; @@ -7,5 +9,10 @@ namespace bts.websocket_core { public class WebsocketClientID : UniqueID { public WebsocketClientID() : base(Guid.NewGuid().ToString()) { } + + public WebsocketClientID(string str) : base(str) { } + + public static WebsocketClientID? Parse(JsonRpcId id) => + new(id.ToString()!); } -} \ No newline at end of file +} 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; } +} diff --git a/src/client/spark_websocket/JsonRpc/IJsonRpcHandler.cs b/src/client/spark_websocket/JsonRpc/IJsonRpcHandler.cs new file mode 100644 index 000000000..cf45f72ca --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/IJsonRpcHandler.cs @@ -0,0 +1,9 @@ +namespace bts.websocket_core.json_rpc; + +using System.Text.Json.Nodes; + +public interface IJsonRpcHandler : IDisposable { + void OnRequest(WebsocketClientID id, JsonRpcRequest req); + void OnConnect(WebsocketConnectNotification notification); + void OnDisconnect(WebsocketDisconnectNotification notification); +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcDataSync.cs b/src/client/spark_websocket/JsonRpc/JsonRpcDataSync.cs new file mode 100644 index 000000000..0ddb58671 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcDataSync.cs @@ -0,0 +1,134 @@ +namespace bts.websocket_core.json_rpc; + +using Spark.Core.Disposables; + +using System.Reactive.Subjects; +using System.Text.Json; +using System.Text.Json.Nodes; + +/// +/// Represents an I/O model (), that accepts websocket +/// requests (from clients) as input, and emits data/notifications (to clients) as output. +/// This class's chief role is managing data subscriptions to sparkd on the clients' behalf. +/// +public abstract class JsonRpcDataSync : ISubject> { + readonly JsonSerializerOptions serializerOpts; + protected readonly object lockObj = new(); + + protected JsonRpcDataSync(JsonSerializerOptions? serializerOpts = null) => + this.serializerOpts = serializerOpts ?? JsonSerializerOptions.Default; + + event Action>? requestSend; + + public void OnCompleted() => + Log.Api.info("DataSync incoming request stream has completed."); + + public void OnError(Exception error) => + Log.Api.error($"DataSync error: {error}"); + + public void OnNext(WebsocketConnectionUpdate value) { + switch (value.Type) { + case WebsocketConnectionUpdateType.IncomingMessage: + onIncomingMessage(value.IncomingMessage); + break; + case WebsocketConnectionUpdateType.Connect: + Log.Api.info($"DataStreamWebsocketDataSync client connected. clientId: {value.Connect.Id}"); + onConnect(value.Connect); + if (value.Connect.Message is not null) { + onIncomingMessage(value.Connect.Message); + } + break; + case WebsocketConnectionUpdateType.Disconnect: + onDisconnect(value.Disconnect); + break; + default: + Log.assert(false, $"Unexpected WebsocketConnectionUpdate type: {value.Type}"); + break; + } + } + + public IDisposable Subscribe(IObserver> observer) { + lock (lockObj) { + requestSend += observer.OnNext; + return Disposable.Create(Dispose); + } + void Dispose() { + lock (lockObj) { + requestSend -= observer.OnNext; + } + } + } + + protected abstract void onConnect(WebsocketConnectNotification notification); + protected abstract void onDisconnect(WebsocketDisconnectNotification notification); + protected abstract void onRequest(WebsocketClientID id, JsonRpcRequest req); + + protected virtual void onInvalidRequest(WebsocketIncomingMessage request, string? errorMsg) { + var logMsg = $"Recieved invalid request:\n{request.message_body}"; + if (errorMsg is not null) { + logMsg += $"\n\n**Exception**\n{errorMsg}"; + } + Log.Api.error(logMsg); + var rpcId = extractRpcId(request.message_body); + if (rpcId is not null) { + // Send error back to client + var rpcErr = JsonRpcError.Create( + (long)JsonRpcErrorCode.InvalidRequest, + $"Received invalid request: {errorMsg ?? "unknown error"}", + request.message_body); + sendError(request.sender_client_id, rpcId.Value, rpcErr); + } + } + + protected void sendNotification(WebsocketClientID id, string method, T rpcParams) { + var rpcNotification = JsonRpcRequest.CreateNotification(method, rpcParams); + var rpcNotificationStr = JsonSerializer.Serialize(rpcNotification, serializerOpts); + send(new WebsocketOutgoingMessage(id, rpcNotificationStr)); + } + + protected void sendResponse(WebsocketClientID id, JsonRpcId rpcId, T rpcParams) { + var rpcRes = JsonRpcResponse.Create(rpcId, rpcParams); + var rpcResStr = JsonSerializer.Serialize(rpcRes, serializerOpts); + send(new WebsocketOutgoingMessage(id, rpcResStr)); + } + + protected void sendError(WebsocketClientID id, JsonRpcId rpcId, JsonRpcError error) { + var rpcRes = JsonRpcResponse.CreateError(rpcId, error); + var rpcResStr = JsonSerializer.Serialize(rpcRes, serializerOpts); + send(new WebsocketOutgoingMessage(id, rpcResStr)); + } + + protected bool send(params WebsocketOutgoingMessage[] messages) => + send(messages.AsEnumerable()); + + protected bool send(IEnumerable messages) { + lock (lockObj) { + if (requestSend is null) { + Log.Api.warn( + $"Unable to send message(s) because '{nameof(requestSend)}' handler is null"); + return false; + } + requestSend(messages); + return true; + } + } + + static JsonRpcId? extractRpcId(string messageBody) { + try { + return JsonNode.Parse(messageBody) + ?.AsObject() + ?.GetValueOrDefault("id") + ?.Deserialize(); + } catch { + return null; + } + } + + void onIncomingMessage(WebsocketIncomingMessage message) { + if (JsonRpcRequest.TryDeserialize(message, out var req, out var err)) { + onRequest(message.sender_client_id, req); + } else { + onInvalidRequest(message, err); + } + } +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcError.cs b/src/client/spark_websocket/JsonRpc/JsonRpcError.cs new file mode 100644 index 000000000..db7c3a8ef --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcError.cs @@ -0,0 +1,23 @@ +namespace bts.websocket_core.json_rpc; + +using System.Text.Json.Serialization; + +public static class JsonRpcError { + public static JsonRpcError Create(long code, string? message, T data) => + new() { + Code = code, + Message = message, + Data = data + }; +} + +public class JsonRpcError { + [JsonPropertyName("code")] + public long Code { get; set; } + + [JsonPropertyName("message")] + public string? Message { get; set; } + + [JsonPropertyName("data")] + public T? Data { get; set; } +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcErrorCode.cs b/src/client/spark_websocket/JsonRpc/JsonRpcErrorCode.cs new file mode 100644 index 000000000..1fb124e60 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcErrorCode.cs @@ -0,0 +1,6 @@ +namespace bts.websocket_core.json_rpc; + +public enum JsonRpcErrorCode { + None = 0, + InvalidRequest = 1, +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcId.cs b/src/client/spark_websocket/JsonRpc/JsonRpcId.cs new file mode 100644 index 000000000..c22efe0fe --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcId.cs @@ -0,0 +1,50 @@ +namespace bts.websocket_core.json_rpc; + +using System.Text.Json; +using System.Text.Json.Serialization; + +[JsonConverter(typeof(JsonRpcIdConverter))] +public readonly struct JsonRpcId { + readonly long? int64; + readonly string? str; + + JsonRpcId(long int64) => + this.int64 = int64; + + JsonRpcId(string str) => + this.str = str; + + public static JsonRpcId Create() => + new(DateTime.Now.Ticks); + + public override string? ToString() => + int64?.ToString() ?? str; + + class JsonRpcIdConverter : JsonConverter { + public override bool CanConvert(Type typeToConvert) => + typeToConvert == typeof(JsonRpcId); + + public override JsonRpcId Read( + ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + if (typeToConvert != typeof(JsonRpcId)) { + throw new ArgumentException($"Unable to parse {typeToConvert}"); + } + return reader.TokenType switch { + JsonTokenType.String => new(reader.GetString()!), + JsonTokenType.Number => new(reader.GetInt64()), + _ => throw new ArgumentException($"Unable to parse {typeToConvert} from {reader.TokenType}"), + }; + } + + public override void Write( + Utf8JsonWriter writer, JsonRpcId value, JsonSerializerOptions options) { + if (value.int64.HasValue) { + writer.WriteNumberValue(value.int64.Value); + } + if (value.str is not null) { + writer.WriteStringValue(value.str); + } + } + } + +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcRequest.cs b/src/client/spark_websocket/JsonRpc/JsonRpcRequest.cs new file mode 100644 index 000000000..f26f66a41 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcRequest.cs @@ -0,0 +1,92 @@ +namespace bts.websocket_core.json_rpc; + +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +public static class JsonRpcRequest { + public static JsonRpcRequest Create(JsonRpcId id, string method, T parameters) => + new() { + Id = id, + Method = method, + Params = parameters + }; + + public static JsonRpcRequest CreateNotification(string method, T parameters) => + new() { + Method = method, + Params = parameters + }; + + public static bool TryDeserialize( + WebsocketIncomingMessage message, + [NotNullWhen(true)] out JsonRpcRequest? result, + [NotNullWhen(false)] out string? error + ) { + try { + result = JsonSerializer.Deserialize>(message.message_body)!; + error = null; + return true; + } catch (Exception ex) { + error = ex.ToString(); + result = null; + return false; + } + } + + public static JsonRpcRequest Deserialize(this JsonRpcRequest req) => + new() { + Method = req.Method, + Params = req.Params.Deserialize()!, + JsonRpc = req.JsonRpc, + Id = req.Id, + }; +} + +public class JsonRpcRequest { + [JsonPropertyName("method")] + public string? Method { get; set; } + + [JsonPropertyName("params")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [MaybeNull] + public T Params { get; set; } + + [JsonPropertyName("jsonrpc")] + public string JsonRpc { get; set; } = "2.0"; + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("id")] + public JsonRpcId? Id { get; set; } + + [JsonIgnore] + public bool IsNotification => + !Id.HasValue; + + public string Serialize() => + Serialize(JsonSerializerOptions.Default); + + public string Serialize(JsonSerializerOptions opts) => + JsonSerializer.Serialize(this, opts); + + public bool SerializeSafe( + [NotNullWhen(true)] out string? result, + [NotNullWhen(false)] out Exception? error) => + SerializeSafe(JsonSerializerOptions.Default, out result, out error); + + public bool SerializeSafe( + JsonSerializerOptions opts, + [NotNullWhen(true)] out string? result, + [NotNullWhen(false)] out Exception? error) { + try { + result = JsonSerializer.Serialize(this, opts); + error = null; + return true; + } catch (Exception ex) { + error = ex; + result = null; + return false; + } + } +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcRequestAsyncEnumerable.cs b/src/client/spark_websocket/JsonRpc/JsonRpcRequestAsyncEnumerable.cs new file mode 100644 index 000000000..de2c01c44 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcRequestAsyncEnumerable.cs @@ -0,0 +1,60 @@ +namespace bts.websocket_core.json_rpc; + +using bts.utils; + +using mbts.utils; + +using Microsoft.Extensions.Logging; + +using System.Net.WebSockets; +using System.Runtime.CompilerServices; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +public class JsonRpcRequestAsyncEnumerable { + public static IAsyncEnumerable> Create(WebSocket webSocket, ILogger logger, CancellationToken cancellationToken) => + Create(webSocket, logger, 4096, cancellationToken); + + public static async IAsyncEnumerable> Create(WebSocket webSocket, ILogger logger, int bufferCapacity, [EnumeratorCancellation]CancellationToken cancellationToken) { + ExceptionAssert.Argument.GreaterThanEqualTo(bufferCapacity, 0); + var buffer = new byte[bufferCapacity]; + var offset = 0; + for (var res = await Recv(); res is not null && !res.CloseStatus.HasValue; res = await Recv()) { + offset += res.Count; + if (offset > buffer.Length) { + throw new InvalidOperationException($"Buffer offset ({offset}) exceeds length ({buffer.Length})."); + } else if (res.EndOfMessage) { + var req = ParseRequest(); + offset = 0; + if (req is not null) { + yield return req; + } + } else if (offset == buffer.Length) { + ArrayHelper.expand(ref buffer, buffer.Length * 2); + } + } + + async Task Recv() { + var offsetBuffer = new ArraySegment(buffer, offset, buffer.Length - offset); + try { + return await webSocket.ReceiveAsync(offsetBuffer, cancellationToken); + } catch (TaskCanceledException) { + logger.LogTrace("JsonRpcRequestAsyncEnumerable 'ReceiveAsync' task was cancelled (exiting gracefully)."); + } catch (Exception ex) { + logger.LogError("JsonRpcRequestAsyncEnumerable Exception while awaiting request: {Exception}", ex); + } + return null; + } + + JsonRpcRequest? ParseRequest() { + try { + return JsonSerializer.Deserialize>( + Encoding.UTF8.GetString(new ArraySegment(buffer, 0, offset))); + } catch (Exception ex) { + logger.LogError("Failed to parse request: {Exception}", ex); + } + return null; + } + } +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcRequestObservable.cs b/src/client/spark_websocket/JsonRpc/JsonRpcRequestObservable.cs new file mode 100644 index 000000000..c06384ed2 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcRequestObservable.cs @@ -0,0 +1,74 @@ +namespace bts.websocket_core.json_rpc; + +using bts.utils; + +using mbts.utils; + +using Microsoft.Extensions.Logging; + +using System.Net.WebSockets; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +public class JsonRpcRequestObservable { + public static IObservable> Create(WebSocket webSocket, ILogger logger, int bufferCapacity = 4096) { + ExceptionAssert.Argument.GreaterThanEqualTo(bufferCapacity, 0); + var buffer = new byte[bufferCapacity]; + var offset = 0; + return Observable.Create>(Subscribe); + + IDisposable Subscribe(IObserver> observer) { + var cxl = new CancellationTokenSource(); + var cxlToken = cxl.Token; + Task.Run(Run); + return Disposable.Create(Dispose); + + void Dispose() { + logger.LogInformation("Disposing JsonRpcRequestObservable subscription."); + cxl.Cancel(); + } + + async Task Run() { + for (var res = await Recv(cxl.Token); res is not null && !res.CloseStatus.HasValue && !cxl.IsCancellationRequested; res = await Recv(cxl.Token)) { + offset += res.Count; + if (offset > buffer.Length) { + throw new InvalidOperationException($"Buffer offset ({offset}) exceeds length ({buffer.Length})."); + } else if (res.EndOfMessage) { + var req = ParseRequest(); + offset = 0; + if (req is not null) { + observer.OnNext(req); + } + } else if (offset == buffer.Length) { + ArrayHelper.expand(ref buffer, buffer.Length * 2); + } + } + } + } + + async Task Recv(CancellationToken cancellationToken) { + var offsetBuffer = new ArraySegment(buffer, offset, buffer.Length - offset); + try { + return await webSocket.ReceiveAsync(offsetBuffer, cancellationToken); + } catch (TaskCanceledException) { + logger.LogTrace("JsonRpcRequestObservable 'ReceiveAsync' task was cancelled (exiting gracefully)."); + } catch (Exception ex) { + logger.LogError("JsonRpcRequestObservable Exception while awaiting request: {Exception}", ex); + } + return null; + } + + JsonRpcRequest? ParseRequest() { + try { + return JsonSerializer.Deserialize>( + Encoding.UTF8.GetString(new ArraySegment(buffer, 0, offset))); + } catch (Exception ex) { + logger.LogError("Failed to parse request: {Exception}", ex); + } + return null; + } + } +} diff --git a/src/client/spark_websocket/JsonRpc/JsonRpcResponse.cs b/src/client/spark_websocket/JsonRpc/JsonRpcResponse.cs new file mode 100644 index 000000000..24d00c214 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcResponse.cs @@ -0,0 +1,29 @@ +namespace bts.websocket_core.json_rpc; + +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +public static class JsonRpcResponse { + public static JsonRpcResponse Create(JsonRpcId id, T result) => + new() { Id = id, Result = result }; + + public static JsonRpcResponse CreateError(JsonRpcId? id, JsonRpcError error) => + new() { Id = id, Error = error }; +} + +public class JsonRpcResponse { + [JsonPropertyName("id")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public JsonRpcId? Id { get; set; } + + [JsonPropertyName("result")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public T? Result { get; set; } + + [JsonPropertyName("error")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public JsonRpcError? Error { get; set; } + + [JsonPropertyName("jsonrpc")] + public string JsonRpc { get; set; } = "2.0"; +}