MessageEncoder

Struct MessageEncoder 

Source
pub struct MessageEncoder<'a> {
    pub token: &'a [u8],
    pub bytes: &'a mut BytesMut,
}

Fields§

§token: &'a [u8]§bytes: &'a mut BytesMut

Implementations§

Source§

impl<'a, 'b> MessageEncoder<'a>

Source

pub fn new( method: StunMethod, token: &'a [u8; 12], bytes: &'a mut BytesMut, ) -> Self

Source

pub fn extend( method: StunMethod, reader: &MessageRef<'a>, bytes: &'a mut BytesMut, ) -> Self

rely on old message to create new message.

§Test
use bytes::BytesMut;
use std::convert::TryFrom;
use turn_server::stun::method::{
    StunMethod as Method, StunMethodKind as Kind,
};
use turn_server::stun::*;

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 = Attributes::default();
let mut buf = BytesMut::new();
let old = MessageDecoder::decode(&buffer[..], &mut attributes).unwrap();
MessageEncoder::extend(Method::Binding(Kind::Request), &old, &mut buf);
assert_eq!(&buf[..], &buffer[..]);
Source

pub fn append<'c, T: Attribute<'c>>(&'c mut self, value: T::Item)

append attribute.

append attribute to message attribute list.

§Test
use bytes::BytesMut;
use std::convert::TryFrom;
use turn_server::stun::attribute::UserName;
use turn_server::stun::method::{
    StunMethod as Method, StunMethodKind as Kind,
};
use turn_server::stun::*;

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 = Attributes::default();
let old = MessageDecoder::decode(&buffer[..], &mut attributes).unwrap();
let mut message =
    MessageEncoder::extend(Method::Binding(Kind::Request), &old, &mut buf);

message.append::<UserName>("panda");
assert_eq!(&new_buf[..], &buf[..]);
Source

pub fn flush(&mut self, digest: Option<&[u8; 16]>) -> Result<(), StunError>

try decoder bytes as message.

§Test
use bytes::BytesMut;
use std::convert::TryFrom;
use turn_server::stun::method::{
    StunMethod as Method, StunMethodKind as Kind,
};
use turn_server::stun::*;

let buffer = [
    0x00u8, 0x01, 0x00, 0x00, 0x21, 0x12, 0xa4, 0x42, 0x72, 0x6d, 0x49,
    0x42, 0x72, 0x52, 0x64, 0x48, 0x57, 0x62, 0x4b, 0x2b,
];

let result = [
    0, 1, 0, 32, 33, 18, 164, 66, 114, 109, 73, 66, 114, 82, 100, 72, 87,
    98, 75, 43, 0, 8, 0, 20, 69, 14, 110, 68, 82, 30, 232, 222, 44, 240,
    250, 182, 156, 92, 25, 23, 152, 198, 217, 222, 128, 40, 0, 4, 74, 165,
    171, 86,
];

let mut attributes = Attributes::default();
let mut buf = BytesMut::with_capacity(1280);
let old = MessageDecoder::decode(&buffer[..], &mut attributes).unwrap();
let mut message =
    MessageEncoder::extend(Method::Binding(Kind::Request), &old, &mut buf);

message
    .flush(Some(&util::long_term_credential_digest(
        "panda",
        "panda",
        "raspberry",
    )))
    .unwrap();
assert_eq!(&buf[..], &result);

Auto Trait Implementations§

§

impl<'a> Freeze for MessageEncoder<'a>

§

impl<'a> RefUnwindSafe for MessageEncoder<'a>

§

impl<'a> Send for MessageEncoder<'a>

§

impl<'a> Sync for MessageEncoder<'a>

§

impl<'a> Unpin for MessageEncoder<'a>

§

impl<'a> !UnwindSafe for MessageEncoder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,