aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/src/lib.rs
blob: 1410a623cbf19f760da4dbea518968a534212e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! A fast and lightweight async implementation of [JSON-RPC
//! 2.0](https://www.jsonrpc.org/specification).
//! 
//! features: 
//! - Supports TCP, TLS, WebSocket, and Unix protocols.
//! - Uses smol(async-std) as the async runtime, but also supports tokio via 
//!   the `tokio` feature.
//! - Allows registration of multiple services (structs) of different types on a
//!   single server.
//!
//! # Example
//!
//! ```
//! use std::sync::Arc;
//!
//! use serde_json::Value;
//! use smol::net::{TcpStream, TcpListener};
//!
//! use karyon_jsonrpc::{Error, Server, Client, rpc_impl};
//!
//! struct HelloWorld {}
//!
//! #[rpc_impl]
//! impl HelloWorld {
//!     async fn say_hello(&self, params: Value) -> Result<Value, Error> {
//!         let msg: String = serde_json::from_value(params)?;
//!         Ok(serde_json::json!(format!("Hello {msg}!")))
//!     }
//!
//!     async fn foo(&self, params: Value) -> Result<Value, Error> {
//!         Ok(serde_json::json!("foo!"))
//!     }
//!
//!     async fn bar(&self, params: Value) -> Result<Value, Error> {
//!         Ok(serde_json::json!("bar!"))
//!     }
//! }
//!
//! // Server
//! async {
//!     // Creates a new server
//!     let server = Server::builder("tcp://127.0.0.1:60000")
//!         .expect("create new server builder")
//!         .service(HelloWorld{})
//!         .build()
//!         .await
//!         .expect("build the server");
//!
//!     // Starts the server
//!     server.start().await.expect("start the server");
//! };
//!
//! // Client
//! async {
//!     // Creates a new client
//!     let client = Client::builder("tcp://127.0.0.1:60000")
//!         .expect("create new client builder")
//!         .build()
//!         .await
//!         .expect("build the client");
//!
//!     let result: String = client.call("HelloWorld.say_hello", "world".to_string())
//!         .await
//!         .expect("send a request");
//! };
//!
//! ```

mod client;
mod codec;
pub mod message;
mod server;

pub use client::Client;
pub use server::Server;

pub use karyon_jsonrpc_internal::{impl_rpc_service, RPCMethod, RPCService};
pub use karyon_jsonrpc_internal::{Error, Result};
pub use karyon_jsonrpc_macro::rpc_impl;
pub use karyon_net::Endpoint;