Crates.io | stun_codec |
lib.rs | stun_codec |
version | 0.3.5 |
source | src |
created_at | 2018-09-03 15:33:59.470115 |
updated_at | 2024-04-03 09:57:33.604803 |
description | Encoders and decoders for STUN (RFC 5389) and its extensions |
homepage | https://github.com/sile/stun_codec |
repository | https://github.com/sile/stun_codec |
max_upload_size | |
id | 82764 |
size | 176,389 |
Encoders and decoders for STUN (RFC 5389) and its extensions.
use bytecodec::{DecodeExt, EncodeExt, Error};
use stun_codec::{Message, MessageClass, MessageDecoder, MessageEncoder, TransactionId};
use stun_codec::rfc5389::{attributes::Software, methods::BINDING, Attribute};
// Creates a message
let mut message = Message::new(MessageClass::Request, BINDING, TransactionId::new([3; 12]));
message.add_attribute(Attribute::Software(Software::new("foo".to_owned())?));
// Encodes the message
let mut encoder = MessageEncoder::new();
let bytes = encoder.encode_into_bytes(message.clone())?;
assert_eq!(
bytes,
[
0, 1, 0, 8, 33, 18, 164, 66, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 128, 34, 0, 3,
102, 111, 111, 0
]
);
// Decodes the message
let mut decoder = MessageDecoder::<Attribute>::new();
let decoded = decoder.decode_from_bytes(&bytes)?.map_err(Error::from)?;
assert_eq!(decoded.class(), message.class());
assert_eq!(decoded.method(), message.method());
assert_eq!(decoded.transaction_id(), message.transaction_id());
assert!(decoded.attributes().eq(message.attributes()));