mrpc

Crates.iomrpc
lib.rsmrpc
version0.0.3
sourcesrc
created_at2024-07-28 02:21:44.764044
updated_at2024-08-04 03:38:48.183927
descriptionMessagePack-RPC for Rust
homepage
repositoryhttps://github.com/cortesi/mrpc
max_upload_size
id1317689
size95,384
Aldo Cortesi (cortesi)

documentation

README

Crates.io Documentation License: MIT

mrpc

A MessagePack-RPC implementation in Rust.

Features

  • Write asynchronous RPC servers and clients

  • Support for TCP and Unix domain sockets

  • Full msgpack-rpc implementation (requests, responses, notifications)

  • Support for bidirectional communication - both servers and clients can handle incoming RPC messages

  • Built on tokio for async I/O

  • Uses rmpv for MessagePack serialization

Quick Start

use mrpc::{Client, Connection, Result, RpcSender, Server};
use rmpv::Value;

#[derive(Clone, Default)]
struct Echo;

#[async_trait::async_trait]
impl Connection for Echo {
    async fn handle_request(
        &self,
        _: RpcSender,
        method: &str,
        params: Vec<Value>,
    ) -> Result<Value> {
        Ok(format!("{} -> {}", method, params[0]).into())
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    // We're just using the default constructor as our ConnectionMaker
    let server = Server::from_fn(Echo::default).tcp("127.0.0.1:0").await?;
    let addr = server.local_addr().unwrap();
    tokio::spawn(server.run());

    // `Connection` is implemented for (), as a convenience for clients who don't need to handle
    // requests or responses.
    let client = Client::connect_tcp(&addr.to_string(), ()).await?;
    let result = client
        .send_request("echo", &[Value::String("Hello there!".into())])
        .await?;
    println!("{}", result);
    Ok(())
}
Commit count: 0

cargo fmt