GitXplorerGitXplorer
L

syncarp

public
11 stars
2 forks
0 issues

Commits

List of commits on branch main.
Unverified
7f0fceab9ce6052939742a0c784767cc2c6cd312

Apply the new formatting.

LLaurentMazare committed 3 years ago
Unverified
a36c54583e98059a8a7de8c18d1eeceaf71a5152

Avoid heartbeating if the connection has closed.

LLaurentMazare committed 3 years ago
Unverified
091aa84f4c306a6d724190dff14e7700b761da9c

Log connection closed.

LLaurentMazare committed 3 years ago
Unverified
b0b3da7f4a8a51c64d0dfe608254e71da7da8734

Add the code for the ocaml example.

LLaurentMazare committed 3 years ago
Unverified
ce1bb6e67e335f126dde5724ab900a3feacaabcf

Do not log an error on connection aborted.

LLaurentMazare committed 3 years ago
Unverified
9246fb347de4f365312d3f043599770d07caf1cf

Move the public traits in a separate file.

LLaurentMazare committed 3 years ago

README

The README file for this repository.

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