Struct stun_types::message::MessageBuilder
source · pub struct MessageBuilder<'a> { /* private fields */ }Implementations§
source§impl<'a> MessageBuilder<'a>
impl<'a> MessageBuilder<'a>
sourcepub fn into_owned<'b>(self) -> MessageBuilder<'b>
pub fn into_owned<'b>(self) -> MessageBuilder<'b>
Consume this builder and produce a new owned version.
sourcepub fn transaction_id(&self) -> TransactionId
pub fn transaction_id(&self) -> TransactionId
Retrieves the 96-bit transaction ID of the Message
§Examples
let mtype = MessageType::from_class_method(MessageClass::Request, BINDING);
let transaction_id = TransactionId::generate();
let message = Message::builder(mtype, transaction_id).build();
let message = Message::from_bytes(&message).unwrap();
assert_eq!(message.transaction_id(), transaction_id);sourcepub fn has_class(&self, cls: MessageClass) -> bool
pub fn has_class(&self, cls: MessageClass) -> bool
Whether this MessageBuilder is for a particular MessageClass
sourcepub fn build(&self) -> Vec<u8>
pub fn build(&self) -> Vec<u8>
Serialize a MessageBuilder to network bytes
§Examples
let mut message = Message::builder(MessageType::from_class_method(MessageClass::Request, BINDING), 1000.into());
let attr = RawAttribute::new(1.into(), &[3]);
assert!(message.add_attribute(attr).is_ok());
assert_eq!(message.build(), vec![0, 1, 0, 8, 33, 18, 164, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 232, 0, 1, 0, 1, 3, 0, 0, 0]);sourcepub fn add_message_integrity(
&mut self,
credentials: &MessageIntegrityCredentials,
algorithm: IntegrityAlgorithm,
) -> Result<(), StunWriteError>
pub fn add_message_integrity( &mut self, credentials: &MessageIntegrityCredentials, algorithm: IntegrityAlgorithm, ) -> Result<(), StunWriteError>
Adds MESSAGE_INTEGRITY attribute to a Message using the provided credentials
§Errors
- If a
MessageIntegrityattribute is already present - If a
MessageIntegritySha256attribute is already present - If a
Fingerprintattribute is already present
§Examples
MessageIntegrityCredentials, ShortTermCredentials, IntegrityAlgorithm, StunWriteError};
let mut message = Message::builder_request(BINDING);
let credentials = ShortTermCredentials::new("pass".to_owned()).into();
assert!(message.add_message_integrity(&credentials, IntegrityAlgorithm::Sha1).is_ok());
// duplicate MessageIntegrity is an error
assert!(matches!(
message.add_message_integrity(&credentials, IntegrityAlgorithm::Sha1),
Err(StunWriteError::AttributeExists(MessageIntegrity::TYPE)),
));
// only one of MessageIntegrity, and MessageIntegritySha256 is allowed
assert!(matches!(
message.add_message_integrity(&credentials, IntegrityAlgorithm::Sha256),
Err(StunWriteError::AttributeExists(MessageIntegrity::TYPE)),
));
let data = message.build();
let message = Message::from_bytes(&data).unwrap();
assert!(message.validate_integrity(&credentials).is_ok());sourcepub fn add_fingerprint(&mut self) -> Result<(), StunWriteError>
pub fn add_fingerprint(&mut self) -> Result<(), StunWriteError>
Adds Fingerprint attribute to a Message
§Errors
- If a
Fingerprintattribute is already present
§Examples
let mut message = Message::builder_request(BINDING);
assert!(message.add_fingerprint().is_ok());
// duplicate FINGERPRINT is an error
assert!(message.add_fingerprint().is_err());sourcepub fn add_attribute(
&mut self,
attr: impl Into<RawAttribute<'a>>,
) -> Result<(), StunWriteError>
pub fn add_attribute( &mut self, attr: impl Into<RawAttribute<'a>>, ) -> Result<(), StunWriteError>
Add a Attribute to this Message. Only one AttributeType can be added for each
Attribute. Attempting to add multiple Atributes of the same AttributeType` will fail.
§Errors
- If the attribute already exists within the message
- If attempting to add attributes when
MessageIntegrity,MessageIntegritySha256orFingerprintatributes already exist.
§Panics
- if a
MessageIntegrityorMessageIntegritySha256attribute is attempted to be added. UseMessage::add_message_integrityinstead. - if a
Fingerprintattribute is attempted to be added. UseMessage::add_fingerprintinstead.
§Examples
Add an Attribute
let mut message = Message::builder_request(BINDING);
let attr = RawAttribute::new(1.into(), &[3]);
assert!(message.add_attribute(attr.clone()).is_ok());
assert!(message.add_attribute(attr).is_err());sourcepub fn has_attribute(&self, atype: AttributeType) -> bool
pub fn has_attribute(&self, atype: AttributeType) -> bool
Return whether this MessageBuilder contains a particular attribute.
Trait Implementations§
source§impl<'a> Clone for MessageBuilder<'a>
impl<'a> Clone for MessageBuilder<'a>
source§fn clone(&self) -> MessageBuilder<'a>
fn clone(&self) -> MessageBuilder<'a>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<'a> Freeze for MessageBuilder<'a>
impl<'a> RefUnwindSafe for MessageBuilder<'a>
impl<'a> Send for MessageBuilder<'a>
impl<'a> Sync for MessageBuilder<'a>
impl<'a> Unpin for MessageBuilder<'a>
impl<'a> UnwindSafe for MessageBuilder<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit)