From 34f528a3b4063b4a25915e60d7f22ee1fb2d1bd9 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Tue, 28 May 2024 15:14:34 +0200 Subject: jsonrpc: add comments & remove debugging code --- jsonrpc/examples/tokio_server/Cargo.toml | 2 +- jsonrpc/examples/tokio_server/src/main.rs | 2 +- jsonrpc/src/client/builder.rs | 3 ++- jsonrpc/src/client/mod.rs | 10 +--------- jsonrpc/src/message.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 33 insertions(+), 12 deletions(-) (limited to 'jsonrpc') diff --git a/jsonrpc/examples/tokio_server/Cargo.toml b/jsonrpc/examples/tokio_server/Cargo.toml index 80f424d..93d8a61 100644 --- a/jsonrpc/examples/tokio_server/Cargo.toml +++ b/jsonrpc/examples/tokio_server/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] env_logger = "0.11.3" -karyon_jsonrpc = { path = "../../", default-features = false, features = ["tokio"] } +karyon_jsonrpc = { path = "../../", default-features = false, features = ["tokio", "ws"] } serde = { version = "1.0.202", features = ["derive"] } serde_json = "1.0.117" tokio = { version = "1.37.0", features = ["full"] } diff --git a/jsonrpc/examples/tokio_server/src/main.rs b/jsonrpc/examples/tokio_server/src/main.rs index d70a46a..41d4c74 100644 --- a/jsonrpc/examples/tokio_server/src/main.rs +++ b/jsonrpc/examples/tokio_server/src/main.rs @@ -48,7 +48,7 @@ async fn main() { }; // Creates a new server - let server = Server::builder("tcp://127.0.0.1:6000") + let server = Server::builder("ws://127.0.0.1:6000") .expect("Create a new server builder") .service(Arc::new(calc)) .build() diff --git a/jsonrpc/src/client/builder.rs b/jsonrpc/src/client/builder.rs index 4bfb5c3..a287070 100644 --- a/jsonrpc/src/client/builder.rs +++ b/jsonrpc/src/client/builder.rs @@ -50,7 +50,8 @@ pub struct ClientBuilder { } impl ClientBuilder { - /// Set timeout for sending and receiving messages, in milliseconds. + /// Set timeout for receiving messages, in milliseconds. Requests will + /// fail if it takes longer. /// /// # Examples /// diff --git a/jsonrpc/src/client/mod.rs b/jsonrpc/src/client/mod.rs index 05479f9..326252a 100644 --- a/jsonrpc/src/client/mod.rs +++ b/jsonrpc/src/client/mod.rs @@ -98,15 +98,7 @@ impl Client { let req_json = serde_json::to_value(&request)?; - match self.timeout { - Some(ms) => { - let t = Duration::from_millis(ms); - timeout(t, self.conn.send(req_json)).await??; - } - None => { - self.conn.send(req_json).await?; - } - } + self.conn.send(req_json).await?; let (tx, rx) = async_channel::bounded(CHANNEL_CAP); self.chans.lock().await.insert(id, tx); diff --git a/jsonrpc/src/message.rs b/jsonrpc/src/message.rs index 1f296cd..36ece38 100644 --- a/jsonrpc/src/message.rs +++ b/jsonrpc/src/message.rs @@ -19,47 +19,75 @@ pub const INVALID_PARAMS_ERROR_CODE: i32 = -32602; /// Internal error: Internal JSON-RPC error. pub const INTERNAL_ERROR_CODE: i32 = -32603; +/// SubscriptionID is used to identify a subscription. pub type SubscriptionID = u32; +/// Request represents a JSON-RPC request message. +/// It includes the JSON-RPC version, an identifier for the request, the method +/// to be invoked, and optional parameters. #[derive(Debug, Serialize, Deserialize)] pub struct Request { + /// JSON-RPC version, typically "2.0". pub jsonrpc: String, + /// Unique identifier for the request, can be a number or a string. pub id: serde_json::Value, + /// The name of the method to be invoked. pub method: String, + /// Optional parameters for the method. #[serde(skip_serializing_if = "Option::is_none")] pub params: Option, } +/// Response represents a JSON-RPC response message. +/// It includes the JSON-RPC version, an identifier matching the request, the result of the request, +/// and an optional error. #[derive(Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Response { + /// JSON-RPC version, typically "2.0". pub jsonrpc: String, + /// Unique identifier for the request, can be a number or a string. #[serde(skip_serializing_if = "Option::is_none")] pub id: Option, + /// Result of the request if it was successful. #[serde(skip_serializing_if = "Option::is_none")] pub result: Option, + /// Error object if the request failed. #[serde(skip_serializing_if = "Option::is_none")] pub error: Option, } +/// Notification represents a JSON-RPC notification message. #[derive(Debug, Serialize, Deserialize)] pub struct Notification { + /// JSON-RPC version, typically "2.0". pub jsonrpc: String, + /// The name of the method to be invoked. pub method: String, + /// Optional parameters for the method. #[serde(skip_serializing_if = "Option::is_none")] pub params: Option, } +/// NotificationResult represents the result of a subscription notification. +/// It includes the result and the subscription ID that triggered the notification. #[derive(Debug, Serialize, Deserialize)] pub struct NotificationResult { + /// Optional data about the notification. pub result: Option, + /// ID of the subscription that triggered the notification. pub subscription: SubscriptionID, } +// Error represents an error in a JSON-RPC response. +// It includes an error code, a message, and optional additional data. #[derive(Debug, Serialize, Deserialize)] pub struct Error { + /// Error code indicating the type of error. pub code: i32, + /// Human-readable error message. pub message: String, + /// Optional additional data about the error. #[serde(skip_serializing_if = "Option::is_none")] pub data: Option, } -- cgit v1.2.3