Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions stun-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ implementation could have been realized as follows:

```rust
fn handle_data(in_bytes: &[u8])
-> Result<Vec<StuntClientEvent>, (StunAgentError, Vec<StuntClientEvent>)> {
-> Result<Vec<StunClientEvent>, (StunAgentError, Vec<StunClientEvent>)> {
// Implementation
}
```
Expand Down Expand Up @@ -195,7 +195,7 @@ assert_eq!(events.len(), 1);
let mut iter = events.iter();

// Next event already contains the buffer that needs to be send to the server.
let StuntClientEvent::OutputPacket(buffer) = iter
let StunClientEvent::OutputPacket(buffer) = iter
.next()
.expect("Expected event")
else {
Expand Down Expand Up @@ -227,15 +227,15 @@ let events = client.events();
assert_eq!(events.len(), 2);
let mut iter = events.iter();
// Next event already contains the buffer that needs to be send to the server.
let StuntClientEvent::OutputPacket(buffer) = iter
let StunClientEvent::OutputPacket(buffer) = iter
.next()
.expect("Expected event")
else {
panic!("Expected OutputBuffer event");
};
// Next event indicates that the user must set a timeout for the transaction
// identified by the transaction_id.
let StuntClientEvent::RestransmissionTimeOut((id, duration)) = iter
let StunClientEvent::RestransmissionTimeOut((id, duration)) = iter
.next()
.expect("Expected event")
else {
Expand All @@ -260,13 +260,13 @@ assert_eq!(events.len(), 2);
let mut iter = events.iter();

// Next event contains the buffer that needs to be retransmitted.
let StuntClientEvent::OutputPacket(buffer) = iter
let StunClientEvent::OutputPacket(buffer) = iter
.next()
.expect("Expected event")
else {
panic!("Expected OutputBuffer event");
};
let StuntClientEvent::RestransmissionTimeOut((id, duration)) = iter
let StunClientEvent::RestransmissionTimeOut((id, duration)) = iter
.next()
.expect("Expected event")
else {
Expand Down Expand Up @@ -305,7 +305,7 @@ the client to generate events that can be pulled by the controller.
assert_eq!(events.len(), 1);

let mut iter = events.iter();
let StuntClientEvent::StunMessageReceived(msg) = iter
let StunClientEvent::StunMessageReceived(msg) = iter
.next()
.expect("Expected event")
else {
Expand Down
64 changes: 32 additions & 32 deletions stun-agent/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::events::{StunTransactionError, StuntClientEvent, TransactionEventHandler};
use crate::events::{StunClientEvent, StunTransactionError, TransactionEventHandler};
use crate::fingerprint::{add_fingerprint_attribute, validate_fingerprint};
use crate::integrity::IntegrityError;
use crate::lt_cred_mech::LongTermCredentialClient;
Expand Down Expand Up @@ -279,8 +279,8 @@ impl From<TransportReliability> for StunRttCalcuator {
/// timers, or additional events. This foundational concept is illustrated in the following API:
///
/// ```no_run
/// # use stun_agent::StuntClientEvent;
/// # fn handle_data(in_bytes: &[u8]) -> Vec<StuntClientEvent> { vec![] }
/// # use stun_agent::StunClientEvent;
/// # fn handle_data(in_bytes: &[u8]) -> Vec<StunClientEvent> { vec![] }
/// # fn perform_action() -> Vec<u8> { vec![] }
/// # let in_bytes = [];
/// let events = handle_data(&in_bytes);
Expand All @@ -293,10 +293,10 @@ impl From<TransportReliability> for StunRttCalcuator {
/// generate further events for the controller. This implementation could have been realized as follows:
///
/// ```no_run
/// # use stun_agent::StuntClientEvent;
/// # use stun_agent::StunClientEvent;
/// # use stun_agent::StunAgentError;
/// fn handle_data(in_bytes: &[u8])
/// -> Result<Vec<StuntClientEvent>, (StunAgentError, Vec<StuntClientEvent>)> {
/// -> Result<Vec<StunClientEvent>, (StunAgentError, Vec<StunClientEvent>)> {
/// // Implementation
/// # Ok(vec![])
/// }
Expand All @@ -308,10 +308,10 @@ impl From<TransportReliability> for StunRttCalcuator {
/// success outcomes and the errors and resulting events in case of failure:
///
/// ```no_run
/// # use stun_agent::StuntClientEvent;
/// # use stun_agent::StunClientEvent;
/// # use stun_agent::StunAgentError;
/// # fn handle_data(in_bytes: &[u8]) -> Result<Vec<StuntClientEvent>, (StunAgentError, Vec<StuntClientEvent>)> { Ok(vec![]) }
/// # fn handle_events(events: Vec<StuntClientEvent>) {}
/// # fn handle_data(in_bytes: &[u8]) -> Result<Vec<StunClientEvent>, (StunAgentError, Vec<StunClientEvent>)> { Ok(vec![]) }
/// # fn handle_events(events: Vec<StunClientEvent>) {}
/// # fn handle_error(error: StunAgentError) {}
/// # let in_bytes = [];
/// let response = handle_data(&in_bytes);
Expand Down Expand Up @@ -398,7 +398,7 @@ impl From<TransportReliability> for StunRttCalcuator {
/// is not received within the designated timeout period, the client generates a timeout event, marking
/// the transaction as failed. Timeouts are also employed to manage re-transmissions of requests sent
/// over unreliable transports. When the client needs to set a timeout for a re-transmission, it generates
/// a [`RestransmissionTimeOut`](`crate::StuntClientEvent::RestransmissionTimeOut`) event, which is then
/// a [`RestransmissionTimeOut`](`crate::StunClientEvent::RestransmissionTimeOut`) event, which is then
/// notified to the controller when the events are pulled.
///
/// If multiple timeouts are scheduled, the client will only notify the controller of the most recent
Expand All @@ -420,7 +420,7 @@ impl From<TransportReliability> for StunRttCalcuator {
/// BINDING indication to a STUN server.
///
/// ```rust
/// # use stun_agent::{RttConfig, StunAttributes, StunClienteBuilder, StuntClientEvent, TransportReliability};
/// # use stun_agent::{RttConfig, StunAttributes, StunClienteBuilder, StunClientEvent, TransportReliability};
/// # use stun_rs::methods::BINDING;
/// # use std::time::Instant;
///
Expand Down Expand Up @@ -462,7 +462,7 @@ impl From<TransportReliability> for StunRttCalcuator {
/// let mut iter = events.iter();
///
/// // Next event already contains the buffer that needs to be send to the server.
/// let StuntClientEvent::OutputPacket(buffer) = iter
/// let StunClientEvent::OutputPacket(buffer) = iter
/// .next()
/// .expect("Expected event")
/// else {
Expand All @@ -475,7 +475,7 @@ impl From<TransportReliability> for StunRttCalcuator {
/// The response must arrive before the timeout is reached, otherwise the client will generate a timeout event
/// and will mark the transaction as failed.
/// ```rust
/// # use stun_agent::{RttConfig, StunAttributes, StunClienteBuilder, StuntClientEvent, TransportReliability};
/// # use stun_agent::{RttConfig, StunAttributes, StunClienteBuilder, StunClientEvent, TransportReliability};
/// # use stun_rs::methods::BINDING;
/// # use std::time::Instant;
/// # let mut client = StunClienteBuilder::new(TransportReliability::Unreliable(RttConfig::default()))
Expand All @@ -499,15 +499,15 @@ impl From<TransportReliability> for StunRttCalcuator {
/// assert_eq!(events.len(), 2);
/// let mut iter = events.iter();
/// // Next event already contains the buffer that needs to be send to the server.
/// let StuntClientEvent::OutputPacket(buffer) = iter
/// let StunClientEvent::OutputPacket(buffer) = iter
/// .next()
/// .expect("Expected event")
/// else {
/// panic!("Expected OutputBuffer event");
/// };
/// // Next event indicates that the user must set a timeout for the transaction
/// // identified by the transaction_id.
/// let StuntClientEvent::RestransmissionTimeOut((id, duration)) = iter
/// let StunClientEvent::RestransmissionTimeOut((id, duration)) = iter
/// .next()
/// .expect("Expected event")
/// else {
Expand All @@ -532,13 +532,13 @@ impl From<TransportReliability> for StunRttCalcuator {
/// let mut iter = events.iter();
///
/// // Next event contains the buffer that needs to be retransmitted.
/// let StuntClientEvent::OutputPacket(buffer) = iter
/// let StunClientEvent::OutputPacket(buffer) = iter
/// .next()
/// .expect("Expected event")
/// else {
/// panic!("Expected OutputBuffer event");
/// };
/// let StuntClientEvent::RestransmissionTimeOut((id, duration)) = iter
/// let StunClientEvent::RestransmissionTimeOut((id, duration)) = iter
/// .next()
/// .expect("Expected event")
/// else {
Expand All @@ -553,14 +553,14 @@ impl From<TransportReliability> for StunRttCalcuator {
/// not set a different value, the default timeout is 39500 ms for both, reliable and not
/// reliable transports. If the client has not received a response after that time, the client
/// will consider the transaction to have timed out, and an event of type
/// [`TransactionFailed`](crate::StuntClientEvent::TransactionFailed) will be generated the
/// [`TransactionFailed`](crate::StunClientEvent::TransactionFailed) will be generated the
/// next time that events were pulled with the error
/// [`TimedOut`](crate::StunTransactionError::TimedOut) for the transaction.
///
/// To finish, the next example shows how to handle buffers received from the server. Raw buffers will
/// be processed by the client to generate events that can be pulled by the controller.
///```rust
/// # use stun_agent::{RttConfig, StunAttributes, StunClienteBuilder, StuntClientEvent, TransportReliability};
/// # use stun_agent::{RttConfig, StunAttributes, StunClienteBuilder, StunClientEvent, TransportReliability};
/// # use stun_rs::methods::BINDING;
/// # use std::time::Instant;
/// # use stun_rs::MessageClass::Indication;
Expand All @@ -585,7 +585,7 @@ impl From<TransportReliability> for StunRttCalcuator {
/// assert_eq!(events.len(), 1);
///
/// let mut iter = events.iter();
/// let StuntClientEvent::StunMessageReceived(msg) = iter
/// let StunClientEvent::StunMessageReceived(msg) = iter
/// .next()
/// .expect("Expected event")
/// else {
Expand Down Expand Up @@ -774,11 +774,11 @@ impl StunClient {
self.transactions.insert(*msg.transaction_id(), transaction);

let mut events = self.transaction_events.init();
events.push(StuntClientEvent::OutputPacket(packet));
events.push(StunClientEvent::OutputPacket(packet));

// Add the most recent timout event if any
if let Some((id, left)) = self.timeouts.next_timeout(instant) {
events.push(StuntClientEvent::RestransmissionTimeOut((id, left)));
events.push(StunClientEvent::RestransmissionTimeOut((id, left)));
}

Ok(*msg.transaction_id())
Expand Down Expand Up @@ -810,7 +810,7 @@ impl StunClient {
})?;

let mut events = self.transaction_events.init();
events.push(StuntClientEvent::OutputPacket(packet));
events.push(StunClientEvent::OutputPacket(packet));

Ok(*msg.transaction_id())
}
Expand Down Expand Up @@ -893,7 +893,7 @@ impl StunClient {
}
None => {
// Notify the user about the received message
events.push(StuntClientEvent::StunMessageReceived(msg));
events.push(StunClientEvent::StunMessageReceived(msg));
}
}

Expand Down Expand Up @@ -921,19 +921,19 @@ impl StunClient {
transaction.instant = None;
self.timeouts.add(instant, rto, transaction_id);
debug!("set timeout {:?} for transaction {:?}", rto, transaction_id);
events.push(StuntClientEvent::OutputPacket(transaction.packet.clone()));
events.push(StunClientEvent::OutputPacket(transaction.packet.clone()));
}
None => {
let protection_violated = self.mechanism.as_mut().is_some_and(|m| {
m.signal_protection_violated_on_timeout(&transaction_id)
});
let event = if protection_violated {
StuntClientEvent::TransactionFailed((
StunClientEvent::TransactionFailed((
transaction_id,
StunTransactionError::ProtectionViolated,
))
} else {
StuntClientEvent::TransactionFailed((
StunClientEvent::TransactionFailed((
transaction_id,
StunTransactionError::TimedOut,
))
Expand All @@ -952,7 +952,7 @@ impl StunClient {

// Add the most recent timout event if any
if let Some((id, left)) = self.timeouts.next_timeout(instant) {
events.push(StuntClientEvent::RestransmissionTimeOut((id, left)));
events.push(StunClientEvent::RestransmissionTimeOut((id, left)));
}
}

Expand All @@ -963,22 +963,22 @@ impl StunClient {
/// Therefore, the user should call this method to retrieve the events as
/// soon as an operation is completed. Otherwise, the events may be lost
/// if a new operation is performed.
pub fn events(&mut self) -> Vec<StuntClientEvent> {
pub fn events(&mut self) -> Vec<StunClientEvent> {
self.transaction_events.events()
}
}

fn process_integrity_error(
error: IntegrityError,
transaction_id: &TransactionId,
) -> Result<Option<StuntClientEvent>, StunAgentError> {
) -> Result<Option<StunClientEvent>, StunAgentError> {
match error {
IntegrityError::ProtectionViolated => Ok(Some(StuntClientEvent::TransactionFailed((
IntegrityError::ProtectionViolated => Ok(Some(StunClientEvent::TransactionFailed((
*transaction_id,
StunTransactionError::ProtectionViolated,
)))),
IntegrityError::Retry => Ok(Some(StuntClientEvent::Retry(*transaction_id))),
IntegrityError::NotRetryable => Ok(Some(StuntClientEvent::TransactionFailed((
IntegrityError::Retry => Ok(Some(StunClientEvent::Retry(*transaction_id))),
IntegrityError::NotRetryable => Ok(Some(StunClientEvent::TransactionFailed((
*transaction_id,
StunTransactionError::DoNotRetry,
)))),
Expand Down
22 changes: 11 additions & 11 deletions stun-agent/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use stun_rs::{StunMessage, TransactionId};

/// Stun client events
#[derive(Debug)]
pub enum StuntClientEvent {
pub enum StunClientEvent {
/// Notification used by the STUN client to send a STUN packet to the server
OutputPacket(StunPacket),
/// This event sets a timeout for a transaction identified by the [`TransactionId`].
Expand Down Expand Up @@ -40,7 +40,7 @@ pub enum StunTransactionError {

#[derive(Debug, Default)]
pub struct TransactionEventHandler {
events: Vec<StuntClientEvent>,
events: Vec<StunClientEvent>,
}

impl TransactionEventHandler {
Expand All @@ -51,19 +51,19 @@ impl TransactionEventHandler {
}
}

pub fn events(&mut self) -> Vec<StuntClientEvent> {
pub fn events(&mut self) -> Vec<StunClientEvent> {
std::mem::take(&mut self.events)
}
}

#[derive(Debug)]
pub struct TransactionEvents<'a> {
handler: &'a mut TransactionEventHandler,
events: Vec<StuntClientEvent>,
events: Vec<StunClientEvent>,
}

impl TransactionEvents<'_> {
pub fn push(&mut self, event: StuntClientEvent) {
pub fn push(&mut self, event: StunClientEvent) {
self.events.push(event);
}
}
Expand Down Expand Up @@ -108,8 +108,8 @@ mod stun_event_tests {
{
let mut events = handler.init();

events.push(StuntClientEvent::Retry(TransactionId::default()));
events.push(StuntClientEvent::Retry(TransactionId::default()));
events.push(StunClientEvent::Retry(TransactionId::default()));
events.push(StunClientEvent::Retry(TransactionId::default()));
}

// Drop the events must commit the events
Expand Down Expand Up @@ -141,16 +141,16 @@ mod stun_event_tests {
let mut handler = TransactionEventHandler::default();
{
let mut events = handler.init();
events.push(StuntClientEvent::Retry(TransactionId::default()));
events.push(StunClientEvent::Retry(TransactionId::default()));
}

{
// Init another transaction when there must be one event
// that was not consumed previously, that event will be dropped
let mut events = handler.init();
events.push(StuntClientEvent::Retry(TransactionId::default()));
events.push(StuntClientEvent::Retry(TransactionId::default()));
events.push(StuntClientEvent::Retry(TransactionId::default()));
events.push(StunClientEvent::Retry(TransactionId::default()));
events.push(StunClientEvent::Retry(TransactionId::default()));
events.push(StunClientEvent::Retry(TransactionId::default()));
}

// There must be only three events
Expand Down
2 changes: 1 addition & 1 deletion stun-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use crate::client::StunClient;
pub use crate::client::StunClienteBuilder;
pub use crate::client::TransportReliability;
pub use crate::events::StunTransactionError;
pub use crate::events::StuntClientEvent;
pub use crate::events::StunClientEvent;
pub use crate::message::StunAttributes;

/// Describes the error that can occur during the STUN agent operation.
Expand Down
Loading
Loading