From fc6fec0d8e69dac51e78b65dcc1a452c8a5b4901 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Wed, 13 Mar 2024 02:07:31 +0100 Subject: jsonrpc: extend the example in the library --- jsonrpc/src/client.rs | 2 +- jsonrpc/src/codec.rs | 2 +- jsonrpc/src/lib.rs | 10 +++++++++- 3 files changed, 11 insertions(+), 3 deletions(-) (limited to 'jsonrpc') diff --git a/jsonrpc/src/client.rs b/jsonrpc/src/client.rs index dc36c0d..efbaf50 100644 --- a/jsonrpc/src/client.rs +++ b/jsonrpc/src/client.rs @@ -54,7 +54,7 @@ impl Client { let mut buffer = vec![]; if let Some(t) = self.config.timeout { - self.codec.read_until_timeout(&mut buffer, t).await?; + self.codec.read_until_with_timeout(&mut buffer, t).await?; } else { self.codec.read_until(&mut buffer).await?; }; diff --git a/jsonrpc/src/codec.rs b/jsonrpc/src/codec.rs index 451cdb4..4a70412 100644 --- a/jsonrpc/src/codec.rs +++ b/jsonrpc/src/codec.rs @@ -94,7 +94,7 @@ impl Codec { Ok(()) } - pub async fn read_until_timeout(&self, buffer: &mut Vec, t: u64) -> Result { + pub async fn read_until_with_timeout(&self, buffer: &mut Vec, t: u64) -> Result { timeout(std::time::Duration::from_secs(t), self.read_until(buffer)).await? } } diff --git a/jsonrpc/src/lib.rs b/jsonrpc/src/lib.rs index df66363..3e0eb8f 100644 --- a/jsonrpc/src/lib.rs +++ b/jsonrpc/src/lib.rs @@ -18,6 +18,14 @@ //! let msg: String = serde_json::from_value(params)?; //! Ok(serde_json::json!(format!("Hello {msg}!"))) //! } +//! +//! async fn foo(&self, params: Value) -> Result { +//! Ok(serde_json::json!("foo!")) +//! } +//! +//! async fn bar(&self, params: Value) -> Result { +//! Ok(serde_json::json!("bar!")) +//! } //! } //! //! // Server @@ -30,7 +38,7 @@ //! let server = Server::new(listener, config, ex.clone()); //! //! // Register the HelloWorld service -//! register_service!(HelloWorld, say_hello); +//! register_service!(HelloWorld, say_hello, foo, bar); //! server.attach_service(HelloWorld{}); //! //! // Starts the server -- cgit v1.2.3