Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

syncarp

An async RPC implementation based on tokio and compatible with OCaml Async_rpc.

This uses tokio and relies on binprot-rs for message serialization.

A simple server matching the OCaml example can be implemented as follows:

struct GetUniqueId;
struct GetUniqueIdImpl(Arc<Mutex<i64>>);

impl JRpc for GetUniqueId {
    type Q = ();
    type R = i64;
    const RPC_NAME: &'static str = "get-unique-id";
    const RPC_VERSION: i64 = 0i64;
}

impl syncarp::JRpcImpl for GetUniqueIdImpl {
    type E = std::convert::Infallible;
    type JRpc = GetUniqueId;

    fn rpc_impl(&self, _q: <Self::JRpc as JRpc>::Q) -> Result<<Self::JRpc as JRpc>::R, Self::E> {
        let mut b = self.0.lock().unwrap();
        let result = *b;
        *b += 1;
        Ok(result)
    }
}

#[tokio::main]
async fn main() -> Result<(), syncarp::Error> {
    let get_unique_id_impl = GetUniqueIdImpl(Mutex::new(0));
    syncarp::RpcServer::new("127.0.0.1")
        .await?
        .add_rpc(get_unique_id_impl)
        .run()
        .await?;
    Ok(())
}

Example Client/Server

The examples directory contains a client and a server implementation in Rust as well as their OCaml equivalents. It is possible to start a Rust server and get the OCaml client to connect to it via the following:

# Start the Rust server.
cargo run --example server

# Connect with the OCaml client.
cd examples/ocaml
dune exec ./rpc_client.exe

Or the other way around:

# Start the OCaml server.
cd examples/ocaml
dune exec ./rpc_server.exe

# Connect with the Rust server (to be run from the top directory).
cargo run --example client

About

An async rpc implementation based on tokio and compatible with OCaml Async_rpc

Resources

Stars

11 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages