| Crates.io | sansio-codec |
| lib.rs | sansio-codec |
| version | 0.7.0 |
| created_at | 2025-12-02 03:33:45.433125+00 |
| updated_at | 2025-12-18 21:18:41.178892+00 |
| description | Protocol codecs for sansio: frame decoders, string codecs, and transport abstractions |
| homepage | https://webrtc.rs |
| repository | https://github.com/webrtc-rs/sansio |
| max_upload_size | |
| id | 1961100 |
| size | 26,169 |
Protocol codecs for the sansio ecosystem, providing reusable frame decoders, string codecs, and transport abstractions.
[dependencies]
sansio-codec = "0.0.8"
use sansio::Pipeline;
use sansio_codec::{
LineBasedFrameDecoder, TaggedByteToMessageCodec,
TaggedStringCodec, TerminatorType
};
use std::rc::Rc;
let pipeline = Rc::new(Pipeline::new());
// Add line-based frame decoder (splits on \n or \r\n)
let frame_decoder = TaggedByteToMessageCodec::new(Box::new(
LineBasedFrameDecoder::new(8192, true, TerminatorType::BOTH)
));
pipeline.add_back(frame_decoder);
// Add string codec (converts bytes to UTF-8 strings)
pipeline.add_back(TaggedStringCodec::new());
// Add your business logic handler
// pipeline.add_back(your_handler);
pipeline.update();
All messages are wrapped with transport metadata:
use sansio_codec::{TransportContext, TaggedBytesMut, TransportProtocol};
use std::time::Instant;
use bytes::BytesMut;
let msg = TaggedBytesMut {
now: Instant::now(),
transport: TransportContext {
local_addr: "127.0.0.1:8080".parse().unwrap(),
peer_addr: "127.0.0.1:1234".parse().unwrap(),
transport_protocol: TransportProtocol::TCP,
ecn: None,
},
message: BytesMut::from("Hello"),
};
byte_to_message_decoderFrame decoders for parsing byte streams:
LineBasedFrameDecoder: Splits frames on line terminators (\n, \r\n, or both)TaggedByteToMessageCodec: Handler wrapper for any MessageDecoderTerminatorType: Configure line terminator type (BOTH, NEWLINE, CarriageNewline)string_codecString encoding/decoding:
TaggedStringCodec: Converts between TaggedBytesMut and TaggedStringtransportTransport abstractions:
TransportContext: Local/peer addresses, transport protocol type, ECN bitsTaggedBytesMut: BytesMut message with transport contextTaggedString: String message with transport contextTransportProtocol: TCP or UDPFourTuple: Local and peer addressesFiveTuple: Local address, peer address, and transport protocolSee the examples directory for complete working examples using these codecs, including:
Licensed under either of:
at your option.