pub struct RpcClient<T, C: Codec = BincodeCodec>where
T: MessageChannel<C>,{ /* private fields */ }Expand description
RPC client for making remote procedure calls.
Implementations§
Source§impl<T: MessageChannel<BincodeCodec> + 'static> RpcClient<T, BincodeCodec>
impl<T: MessageChannel<BincodeCodec> + 'static> RpcClient<T, BincodeCodec>
Sourcepub fn new(transport: T) -> Self
pub fn new(transport: T) -> Self
Examples found in repository?
84async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
85 println!("[Client] Connecting to RPC server");
86
87 let transport = SharedMemoryFrameTransport::connect_client(SERVICE_NAME)?;
88 let channel = MessageChannelAdapter::new(transport);
89
90 let client = RpcClient::new(channel);
91 let handle = client.try_start()?;
92
93 println!("[Client] Connected!\n");
94
95 println!("[Client] Calling add(10, 32)");
96 let resp: AddResponse = client.call("add", &AddRequest { a: 10, b: 32 }).await?;
97 println!("[Client] Result: {}\n", resp.result);
98
99 println!("[Client] Calling add(100, 200)");
100 let resp: AddResponse = client.call("add", &AddRequest { a: 100, b: 200 }).await?;
101 println!("[Client] Result: {}\n", resp.result);
102
103 println!("[Client] Calling echo(\"Hello, RPC!\")");
104 let resp: EchoResponse = client
105 .call(
106 "echo",
107 &EchoRequest {
108 message: "Hello, RPC!".to_string(),
109 },
110 )
111 .await?;
112 println!(
113 "[Client] Result: message=\"{}\", length={}\n",
114 resp.message, resp.length
115 );
116
117 println!("[Client] Calling unknown method");
118 let result: Result<(), _> = client.call("unknown", &()).await;
119 match result {
120 Ok(_) => println!("[Client] Unexpected success"),
121 Err(e) => println!("[Client] Got expected error: {}\n", e),
122 }
123
124 let close_result = client.close().await;
125 let join_result = handle.join().await;
126 close_result?;
127 join_result?;
128 println!("[Client] Done!");
129
130 Ok(())
131}pub fn with_timeout(transport: T, default_timeout: Duration) -> Self
Source§impl<T, C> RpcClient<T, C>
impl<T, C> RpcClient<T, C>
pub fn with_codec(transport: T, codec: C) -> Self
pub fn with_codec_and_timeout( transport: T, codec: C, default_timeout: Duration, ) -> Self
Sourcepub fn start(&self) -> RpcClientHandle
pub fn start(&self) -> RpcClientHandle
Start the client’s background receive loop.
Sourcepub fn try_start(&self) -> Result<RpcClientHandle>
pub fn try_start(&self) -> Result<RpcClientHandle>
Start the background receive loop and reject duplicate or post-close startup.
Examples found in repository?
84async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
85 println!("[Client] Connecting to RPC server");
86
87 let transport = SharedMemoryFrameTransport::connect_client(SERVICE_NAME)?;
88 let channel = MessageChannelAdapter::new(transport);
89
90 let client = RpcClient::new(channel);
91 let handle = client.try_start()?;
92
93 println!("[Client] Connected!\n");
94
95 println!("[Client] Calling add(10, 32)");
96 let resp: AddResponse = client.call("add", &AddRequest { a: 10, b: 32 }).await?;
97 println!("[Client] Result: {}\n", resp.result);
98
99 println!("[Client] Calling add(100, 200)");
100 let resp: AddResponse = client.call("add", &AddRequest { a: 100, b: 200 }).await?;
101 println!("[Client] Result: {}\n", resp.result);
102
103 println!("[Client] Calling echo(\"Hello, RPC!\")");
104 let resp: EchoResponse = client
105 .call(
106 "echo",
107 &EchoRequest {
108 message: "Hello, RPC!".to_string(),
109 },
110 )
111 .await?;
112 println!(
113 "[Client] Result: message=\"{}\", length={}\n",
114 resp.message, resp.length
115 );
116
117 println!("[Client] Calling unknown method");
118 let result: Result<(), _> = client.call("unknown", &()).await;
119 match result {
120 Ok(_) => println!("[Client] Unexpected success"),
121 Err(e) => println!("[Client] Got expected error: {}\n", e),
122 }
123
124 let close_result = client.close().await;
125 let join_result = handle.join().await;
126 close_result?;
127 join_result?;
128 println!("[Client] Done!");
129
130 Ok(())
131}pub fn transport(&self) -> Arc<T> ⓘ
pub fn stream_manager(&self) -> Arc<StreamManager<C>> ⓘ
Sourcepub async fn call<Req, Resp>(&self, method: &str, request: &Req) -> Result<Resp>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
pub async fn call<Req, Resp>(&self, method: &str, request: &Req) -> Result<Resp>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
Make a typed RPC call.
Examples found in repository?
84async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
85 println!("[Client] Connecting to RPC server");
86
87 let transport = SharedMemoryFrameTransport::connect_client(SERVICE_NAME)?;
88 let channel = MessageChannelAdapter::new(transport);
89
90 let client = RpcClient::new(channel);
91 let handle = client.try_start()?;
92
93 println!("[Client] Connected!\n");
94
95 println!("[Client] Calling add(10, 32)");
96 let resp: AddResponse = client.call("add", &AddRequest { a: 10, b: 32 }).await?;
97 println!("[Client] Result: {}\n", resp.result);
98
99 println!("[Client] Calling add(100, 200)");
100 let resp: AddResponse = client.call("add", &AddRequest { a: 100, b: 200 }).await?;
101 println!("[Client] Result: {}\n", resp.result);
102
103 println!("[Client] Calling echo(\"Hello, RPC!\")");
104 let resp: EchoResponse = client
105 .call(
106 "echo",
107 &EchoRequest {
108 message: "Hello, RPC!".to_string(),
109 },
110 )
111 .await?;
112 println!(
113 "[Client] Result: message=\"{}\", length={}\n",
114 resp.message, resp.length
115 );
116
117 println!("[Client] Calling unknown method");
118 let result: Result<(), _> = client.call("unknown", &()).await;
119 match result {
120 Ok(_) => println!("[Client] Unexpected success"),
121 Err(e) => println!("[Client] Got expected error: {}\n", e),
122 }
123
124 let close_result = client.close().await;
125 let join_result = handle.join().await;
126 close_result?;
127 join_result?;
128 println!("[Client] Done!");
129
130 Ok(())
131}Sourcepub async fn call_with_timeout<Req, Resp>(
&self,
method: &str,
request: &Req,
timeout: Duration,
) -> Result<Resp>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
pub async fn call_with_timeout<Req, Resp>(
&self,
method: &str,
request: &Req,
timeout: Duration,
) -> Result<Resp>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
Make a typed RPC call with custom timeout.
Sourcepub async fn call_with_options<Req, Resp>(
&self,
method: &str,
request: &Req,
options: CallOptions,
) -> Result<Resp>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
pub async fn call_with_options<Req, Resp>(
&self,
method: &str,
request: &Req,
options: CallOptions,
) -> Result<Resp>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
Make a typed RPC call with explicit per-call options.
CallOptions::without_timeout disables only the application response deadline. The
local wait still ends on explicit client close or terminal connection failure. Dropping
the call future removes its local pending registration but does not cancel server work.
Sourcepub async fn call_server_stream<Req, Resp>(
&self,
method: &str,
request: &Req,
) -> Result<StreamReceiver<Resp, C>>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
pub async fn call_server_stream<Req, Resp>(
&self,
method: &str,
request: &Req,
) -> Result<StreamReceiver<Resp, C>>where
Req: Serialize,
Resp: for<'de> Deserialize<'de>,
Initiate a server streaming RPC call.
Sourcepub async fn notify<Req: Serialize>(
&self,
method: &str,
request: &Req,
) -> Result<()>
pub async fn notify<Req: Serialize>( &self, method: &str, request: &Req, ) -> Result<()>
Send a one-way notification (no response expected).
Sourcepub async fn call_raw(&self, method: &str, payload: Vec<u8>) -> Result<Vec<u8>>
pub async fn call_raw(&self, method: &str, payload: Vec<u8>) -> Result<Vec<u8>>
Make a raw bytes RPC call.
Sourcepub async fn call_raw_with_timeout(
&self,
method: &str,
payload: Vec<u8>,
timeout: Duration,
) -> Result<Vec<u8>>
pub async fn call_raw_with_timeout( &self, method: &str, payload: Vec<u8>, timeout: Duration, ) -> Result<Vec<u8>>
Make a raw bytes RPC call with custom timeout.
Sourcepub async fn call_raw_with_options(
&self,
method: &str,
payload: Vec<u8>,
options: CallOptions,
) -> Result<Vec<u8>>
pub async fn call_raw_with_options( &self, method: &str, payload: Vec<u8>, options: CallOptions, ) -> Result<Vec<u8>>
Make a raw bytes RPC call with explicit per-call options.
CallOptions::without_timeout disables only the application response deadline. The
local wait remains interruptible by explicit client close, terminal connection failure,
and future cancellation. These local events do not cancel server work.
pub fn is_connected(&self) -> bool
pub fn is_started(&self) -> bool
pub fn is_running(&self) -> bool
pub fn is_closed(&self) -> bool
pub fn active_streams(&self) -> usize
Sourcepub async fn close(&self) -> Result<()>
pub async fn close(&self) -> Result<()>
Examples found in repository?
84async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
85 println!("[Client] Connecting to RPC server");
86
87 let transport = SharedMemoryFrameTransport::connect_client(SERVICE_NAME)?;
88 let channel = MessageChannelAdapter::new(transport);
89
90 let client = RpcClient::new(channel);
91 let handle = client.try_start()?;
92
93 println!("[Client] Connected!\n");
94
95 println!("[Client] Calling add(10, 32)");
96 let resp: AddResponse = client.call("add", &AddRequest { a: 10, b: 32 }).await?;
97 println!("[Client] Result: {}\n", resp.result);
98
99 println!("[Client] Calling add(100, 200)");
100 let resp: AddResponse = client.call("add", &AddRequest { a: 100, b: 200 }).await?;
101 println!("[Client] Result: {}\n", resp.result);
102
103 println!("[Client] Calling echo(\"Hello, RPC!\")");
104 let resp: EchoResponse = client
105 .call(
106 "echo",
107 &EchoRequest {
108 message: "Hello, RPC!".to_string(),
109 },
110 )
111 .await?;
112 println!(
113 "[Client] Result: message=\"{}\", length={}\n",
114 resp.message, resp.length
115 );
116
117 println!("[Client] Calling unknown method");
118 let result: Result<(), _> = client.call("unknown", &()).await;
119 match result {
120 Ok(_) => println!("[Client] Unexpected success"),
121 Err(e) => println!("[Client] Got expected error: {}\n", e),
122 }
123
124 let close_result = client.close().await;
125 let join_result = handle.join().await;
126 close_result?;
127 join_result?;
128 println!("[Client] Done!");
129
130 Ok(())
131}