Skip to main content

thruput_rpc_api/
handler.rs

1//! Trait for implementing RPC service handlers.
2
3use crate::{ProgVers, RpcError};
4use async_trait::async_trait;
5
6/// Trait for implementing RPC service handlers.
7///
8/// Implement this trait to define how your service handles incoming RPC
9/// calls. Generated protocol dispatchers implement it; hand-written services
10/// can too. Register handlers with `thruput_rpc_driver::ServiceMap` and pass the map
11/// to `thruput_rpc_driver::RpcServer`.
12///
13/// For bidirectional RPC (calling back to the peer from within a handler),
14/// use `thruput_rpc_driver::peer()` to obtain the per-connection sender.
15///
16/// With the driver's default serial dispatch, calling back to the peer
17/// inline will deadlock — spawn a separate task instead. With the driver's
18/// `concurrent-dispatch` feature enabled, inline callbacks work without
19/// deadlocking.
20#[async_trait]
21pub trait RpcHandler: Send + Sync {
22    /// Handle an incoming RPC call for the given procedure number.
23    async fn dispatch(&self, proc: u32, data: &[u8]) -> Result<Vec<u8>, RpcError>;
24
25    /// Return the program number and version this service handles.
26    fn prog_vers(&self) -> ProgVers;
27}