Skip to main content

Crate folk_protocol

Crate folk_protocol 

Source
Expand description

§folk-protocol

Wire format for Folk: typed RPC messages and length-prefixed framing.

§What this crate provides

  • RpcMessage: the three message variants of MessagePack-RPC (Request, Response, Notify), encoded as positional MessagePack arrays.
  • FrameCodec: a Tokio codec that combines a 4-byte big-endian length prefix with rmp_serde payload encoding/decoding.
  • Error: a single error type for all fallible operations in this crate.

§Wire format

Every message on the wire is:

+----------------+---------------------------------+
|  4-byte length |  MessagePack-encoded array      |
|  big-endian    |  (the RpcMessage payload)       |
+----------------+---------------------------------+

Maximum frame size is MAX_FRAME_SIZE (16 MiB). See folk-spec/spec/02-protocol.md for the full specification.

§Example

use bytes::BytesMut;
use folk_protocol::{FrameCodec, RpcMessage};
use rmpv::Value;
use tokio_util::codec::{Decoder, Encoder};

let mut codec = FrameCodec::new();
let msg = RpcMessage::request(1, "echo", Value::String("hello".into()));

let mut buf = BytesMut::new();
codec.encode(msg.clone(), &mut buf).unwrap();

let decoded = codec.decode(&mut buf).unwrap().unwrap();
assert_eq!(msg, decoded);

Re-exports§

pub use codec::FrameCodec;
pub use error::Error;
pub use error::MAX_FRAME_SIZE;
pub use error::Result;
pub use message::RpcMessage;

Modules§

codec
Tokio codec for length-prefixed MessagePack-RPC frames.
error
Error type for the protocol crate.
message
MessagePack-RPC message types.