Crate pirates

source ·
Expand description

Pirates - a straightforward ArrrrPC library

The core of things in the RPC definition itself. you achieve this by implementing RpcDefinition on a struct of your choice. the #[pirates::rpc_definition] macro can do this for you on an impl that contains a run and implement function (Enable the “macros” feature)

#[pirates::rpc_definition]
impl AddName {
    fn name() -> RpcId {
        RpcId::AddName
    }
    fn implement(state: &mut ServerState, query: String) -> RpcResult<()> {
        state.names.push(query);
        Ok(())
    }
}

There are two core types these are generic over which you need to define:

  1. Rpc Identifier. Create a type which implements RpcName
#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
enum RpcId {
    AddName,
    GetNames,
}
impl std::fmt::Display for RpcId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::AddName => write!(f, "AddName"),
            Self::GetNames => write!(f, "GetNames"),
        }
    }
}
  1. Server state. Any type inside an Arc<Mutex that the server can hand to RPCs
struct ServerState {
    names: Vec<String>,
}

When you have an rpc definition, you can now serve it. Serving is done by creating an RpcServer and awaiting its serve method

let mut server = RpcServer::new(state.clone());
server.add_rpc(Box::new(rpcs::AddName::server()));
server.serve("127.0.0.1:5959").await;

Elsewhere, to call it, use the call_client function with access to the RPC

let addr = "127.0.0.1:5959";
let name = String::from("Gaspode the wonder dog");
pirates::call_client(addr, name, rpcs::AddName::client()).await;

Modules

Structs

  • An RpcClient encapsulates an Rpc and allows it to be called, providing a Transport a convenience function, call_client is provided which wraps this type and uses the [TcpTransport] transport
  • Transport for data betweeen client and server, generic over the rpc names and internal transport The majority of the heavy lifting is done by the [internal_transport], see the definition of the InternalTransport trait for more information
  • TransportConfig defines various config options for transport handling [rcv_timeout] is used to protect receiving with a timeout [wire_config] is for serialising sent data, see the type def for more

Enums

  • TransportWireConfig defines how to (de)serialise query/response. Extra methods are available by enabling their feature

Traits

Functions

  • Basic client call function using the [TpcTransport] internal transport with [TransportConfig::Pickle]

Type Definitions