pub struct Context { /* private fields */ }Expand description
Owns and manages the set of active subscriptions.
Implementations§
Source§impl Context
impl Context
Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Returns the number of active subscriptions currently stored in the context.
Sourcepub fn contains_subscription(&self, id: SubscriptionId) -> bool
pub fn contains_subscription(&self, id: SubscriptionId) -> bool
Returns true if a subscription with the given ID exists.
Sourcepub fn get_subscription(&self, id: SubscriptionId) -> Option<&Subscription>
pub fn get_subscription(&self, id: SubscriptionId) -> Option<&Subscription>
Returns a read-only reference to the subscription with the given ID, if it exists.
Sourcepub fn get_subscription_mut(
&mut self,
id: SubscriptionId,
) -> Option<&mut Subscription>
pub fn get_subscription_mut( &mut self, id: SubscriptionId, ) -> Option<&mut Subscription>
Returns a mutable reference to the subscription with the given ID, if it exists.
Sourcepub fn add_subscription(
&mut self,
config: SubscriptionConfig,
) -> Result<SubscriptionId, McrxError>
pub fn add_subscription( &mut self, config: SubscriptionConfig, ) -> Result<SubscriptionId, McrxError>
Adds a new subscription to the context.
The configuration is validated before insertion. If an identical subscription already exists, an error is returned instead of creating a duplicate.
This function creates and binds the underlying socket, but does not join the
multicast group yet. Call join_subscription() to activate multicast reception.
Sourcepub fn add_subscription_with_socket(
&mut self,
config: SubscriptionConfig,
socket: Socket,
) -> Result<SubscriptionId, McrxError>
pub fn add_subscription_with_socket( &mut self, config: SubscriptionConfig, socket: Socket, ) -> Result<SubscriptionId, McrxError>
Adds a new subscription using a caller-provided socket.
The socket must already be bound to the destination port from config.
This method preserves the existing lifecycle model: the context will still
perform multicast join/leave operations later via join_subscription() and
leave_subscription().
The supplied socket is switched to non-blocking mode so the receive APIs keep their usual non-blocking behavior.
Sourcepub fn remove_subscription(&mut self, id: SubscriptionId) -> bool
pub fn remove_subscription(&mut self, id: SubscriptionId) -> bool
Removes the subscription with the given ID.
Returns true if a subscription was removed and false if no matching subscription was found.
This uses swap_remove, so subscription order is not preserved.
Sourcepub fn take_subscription(&mut self, id: SubscriptionId) -> Option<Subscription>
pub fn take_subscription(&mut self, id: SubscriptionId) -> Option<Subscription>
Removes the subscription with the given ID and returns it to the caller.
This preserves the current socket ownership and lifecycle state, which is useful when moving a subscription into an external event loop or runtime.
This uses swap_remove, so subscription order is not preserved.
Sourcepub fn join_subscription(&mut self, id: SubscriptionId) -> Result<(), McrxError>
pub fn join_subscription(&mut self, id: SubscriptionId) -> Result<(), McrxError>
Joins the multicast group for the given subscription.
Sourcepub fn leave_subscription(
&mut self,
id: SubscriptionId,
) -> Result<(), McrxError>
pub fn leave_subscription( &mut self, id: SubscriptionId, ) -> Result<(), McrxError>
Leaves the multicast group for the given subscription while keeping the socket bound.
Sourcepub fn subscriptions(&self) -> &[Subscription]
pub fn subscriptions(&self) -> &[Subscription]
Returns a read-only slice of all subscriptions currently stored in the context.
Sourcepub fn subscriptions_mut(&mut self) -> &mut [Subscription]
pub fn subscriptions_mut(&mut self) -> &mut [Subscription]
Returns a mutable slice of all subscriptions currently stored in the context.
Sourcepub fn try_recv_any(&mut self) -> Result<Option<Packet>, McrxError>
pub fn try_recv_any(&mut self) -> Result<Option<Packet>, McrxError>
Attempts to receive a single packet from any joined subscription without blocking.
Subscriptions are scanned using round-robin style fairness so that repeated calls do not always favor the first subscription.
Returns the first available packet, if any joined subscription currently has one ready to be read.
Sourcepub fn try_recv_any_with_metadata(
&mut self,
) -> Result<Option<PacketWithMetadata>, McrxError>
pub fn try_recv_any_with_metadata( &mut self, ) -> Result<Option<PacketWithMetadata>, McrxError>
Attempts to receive a single packet with richer receive metadata from any joined subscription without blocking.
This uses the same round-robin fairness logic as try_recv_any().
Sourcepub fn try_recv_batch_into(
&mut self,
out: &mut Vec<Packet>,
max_packets: usize,
) -> Result<usize, McrxError>
pub fn try_recv_batch_into( &mut self, out: &mut Vec<Packet>, max_packets: usize, ) -> Result<usize, McrxError>
Attempts to receive up to max_packets packets from any subscriptions without blocking.
This method repeatedly calls try_recv_any() using the same round-robin fairness logic
and pushes received packets into the provided out vector.
Returns the number of packets that were added to out.
Behavior:
- Stops early if no more packets are available
- Does not block or wait for new packets
- Preserves fairness across subscriptions
Sourcepub fn try_recv_batch_with_metadata_into(
&mut self,
out: &mut Vec<PacketWithMetadata>,
max_packets: usize,
) -> Result<usize, McrxError>
pub fn try_recv_batch_with_metadata_into( &mut self, out: &mut Vec<PacketWithMetadata>, max_packets: usize, ) -> Result<usize, McrxError>
Attempts to receive up to max_packets packets with richer receive
metadata from any subscriptions without blocking.
Sourcepub fn try_recv_all_into(
&mut self,
out: &mut Vec<Packet>,
) -> Result<usize, McrxError>
pub fn try_recv_all_into( &mut self, out: &mut Vec<Packet>, ) -> Result<usize, McrxError>
Attempts to receive all currently available packets without blocking.
This is a convenience wrapper around try_recv_batch_into that continues
draining until no more packets are available.
Note: this only drains packets that are currently available without blocking.
It may result in unbounded growth of out if a large number of packets are queued,
so callers should ensure capacity if needed.
Sourcepub fn try_recv_all_with_metadata_into(
&mut self,
out: &mut Vec<PacketWithMetadata>,
) -> Result<usize, McrxError>
pub fn try_recv_all_with_metadata_into( &mut self, out: &mut Vec<PacketWithMetadata>, ) -> Result<usize, McrxError>
Attempts to receive all currently available packets with richer receive metadata without blocking.