Struct faster_stun::message::MessageWriter
source · pub struct MessageWriter<'a> { /* private fields */ }
Implementations§
source§impl<'a, 'b> MessageWriter<'a>
impl<'a, 'b> MessageWriter<'a>
sourcepub fn extend(
method: Method,
reader: &MessageReader<'a, 'b>,
raw: &'a mut BytesMut
) -> Self
pub fn extend(
method: Method,
reader: &MessageReader<'a, 'b>,
raw: &'a mut BytesMut
) -> Self
rely on old message to create new message.
Unit Test
use faster_stun::*;
use bytes::BytesMut;
use std::convert::TryFrom;
let buffer = [
0x00u8, 0x01, 0x00, 0x00, 0x21, 0x12, 0xa4, 0x42, 0x72, 0x6d, 0x49,
0x42, 0x72, 0x52, 0x64, 0x48, 0x57, 0x62, 0x4b, 0x2b,
];
let mut attributes = Vec::new();
let mut buf = BytesMut::new();
let old = MessageReader::decode(&buffer[..], &mut attributes).unwrap();
MessageWriter::extend(Method::Binding(Kind::Request), &old, &mut buf);
assert_eq!(&buf[..], &buffer[..]);
sourcepub fn append<T: Property<'a>>(&mut self, value: T::Inner)
pub fn append<T: Property<'a>>(&mut self, value: T::Inner)
append attribute.
append attribute to message attribute list.
Unit Test
use faster_stun::*;
use faster_stun::attribute::UserName;
use std::convert::TryFrom;
use bytes::BytesMut;
let buffer = [
0x00u8, 0x01, 0x00, 0x00, 0x21, 0x12, 0xa4, 0x42, 0x72, 0x6d, 0x49,
0x42, 0x72, 0x52, 0x64, 0x48, 0x57, 0x62, 0x4b, 0x2b,
];
let new_buf = [
0x00u8, 0x01, 0x00, 0x00, 0x21, 0x12, 0xa4, 0x42, 0x72, 0x6d, 0x49,
0x42, 0x72, 0x52, 0x64, 0x48, 0x57, 0x62, 0x4b, 0x2b, 0x00, 0x06, 0x00,
0x05, 0x70, 0x61, 0x6e, 0x64, 0x61, 0x00, 0x00, 0x00,
];
let mut buf = BytesMut::new();
let mut attributes = Vec::new();
let old = MessageReader::decode(&buffer[..], &mut attributes).unwrap();
let mut message =
MessageWriter::extend(Method::Binding(Kind::Request), &old, &mut buf);
message.append::<UserName>("panda");
assert_eq!(&new_buf[..], &buf[..]);
sourcepub fn flush(&mut self, auth: Option<&[u8; 16]>) -> Result<()>
pub fn flush(&mut self, auth: Option<&[u8; 16]>) -> Result<()>
try decoder bytes as message.
Unit Test
use faster_stun::*;
use bytes::BytesMut;
use std::convert::TryFrom;
let buffer = [
0x00u8, 0x01, 0x00, 0x00, 0x21, 0x12, 0xa4, 0x42, 0x72, 0x6d, 0x49,
0x42, 0x72, 0x52, 0x64, 0x48, 0x57, 0x62, 0x4b, 0x2b,
];
let result = [
0x00u8, 0x01, 0x00, 0x20, 0x21, 0x12, 0xa4, 0x42, 0x72, 0x6d, 0x49,
0x42, 0x72, 0x52, 0x64, 0x48, 0x57, 0x62, 0x4b, 0x2b, 0x00, 0x08, 0x00,
0x14, 0x45, 0x0e, 0x6e, 0x44, 0x52, 0x1e, 0xe8, 0xde, 0x2c, 0xf0, 0xfa,
0xb6, 0x9c, 0x5c, 0x19, 0x17, 0x98, 0xc6, 0xd9, 0xde, 0x80, 0x28, 0x00,
0x04, 0xed, 0x41, 0xb6, 0xbe,
];
let mut attributes = Vec::new();
let mut buf = BytesMut::with_capacity(1280);
let old = MessageReader::decode(&buffer[..], &mut attributes).unwrap();
let mut message =
MessageWriter::extend(Method::Binding(Kind::Request), &old, &mut buf);
message
.flush(Some(&util::long_key("panda", "panda", "raspberry")))
.unwrap();
assert_eq!(&buf[..], &result);