An interprocess message bus system built in Rust.

Overview

An interprocess message bus system built in Rust, which can be used to pass messages between multiple processes, even including kernel objects (HANDLE/MachPort).

Goals

  • Easy to use: join, send, recv, that's everything
  • Bus architecture: No server or client, messages can be freely transmitted among multiple endpoints
  • Message typing: An endpoint can send or receive multiple message types simultaneously without all endpoints defining a complete global message structure
  • Practical features: Object, Memory Region, Selector and so on

Getting Started

[dependencies]
ipmb = "0.7"

earth.rs:

use ipmb::label;

fn main () {
    // Join your bus 
    let options = ipmb::Options::new("com.solar", label!("earth"), "");
    let (sender, receiver) = ipmb::join::<String, String>(options, None).expect("Join com.solar failed");

    // Receive messages
    while let Ok(message) = receiver.recv(None) {
        log::info!("received: {}", message.payload);
    }
}

moon.rs:

use ipmb::label;
use std::thread;
use std::time::Duration;

fn main () {
    // Join your bus 
    let options = ipmb::Options::new("com.solar", label!("moon"), "");
    let (sender, receiver) = ipmb::join::<String, String>(options, None).expect("Join com.solar failed");

    loop {
        // Create a message
        let selector = ipmb::Selector::unicast("earth");
        let mut message = ipmb::Message::new(selector, "hello world".to_string());

        // Send the message
        sender.send(message).expect("Send message failed");
        
        thread::sleep(Duration::from_secs(1));
    }
}

Concepts

Identifier

An identifier is a system-level unique name for a bus, and only endpoints on the same bus can communicate with each other. On macOS, it will be used to register the MachPort service, and on Windows, it will be used to create the corresponding named pipe.

Label

Label is the description of an endpoint, and a message can be routed to an endpoint with a specific label. A label can contain multiple elements, such as label!("renderer", "codec").

Selector

Selector is used to describe the routing rules of the message, which consists of 2 parts:

  1. SelectorMode: Specify how to consume the message when multiple endpoints satisfy routing rules at the same time.
    • Unicast: Only one endpoint can consume this message
    • Multicast: All endpoints can consume this message
  2. LabelOp: Describe the matching rules of label, and supports logical operations of AND/OR/NOT.

Payload

Payload is the body content of a message, and its type can be specified by the type parameter of the join function. You can define your own message types:

use serde::{Deserialize, Serialize};
use type_uuid::TypeUuid;

#[derive(Debug, Serialize, Deserialize, TypeUuid)]
#[uuid = "7b07473e-9659-4d47-a502-8245d71c0078"]
struct MyMessage {
    foo: i32,
    bar: bool,
}

fn main() {
    let (sender, receiver) = ipmb::join::<MyMessage, MyMessage>(..).unwrap();
}

MessageBox

MessageBox is a container for multiple message types, allowing endpoints to send/receive multiple message types.

use ipmb::MessageBox;

#[derive(MessageBox)]
enum MultipleMessage {
   String(String),
   I32(i32),
   MyMessage(MyMessage),
}

fn main() {
    let (sender, receiver) = ipmb::join::<MultipleMessage, MultipleMessage>(..).unwrap();
}

Object

Object is the kernel object representation, MachPort on macOS, HANDLE on Windows, ipmb supports sending Object as message attachment to other endpoints.

fn main () {
   let mut message = ipmb::Message::new(..);
   let obj = unsafe { ipmb::Object::from_raw(libc::mach_task_self()) };
   message.objects.push(obj);
}

MemoryRegion

MemoryRegion is a shared memory block, ipmb supports sending MemoryRegion as message attachment to other endpoints without copying.

fn main() {
   let mut message = ipmb::Message::new(..);
   let mut region = ipmb::MemoryRegion::new(16 << 10);
   let data = region.map(..).expect("Mapping failed");
   data[0] = 0x10;
   message.memory_regions.push(region);
}

MemoryRegistry

Efficiently performs many MemoryRegions allocation by sharing and reusing MemoryRegions.

fn main() {
   let mut registry = ipmb::MemoryRegistry::default();
   // Alloc memory region from the registry
   let mut region = registry.alloc(8 << 20, None);
}

Language Bindings

  1. C/C++: ipmb-ffi provides ipmb_ffi.h/ipmb.h
  2. Node.js: ipmb-js provides node package

Supported Platforms

Platform
macOS
Windows
Linux 🛠️

Benchmark

Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz, macOS 13.4

[2023-06-29T08:54:48Z INFO  bench]       16 B    752,469/s    12.0 MB/s
[2023-06-29T08:54:48Z INFO  bench]       64 B    437,096/s    28.0 MB/s
[2023-06-29T08:54:48Z INFO  bench]     1.0 KB    412,224/s   422.1 MB/s
[2023-06-29T08:54:48Z INFO  bench]     4.1 KB    327,748/s     1.3 GB/s
[2023-06-29T08:54:49Z INFO  bench]    16.4 KB     33,261/s   544.9 MB/s

License

ipmb is dual-licensed:

You might also like...
Nostr CLI client built with Rust

