pub trait RpcHandler {
// Required method
fn handle(&self, method_id: u32, payload: &[u8]) -> Result<Vec<u8>>;
}Expand description
Trait for handling RPC method dispatches on the server side.
Implement this for your service type to map method_id values to
handler logic. The payload is the postcard-serialized request arguments;
the return value should be the postcard-serialized response.
§Example
use grafos_rpc::RpcHandler;
use grafos_std::error::{FabricError, Result};
use serde::{Serialize, Deserialize};
const METHOD_GREET: u32 = 0;
#[derive(Serialize, Deserialize)]
struct GreetRequest { name: String }
struct Greeter;
impl RpcHandler for Greeter {
fn handle(&self, method_id: u32, payload: &[u8]) -> Result<Vec<u8>> {
match method_id {
METHOD_GREET => {
let req: GreetRequest = postcard::from_bytes(payload)
.map_err(|_| FabricError::IoError(-200))?;
let greeting = format!("hello, {}!", req.name);
postcard::to_allocvec(&greeting)
.map_err(|_| FabricError::IoError(-201))
}
_ => Err(FabricError::Unsupported),
}
}
}Required Methods§
Sourcefn handle(&self, method_id: u32, payload: &[u8]) -> Result<Vec<u8>>
fn handle(&self, method_id: u32, payload: &[u8]) -> Result<Vec<u8>>
Dispatch a single RPC call.
method_id: Identifies which method is being called (by convention,u32constants starting at 0).payload: Postcard-serialized request arguments.
Returns postcard-serialized response bytes on success. Return
Err(FabricError::Unsupported) for unknown method IDs.