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
trivernis 324a788031
Change how replies are emitted
All events that are emitted inside a handler function are seen
as replies to the event that called the handler.
The FromPayloadBytes and ToPayloadBytes traits have been renamed
to FromPayload and IntoPayload. The IntoPayload trait passes the
context in its arguments to allow serializable structures to be
serialized by the default serializer.

Signed-off-by: trivernis <trivernis@protonmail.com>
2 years ago
.github/workflows Update github test action 2 years ago
benches Fix benchmarks 2 years ago
src Change how replies are emitted 2 years ago
tests Change how replies are emitted 2 years ago
.gitignore Update dependencies 3 years ago
Cargo.lock Change how replies are emitted 2 years ago
Cargo.toml Change how replies are emitted 2 years ago
LICENSE Add README, LICENSE and metadata for crates.io 3 years ago
README.md Remove generic bounds from Context, Namespace and EventEmitter 2 years ago
SPECIFICATON.md Change how replies are emitted 2 years ago

README.md

bromine

Asynchronous event driven interprocess communication supporting tcp and unix domain sockets.


Usage

Client:

use bromine::prelude::*;
use tokio::net::TcpListener;

/// 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::<TcpListener>::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 bromine::prelude::*;
use tokio::net::TcpListener;
// create the server

#[tokio::main]
async fn main() {
    IPCBuilder::<TcpListener>::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 bromine::prelude::*;
use tokio::net::TcpListener;
// create the client

#[tokio::main]
async fn main() {
    let ctx = IPCBuilder::<TcpListener>::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 bromine::prelude::*;
use tokio::net::TcpListener;
// 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() -> &'static str {"my_namespace"}
 
     fn register(handler: &mut EventHandler) {
         events!(handler, 
            "ping" => Self::ping
         );
     }
}

#[tokio::main]
async fn main() {
    IPCBuilder::<TcpListener>::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();
}

Benchmarks

Benchmarks are generated on each commit. They can be reviewed here.

License

Apache-2.0