memlink-protocol
Binary protocol definitions for MemLink IPC
Overview
memlink-protocol provides the core protocol definitions for MemLink IPC, including fixed 32-byte message headers, version negotiation, feature flags, and MessagePack serialization.
Key Features:
- ๐ฆ Fixed 32-byte message headers with packed representation
- ๐ข Magic numbers and version constants for protocol identification
- ๐ท๏ธ Type aliases for consistent sizing across platforms
- โ Comprehensive error types for all failure modes
- ๐ Version negotiation with feature flag support
- โก Zero-copy message parsing from shared memory
- ๐ฏ Arena-backed slices for efficient memory management
- ๐ Stream handles for large payload transfers
Features
| Feature | Description |
|---|---|
std |
Enable std library support (for threading tests) |
shm |
Enable integration with memlink-shm crate |
Quick Start
use ;
use MessagePackSerializer;
use Serializer;
// Create a request header
let header = new;
// Validate the header
assert!;
// Convert to bytes for transmission
let bytes = header.as_bytes;
// Parse from bytes
let parsed = from_bytes?;
// Create and serialize a request
let request = new;
let bytes = MessagePackSerializer.serialize_request?;
let parsed = MessagePackSerializer.deserialize_request?;
๐ More examples
Wire Format
All multi-byte fields use big-endian byte order for network transmission:
| Offset | Size | Field | Type | Description |
|---|---|---|---|---|
| 0 | 4 | magic | u32 | Magic number (0x4D4C4E4B) |
| 4 | 1 | version | u8 | Protocol version |
| 5 | 1 | msg_type | u8 | Message type |
| 6 | 2 | features | u16 | Feature flags (big-endian) |
| 8 | 8 | request_id | u64 | Request identifier |
| 16 | 8 | module_id | u64 | Module identifier |
| 24 | 4 | method_hash | u32 | Method name hash |
| 28 | 4 | payload_len | u32 | Payload size in bytes |
Total: 32 bytes (exactly)
Protocol Constants
| Constant | Value | Description |
|---|---|---|
MEMLINK_MAGIC |
0x4D4C4E4B |
"MLNK" in ASCII, big-endian |
PROTOCOL_VERSION |
1 |
Current protocol version |
HEADER_SIZE |
32 |
Header size in bytes |
MAX_PAYLOAD_SIZE |
64 MiB |
Maximum payload (67,108,864 bytes) |
CONTROL_REGION_SIZE |
4 KiB |
SHM control region (4,096 bytes) |
Type Aliases
| Type | Alias | Description |
|---|---|---|
RequestId |
u64 |
Unique identifier for request/response correlation |
ModuleId |
u64 |
Identifier for modules or services |
MethodHash |
u32 |
FNV-1a hash of method name for dispatch |
TraceId |
u128 |
Distributed tracing identifier |
SpanId |
u64 |
Span identifier within a trace |
Message Types
| Type | Value | Description |
|---|---|---|
Request |
0 | Initiates an operation (expects response) |
Response |
1 | Completes an operation |
Error |
2 | Indicates failure |
StreamHandle |
3 | Stream handle for streaming operations |
HealthCheck |
16 | Health check request/response |
LoadModule |
32 | Load a new module |
UnloadModule |
33 | Unload a module |
Stats |
48 | Statistics request/response |
Event |
96 | Event notification |
Status Codes
| Status | Value | Description |
|---|---|---|
Success |
0 | Operation completed successfully |
ModuleNotFound |
1 | The specified module was not found |
MethodNotFound |
2 | The specified method was not found |
ExecutionError |
3 | Execution failed with an error |
Timeout |
4 | Operation timed out |
QuotaExceeded |
5 | Resource quota exceeded |
BackpressureRejection |
6 | Request rejected due to backpressure |
Feature Flags
| Feature | Bit | Description |
|---|---|---|
STREAMING |
0 | Chunked transfer support |
BATCHING |
1 | Batch message grouping |
PRIORITY_DEGRADATION |
2 | Priority-based fallback |
Version Negotiation
use ;
// Negotiate between client and server versions
let result = negotiate_version;
assert!;
assert_eq!; // Negotiates to lowest common
// Validate version support
assert!;
assert!;
Performance
Benchmark results from cargo bench -p memlink-protocol:
Serialization Performance
| Operation | Time | Throughput |
|---|---|---|
| Request serialize | 1.07 ยตs | 4.59 MiB/s |
| Request deserialize | 1.11 ยตs | 4.44 MiB/s |
| Response serialize | 1.17 ยตs | 4.16 MiB/s |
| Response deserialize | 491 ns | 10.2 MiB/s |
| Error serialize | 1.00 ยตs | 20.8 MiB/s |
| Error deserialize | 477 ns | 43.7 MiB/s |
Large Payload Performance
| Payload | Serialize | Deserialize |
|---|---|---|
| 1 KB | 18.2 ยตs (55.5 MiB/s) | 12.0 ยตs (83.3 MiB/s) |
| 10 KB | 109 ยตs (89.4 MiB/s) | 121 ยตs (81.0 MiB/s) |
| 100 KB | 1.30 ms (75.4 MiB/s) | 1.55 ms (62.9 MiB/s) |
Header Operations
| Operation | Time |
|---|---|
as_bytes() |
46 ns |
from_bytes() |
37 ns |
validate() |
<1 ns |
๐ Run benchmarks
Error Handling
use ProtocolError;
match header.validate
Error Recovery
| Error Type | Recoverable | Description |
|---|---|---|
Timeout |
โ Yes | Retry with backoff |
BufferOverflow |
โ Yes | Apply backpressure |
QuotaExceeded |
โ Yes | Wait for resources |
InvalidMagic |
โ No | Data corruption |
UnsupportedVersion |
โ No | Protocol mismatch |
Design Principles
- Binary-only: No JSON or text-based protocols for maximum performance
- Fixed header size: Exactly 32 bytes for predictable memory layout
- Platform-independent: Fixed-width types (u32, u64) and big-endian wire format
- Explicit packing:
#[repr(C, packed)]to prevent padding - Safe conversion: No
std::mem::transmutefor byte conversion
Safety
This crate follows strict safety guidelines:
- โ
No
unsafecode in core implementation - โ
No
transmutefor byte conversion (uses explicitto_be_bytes) - โ Compile-time size assertions for header structure
- โ Comprehensive validation before parsing
- โ Bounds checking on all buffer operations
Development
Prerequisites
- Rust 1.70 or later
Building
# Build the crate
# Build with SHM integration
Testing
# Run all tests
# Run integration tests
# Run doc tests
Benchmarks
# Run all benchmarks
# Run specific benchmark
Code Quality
# Format code
# Run clippy
Examples
# Run the basic example
Related Crates
| Crate | Description |
|---|---|
| memlink-shm | Shared memory IPC library |
| memlink-runtime | Dynamic module loading |
| memlink-msdk | Module SDK |
| memlink-msdk-macros | Proc macros |
License
Apache-2.0 license.
See LICENSE-APACHE for the full license text.
Contributing
Contributions are welcome! Please follow the project guidelines:
- Add tests for new functionality
- Ensure
cargo clippypasses with no warnings - Update documentation for API changes
- Follow the existing code style
memlink-protocol - Fast, reliable binary protocol for Rust IPC