Struct veloren_network::api::Participant

source ·
pub struct Participant {
    local_pid: Pid,
    remote_pid: Pid,
    a2b_open_stream_s: UnboundedSender<(Prio, Promises, Bandwidth, Sender<Stream>)>,
    b2a_stream_opened_r: UnboundedReceiver<Stream>,
    b2a_event_r: UnboundedReceiver<ParticipantEvent>,
    b2a_bandwidth_stats_r: Receiver<f32>,
    a2s_disconnect_s: Arc<Mutex<Option<UnboundedSender<(Pid, (Duration, Sender<Result<(), ParticipantError>>))>>>>,
}
Expand description

Participants are generated by the Network and represent a connection to a remote Participant. Look at the connect and connected method of Networks on how to generate Participants

Fields§

§local_pid: Pid§remote_pid: Pid§a2b_open_stream_s: UnboundedSender<(Prio, Promises, Bandwidth, Sender<Stream>)>§b2a_stream_opened_r: UnboundedReceiver<Stream>§b2a_event_r: UnboundedReceiver<ParticipantEvent>§b2a_bandwidth_stats_r: Receiver<f32>§a2s_disconnect_s: Arc<Mutex<Option<UnboundedSender<(Pid, (Duration, Sender<Result<(), ParticipantError>>))>>>>

Implementations§

source§

impl Participant

source

pub(crate) fn new( local_pid: Pid, remote_pid: Pid, a2b_open_stream_s: UnboundedSender<(Prio, Promises, Bandwidth, Sender<Stream>)>, b2a_stream_opened_r: UnboundedReceiver<Stream>, b2a_event_r: UnboundedReceiver<ParticipantEvent>, b2a_bandwidth_stats_r: Receiver<f32>, a2s_disconnect_s: UnboundedSender<(Pid, (Duration, Sender<Result<(), ParticipantError>>))>, ) -> Self

source

pub async fn open( &self, prio: u8, promises: Promises, bandwidth: Bandwidth, ) -> Result<Stream, ParticipantError>

Opens a Stream on this Participant with a certain Priority and Promises

§Arguments
  • prio - defines which stream is processed first when limited on bandwidth. See Prio for documentation.
  • promises - use a combination of you preferred Promises, see the link for further documentation. You can combine them, e.g. Promises::ORDERED | Promises::CONSISTENCY The Stream will then guarantee that those promises are met.
  • bandwidth - sets a guaranteed bandwidth which is reserved for this stream. When excess bandwidth is available it will be used. See Bandwidth for details.

A ParticipantError might be thrown if the Participant is already closed. Streams can be created without a answer from the remote side, resulting in very fast creation and closing latency.

§Examples
use tokio::runtime::Runtime;
use veloren_network::{ConnectAddr, ListenAddr, Network, Pid, Promises};

// Create a Network, connect on port 2100 and open a stream
let runtime = Runtime::new().unwrap();
let network = Network::new(Pid::new(), &runtime);
runtime.block_on(async {
    let p1 = network
        .connect(ConnectAddr::Tcp("127.0.0.1:2100".parse().unwrap()))
        .await?;
    let _s1 = p1
        .open(4, Promises::ORDERED | Promises::CONSISTENCY, 1000)
        .await?;
    drop(network);
})
source

pub async fn opened(&mut self) -> Result<Stream, ParticipantError>

Use this method to handle Streams opened from remote site, like the connected method of Network. This is the associated method to open. It’s guaranteed that the order of open and opened is equal. The nth Streams on one side will represent the nth on the other side. A ParticipantError might be thrown if the Participant is already closed.

§Examples
use tokio::runtime::Runtime;
use veloren_network::{Network, Pid, ListenAddr, ConnectAddr, Promises};

// Create a Network, connect on port 2110 and wait for the other side to open a stream
// Note: It's quite unusual to actively connect, but then wait on a stream to be connected, usually the Application taking initiative want's to also create the first Stream.
let runtime = Runtime::new().unwrap();
let mut network = Network::new(Pid::new(), &runtime);
runtime.block_on(async {
    let mut p1 = network.connect(ConnectAddr::Tcp("127.0.0.1:2110".parse().unwrap())).await?;
    let _s1 = p1.opened().await?;
    drop(network);
})
source

pub async fn disconnect(self) -> Result<(), ParticipantError>

disconnecting a Participant in a async way. Use this rather than Participant::Drop if you want to close multiple Participants.

This function will wait for all Streams to properly close, including all messages to be send before closing. If an error occurs with one of the messages. Except if the remote side already dropped the Participant simultaneously, then messages won’t be send

There is NO disconnected function in Participant, if a Participant is no longer reachable (e.g. as the network cable was unplugged) the Participant will fail all action, but needs to be manually disconnected, using this function.

§Examples
use tokio::runtime::Runtime;
use veloren_network::{Network, Pid, ListenAddr, ConnectAddr};

// Create a Network, listen on port `2030` TCP and opens returns their Pid and close connection.
let runtime = Runtime::new().unwrap();
let mut network = Network::new(Pid::new(), &runtime);
let err = runtime.block_on(async {
    network
        .listen(ListenAddr::Tcp("127.0.0.1:2030".parse().unwrap()))
        .await?;
    while let Ok(participant) = network.connected().await {
        println!("Participant connected: {}", participant.remote_pid());
        participant.disconnect().await?;
    }
});
drop(network);
source

pub async fn fetch_event( &mut self, ) -> Result<ParticipantEvent, ParticipantError>

Use this method to query ParticipantEvent. Those are internal events from the network crate that will get reported to the frontend. E.g. Creation and Deletion of Channels.

Make sure to call this function from time to time to not let events stack up endlessly and create a memory leak.

§Examples
use tokio::runtime::Runtime;
use veloren_network::{Network, Pid, ListenAddr, ConnectAddr, Promises, ParticipantEvent};

// Create a Network, connect on port 2040 and wait for the other side to open a stream
// Note: It's quite unusual to actively connect, but then wait on a stream to be connected, usually the Application taking initiative want's to also create the first Stream.
let runtime = Runtime::new().unwrap();
let mut network = Network::new(Pid::new(), &runtime);
runtime.block_on(async {
    let mut p1 = network.connect(ConnectAddr::Tcp("127.0.0.1:2040".parse().unwrap())).await?;
    let event = p1.fetch_event().await?;
    drop(network);
})
source

pub fn try_fetch_event( &mut self, ) -> Result<Option<ParticipantEvent>, ParticipantError>

use try_fetch_event to check for a ParticipantEvent . This function does not block and returns immediately. It’s intended for use in non-async context only. Other then that, the same rules apply than for fetch_event.

source

pub fn bandwidth(&self) -> f32

Returns the current approximation on the maximum bandwidth available. This WILL fluctuate based on the amount/size of send messages.

source

pub fn remote_pid(&self) -> Pid

Returns the remote Pid

Trait Implementations§

source§

impl Debug for Participant

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Participant

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl PartialEq for Participant

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

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.

§

impl<T> Instrument for T

§

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

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

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
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

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>,

§

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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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