A flexible ipc rust library supporting several protocols
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
Julius Riegel eefef04092
Merge pull request #10 from Trivernis/develop
Develop
3 years ago
.github/workflows Add publish github action 3 years ago
src Add namespaces from structs and restructure exports 3 years ago
.gitignore Update dependencies 3 years ago
Cargo.lock Add namespaces from structs and restructure exports 3 years ago
Cargo.toml Add namespaces from structs and restructure exports 3 years ago
LICENSE Add README, LICENSE and metadata for crates.io 4 years ago
README.md Add namespaces from structs and restructure exports 3 years ago

README.md

rmp-ipc

Interprocess Communication via TCP using Rust MessagePack.

Usage

Client:

use rmp_ipc::{callback, Event, context::Context, IPCBuilder, error::Result};

/// Callback ping function
async fn handle_ping(ctx: &Context, event: Event) -> Result<()> {
    println!("Received ping event.");
    ctx.emitter.emit_response(event.id(), "pong", ()).await?;
    Ok(())
}

#[tokio::main]
async fn main() {
    // create the client
    let ctx = IPCBuilder::new()
        .address("127.0.0.1:2020")
        // register callback
        .on("ping", callback!(handle_ping))
        .build_client().await.unwrap();

// emit an initial event
    let response = ctx.emitter.emit("ping", ()).await?.await_response(&ctx).await?;
}

Server:

use rmp_ipc::{IPCBuilder, callback};
// create the server

#[tokio::main]
async fn main() {
    IPCBuilder::new()
        .address("127.0.0.1:2020")
        // register callback
        .on("ping", callback!(ctx, event, async move {
            println!("Received ping event.");
            Ok(())
        }))
        .build_server().await.unwrap();
}

Namespaces

Client:

use rmp_ipc::IPCBuilder;
// create the client

#[tokio::main]
async fn main() {
    let ctx = IPCBuilder::new()
        .address("127.0.0.1:2020")
        // register namespace
        .namespace("mainspace-client")
        // register callback (without macro)
        .on("ping", |_ctx, _event| Box::pin(async move {
            println!("Received ping event.");
            Ok(())
        }))
        .build()
        .build_client().await.unwrap();

// emit an initial event
    let response = ctx.emitter.emit_to("mainspace-server", "ping", ()).await?
        .await_response(&ctx).await?;
}

Server:

use rmp_ipc::{IPCBuilder, EventHandler, namespace, command, Event, context::Context};
// create the server

pub struct MyNamespace;

impl MyNamespace {
     async fn ping(_ctx: &Context, _event: Event) -> Result<()> {
         println!("My namespace received a ping");
         Ok(())
     }
}

impl NamespaceProvider for MyNamespace {
     fn name() -> String {String::from("my_namespace")}
 
     fn register(handler: &mut EventHandler) {
         handler.on("ping", callback!(Self::ping))
     }
}

#[tokio::main]
async fn main() {
    IPCBuilder::new()
        .address("127.0.0.1:2020")
        // register namespace
        .namespace("mainspace-server")
        // register callback
        .on("ping", |_ctx, _event| Box::pin(async move {
            println!("Received ping event.");
            Ok(())
        }))
        .build()
        .add_namespace(namespace!(MyNamespace))
        .build_server().await.unwrap();
}

License

Apache-2.0