pub struct RpcClient<Svc>where
Svc: RpcService,{ /* private fields */ }
Expand description
A RPC client handle for a given service.
use rkyv::{Archive, Deserialize, Serialize};
use datacake_rpc::{Handler, Request, RpcService, ServiceRegistry, Status, RpcClient, Channel};
use std::net::SocketAddr;
#[repr(C)]
#[derive(Serialize, Deserialize, Archive, PartialEq, Debug)]
#[archive(compare(PartialEq), check_bytes)]
#[archive_attr(derive(PartialEq, Debug))]
pub struct MyMessage {
name: String,
age: u32,
}
pub struct EchoService;
impl RpcService for EchoService {
fn register_handlers(registry: &mut ServiceRegistry<Self>) {
registry.add_handler::<MyMessage>();
}
}
#[datacake_rpc::async_trait]
impl Handler<MyMessage> for EchoService {
type Reply = MyMessage;
async fn on_message(&self, msg: Request<MyMessage>) -> Result<Self::Reply, Status> {
Ok(msg.to_owned().unwrap())
}
}
let connect = "127.0.0.1:8000".parse::<SocketAddr>()?;
let client = Channel::connect(connect);
let rpc_client = RpcClient::<EchoService>::new(client);
let msg = MyMessage {
name: "Bobby".to_string(),
age: 12,
};
let resp = rpc_client.send(&msg).await?;
assert_eq!(resp, msg);
Implementations§
Source§impl<Svc> RpcClient<Svc>where
Svc: RpcService,
impl<Svc> RpcClient<Svc>where
Svc: RpcService,
Sourcepub fn new(channel: Channel) -> Self
pub fn new(channel: Channel) -> Self
Creates a new RPC client which can handle a new service type.
RpcClient’s are cheap to create and should be preferred over locking or other synchronization primitives.
Sourcepub fn set_timeout(&mut self, timeout: Duration)
pub fn set_timeout(&mut self, timeout: Duration)
Sets a timeout of a given amount of time.
If any requests exceed this amount of time Status::timeout
is returned.
Sourcepub fn new_client<Svc2>(&self) -> RpcClient<Svc2>where
Svc2: RpcService,
pub fn new_client<Svc2>(&self) -> RpcClient<Svc2>where
Svc2: RpcService,
Creates a new RPC client which can handle a new service type.
RpcClient’s are cheap to create and should be preferred over locking or other synchronization primitives.
Sourcepub async fn send<Msg>(
&self,
msg: &Msg,
) -> Result<MessageReply<Svc, Msg>, Status>where
Msg: RequestContents + TryAsBody,
Svc: Handler<Msg>,
<Svc as Handler<Msg>>::Reply: RequestContents + TryIntoBody,
pub async fn send<Msg>(
&self,
msg: &Msg,
) -> Result<MessageReply<Svc, Msg>, Status>where
Msg: RequestContents + TryAsBody,
Svc: Handler<Msg>,
<Svc as Handler<Msg>>::Reply: RequestContents + TryIntoBody,
Sends a message to the server and wait for a reply.
This lets you send messages behind a reference which can help avoid excess copies when it isn’t needed.
In the event you need to send a Body or type which must consume self
you can use Self::send_owned
Sourcepub async fn send_owned<Msg>(
&self,
msg: Msg,
) -> Result<MessageReply<Svc, Msg>, Status>where
Msg: RequestContents + TryIntoBody,
Svc: Handler<Msg>,
<Svc as Handler<Msg>>::Reply: RequestContents + TryIntoBody,
pub async fn send_owned<Msg>(
&self,
msg: Msg,
) -> Result<MessageReply<Svc, Msg>, Status>where
Msg: RequestContents + TryIntoBody,
Svc: Handler<Msg>,
<Svc as Handler<Msg>>::Reply: RequestContents + TryIntoBody,
Sends a message to the server and wait for a reply using an owned message value.
This allows you to send types implementing TryIntoBody like Body.