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/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..cc460bc04 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcRequest.cs @@ -0,0 +1,84 @@ +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 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/JsonRpcResponse.cs b/src/client/spark_websocket/JsonRpc/JsonRpcResponse.cs new file mode 100644 index 000000000..ea7d8f2e1 --- /dev/null +++ b/src/client/spark_websocket/JsonRpc/JsonRpcResponse.cs @@ -0,0 +1,30 @@ +namespace bts.websocket_core.json_rpc; + +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +public static class JsonRpcResponse { + public static JsonRpcResponse Create(JsonRpcId id, T result) => + new() { Result = result }; + + public static JsonRpcResponse CreateError(JsonRpcId id, JsonRpcError error) => + new() { Id = id, Error = error }; +} + +public class JsonRpcResponse { + [JsonPropertyName("id")] + public JsonRpcId Id { get; set; } + + [JsonPropertyName("result")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [MaybeNull] + 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"; +}