3 releases
0.5.7 | Jun 19, 2024 |
---|---|
0.5.6 | Jun 17, 2024 |
0.5.5 | Jun 7, 2024 |
#1452 in Network programming
Used in 8 crates
(4 directly)
43KB
755 lines
krossbar-rpc
RPC library used by Krossbar platform for communication.
The library:
- Receives tokio::net::UnixStream and returns RPC handle;
- Allows making calls, subscribing to an endpoint, and sending tokio::net::UnixStream using RPC connection;
- Supports replacing the stream after reconnection, resubscribing to the active subscriptions, and keeping all client handles valid;
- Supports message exchange monitoring via [Monitor]
Use rpc::Rpc::poll method to poll the stream. This includes waiting for a call or subscriptions response.
Examples
RPC calls:
use futures::{select, FutureExt};
use tokio::net::UnixStream;
use krossbar_rpc::rpc::Rpc;
async fn call() {
let stream = UnixStream::connect("/tmp/hub.sock").await.unwrap();
let mut rpc = Rpc::new(stream);
let call = rpc.call::<u32, u32>("echo", &42).await.unwrap();
select! {
response = call.fuse() => {
println!("Call response: {response:?}")
},
_ = rpc.poll().fuse() => {}
}
}
RPC subscription:
use futures::{select, FutureExt, StreamExt};
use tokio::net::UnixStream;
use krossbar_rpc::rpc::Rpc;
async fn subscribe() {
let stream = UnixStream::connect("/tmp/hub.sock").await.unwrap();
let mut rpc = Rpc::new(stream);
let subscription = rpc.subscribe::<u32>("signal").await.unwrap();
select! {
response = subscription.take(2).collect::<Vec<krossbar_rpc::Result<u32>>>() => {
println!("Subscription response: {response:?}")
},
_ = rpc.poll().fuse() => {}
}
}
One-way message:
use futures::{select, FutureExt};
use tokio::net::UnixStream;
use krossbar_rpc::rpc::Rpc;
async fn message() {
let stream = UnixStream::connect("/tmp/hub.sock").await.unwrap();
let mut rpc = Rpc::new(stream);
let call = rpc.send_message("echo", &42).await.unwrap();
let incoming_message = rpc.poll().await;
}
Polling imcoming messages:
use futures::{select, FutureExt};
use tokio::net::UnixStream;
use krossbar_rpc::{rpc::Rpc, request::Body};
async fn poll() {
let stream = UnixStream::connect("/tmp/hub.sock").await.unwrap();
let mut rpc = Rpc::new(stream);
loop {
let request = rpc.poll().await;
if request.is_none() {
println!("Client disconnected");
return;
}
let mut request = request.unwrap();
println!("Incoming method call: {}", request.endpoint());
match request.take_body().unwrap() {
Body::Message(bson) => {
println!("Incoming message: {bson:?}");
},
Body::Call(bson) => {
println!("Incoming call: {bson:?}");
request.respond(Ok(bson)).await;
},
Body::Subscription => {
println!("Incoming subscription");
request.respond(Ok(41)).await;
request.respond(Ok(42)).await;
request.respond(Ok(43)).await;
},
Body::Fd { client_name, .. } => {
println!("Incoming connection request from {client_name}");
request.respond(Ok(())).await;
}
}
}
}
See tests/
for more examples.
Dependencies
~10–19MB
~271K SLoC