Nostr CLI client built with Rust Encrypted chat inside Nostr leaks metadata of who talks to who. This small CLI app implements a "public inbox," which

A lightweight focus CLI tool built with Rust

A lightweight pomodoro focus tool with cross-platform desktop notifications on Linux, MacOS and Windows.

Kurzlink is a simple static site generator built in rust

kurzlink What is kurzlink? Kurzlink is a simple static site generator built in rust.

Password manager built in Rust using SurrealDB and MagicCrypt.

you-shall-pass Password manager built in Rust using SurrealDB and MagicCrypt. Features Store using file backed SurrealDB. Encrypt passwords before sto

Open-source Fortnite launcher, built in Rust.

Instigator Instigator is a basic command-line Fortnite launcher I've been working on for the last day and a bit. It is extremely basic. It injects con

An easy-to-use SocketCAN library for Python and C++, built in Rust.

JCAN An easy-to-use SocketCAN library for Python and C++, built in Rust, using cxx-rs and pyo3. Warning: I have never used Rust before and I don't kno

A TUI for your todos built in Rust with full CLI support.

todui TUI Features This app allows for almost anythig you would need when dealing with todos: Create, edit, and delete tasks Add links to tasks Add du

Nodium is an easy-to-use data analysis and automation platform built using Rust, designed to be versatile and modular.
Nodium is an easy-to-use data analysis and automation platform built using Rust, designed to be versatile and modular.

Nodium is an easy-to-use data analysis and automation platform built using Rust, designed to be versatile and modular. Nodium aims to provide a user-friendly visual node-based interface for various tasks.

Experimental OS, built with rust

You can support this night time project by hiring me for a day time job ! Fomos Experimental OS, built with Rust output.mp4 Fun fact: there are 3 apps

Comments
  • ipmb-js: refactor thread model

    ipmb-js: refactor thread model

    Description

    Each recv call of ipmb-js will generate a blocking task on the uv thread pool. If multiple endpoints in a process call recv concurrently, the uv thread pool will be exhausted.

    This pr refactors the thread model ofipmb-js and adds a LocalBuffer structure, which contains the message receiving buffer and the deferred list. Each time recv is called, If the message buffer is not empty the deferred will be resolved directly, otherwise the deferred will be registered in the deferred list; the receiver thread will dispatch a resolve proxy task to uv thread pool after receiving a message.

    opened by xiaopengli89 0
Owner
Bytedance Inc.
Bytedance Inc.
A Rust program/library to write a Hello World message to the standard output.

hello-world Description hello-world is a Rust program and library that prints the line Hello, world! to the console. Why was this created? This progra

null 0 May 11, 2022
Galileo OSNMA (Open Service Navigation Message Authentication)

galileo-osnma galileo-osnma is a Rust implementation of the Galileo OSNMA (Open Service Navigation Message Authentication) protocol. This protocol is

Daniel Estévez 26 Nov 25, 2022
Databento Binary Encoding (DBZ) - Fast message encoding and storage format for market data

dbz A library (dbz-lib) and CLI tool (dbz-cli) for working with Databento Binary Encoding (DBZ) files. Python bindings for dbz-lib are provided in the

Databento, Inc. 15 Nov 4, 2022
First project in rust which will be to make an accounts system & Leaderboard/Score system

rust-backend this is my first project in rust which will be to make a backend for compsci project it will include: Accounts, Player Achievements (if I

NaughtyDog6000 2 Jul 13, 2023
RustGPT is a ChatGPT UI built with Rust + HTMX: the power of Rust coupled with the simplicity of HTMX 💚

RustGPT ??✨ RustGPT.Blog.Post.mp4 Welcome to the RustGPT repository! Here, you'll find a web ChatGPT clone entirely crafted using Rust and HTMX, where

Bitswired 529 Dec 4, 2023
Real-time CLI level meter built in Rust.

Meter This is a very simple command line utility written in Rust for measuring the gain of a microphone. It displays the values in dBFS. This is usefu

Chris Burgess 16 Sep 8, 2022
🔭 Search Dash.app from Neovim with Telescope. Built with Rust 🦀 and Lua

Dash.nvim Query Dash.app within Neovim with a Telescope picker! The theme used in the recording is lighthaus.nvim. Note: Dash is a Mac-only app, so yo

Mat Jones 193 Dec 28, 2022
nhdl - a command-line doujinshi downloader client built in rust!

nhdl nhdl - a command-line doujinshi downloader client built in rust! goals fast and asynchronous doujinshi downloader, supporting various doujinshi s

Lumine 2 Dec 23, 2022
A blazingly fast Insertion Sort and Quick Sort visualizer built with Rust and WASM.

sortysort A blazingly fast Insertion Sort and Quick Sort visualizer built with Rust and WASM. Try it in your browser from here Testing locally cargo r

null 3 Jan 28, 2022
a universal meta-transliterator that can decipher arbitrary encoding schemas, built in pure Rust

transliterati a universal meta-transliterator that can decipher arbitrary encoding schemas, built in pure Rust what does it do? You give it this: Барл

Catherine Koshka 7 Dec 21, 2022