| Crates.io | tokio_ipc |
| lib.rs | tokio_ipc |
| version | 0.1.0 |
| created_at | 2025-10-20 04:58:32.472592+00 |
| updated_at | 2025-10-20 04:58:32.472592+00 |
| description | Multi-protocol RPC framework built on top of tokio |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1891462 |
| size | 163,979 |
A high-performance, multi-protocol RPC framework built on top of tokio_socket, enabling type-safe inter-process communication with support for multiple protocols over a single connection.
Define your protocols using the protocol! macro:
use tokio_ipc::protocol;
protocol! {
pub mod my_protocol {
// Request with response
echo { message: String } -> { response: String };
// Request without response (fire-and-forget)
notify { event: String };
// No parameters
get_status -> { status: String };
}
}
Implement the protocol handler:
#[derive(Clone)]
struct MyHandler;
impl my_protocol::Receive for MyHandler {
async fn echo(&self, message: String) -> Result<my_protocol::echo::Response> {
Ok(my_protocol::echo::Response {
response: format!("Echo: {}", message)
})
}
async fn notify(&self, event: String) -> Result<()> {
println!("Event: {}", event);
Ok(())
}
async fn get_status(&self) -> Result<my_protocol::get_status::Response> {
Ok(my_protocol::get_status::Response {
status: "healthy".to_string()
})
}
}
The examples/ directory contains comprehensive real-world examples demonstrating various use cases:
basic.rs)The simplest multi-protocol example showing:
app_protocol + admin_protocol)Run it:
# Demo mode (integrated)
cargo run --example basic
# Separate processes
cargo run --example basic -- --server # Terminal 1
cargo run --example basic -- --client # Terminal 2
bidirectional.rs)Demonstrates full-duplex communication:
Run it:
cargo run --example bidirectional
file_transfer.rs)Real-world file transfer with progress tracking:
file_protocol + progress_protocolKey Features:
Run it:
cargo run --example file_transfer
database.rs)In-memory database with transaction support:
db_protocol + replication_protocolKey Features:
Run it:
cargo run --example database
service_mesh.rs)Service discovery and health monitoring:
service_protocol + health_protocol + discovery_protocolKey Features:
Run it:
# Demo mode
cargo run --example service_mesh
# Separate processes
cargo run --example service_mesh -- --registry # Terminal 1
cargo run --example service_mesh -- --service # Terminal 2
cargo run --example service_mesh -- --client # Terminal 3
Implement multiple protocol traits on one handler:
#[derive(Clone)]
struct MyService {
state: Arc<RwLock<ServiceState>>,
}
impl protocol_a::Receive for MyService { /* ... */ }
impl protocol_b::Receive for MyService { /* ... */ }
impl protocol_c::Receive for MyService { /* ... */ }
// Generate sender and receiver
tokio_ipc::protocol_sender!(MySender impl [protocol_a, protocol_b, protocol_c]);
tokio_ipc::protocol_handler!(MyReceiver impl [protocol_a, protocol_b, protocol_c] with MyService);
Compose protocols at runtime:
let registry = tokio_ipc::registry! {
protocol_a => HandlerA::new(),
protocol_b => HandlerB::new(),
protocol_c => HandlerC::new(),
};
Server can call client methods:
// Server handler receives client sender
impl RpcServerHandler for MyServer {
type SendRpc = ClientSender; // Can call client methods
type ReceiveRpc = ServerReceiver;
async fn on_rpc_connect(&self, client_sender: &Self::SendRpc) -> Self::ReceiveRpc {
// Server can now call methods on the client
client_sender.notify("Welcome!".to_string()).await?;
// ...
}
}
Each protocol should handle one aspect of functionality:
-> { fields } for operations that need acknowledgmentProtocol IDs are auto-generated from name + version:
#[version(2)]
pub mod my_protocol {
// Protocol version 2
}
All examples have comprehensive integration tests in tests/test_new_examples.rs:
# Run all example tests
cargo test --test test_new_examples
# Run specific example test
cargo test --test test_new_examples test_file_transfer_demo_mode
Tests verify:
┌─────────────────────────────────────────────────────────────┐
│ Application Layer │
│ (Your protocol implementations: Receive + Sender traits) │
└─────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────┐
│ Protocol Layer (tokio_ipc) │
│ • Protocol multiplexing (protocol_id routing) │
│ • RPC packet handling (request/response matching) │
│ • Serialization (bincode) │
└─────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────┐
│ Transport Layer (tokio_socket) │
│ • Socket abstraction (TCP/UDS) │
│ • Packet framing (length-prefixed) │
│ • Connection management │
└─────────────────────────────────────────────────────────────┘
See workspace LICENSE file.