Struct Peer

Source
pub struct Peer<S: Socket>(/* private fields */);
Expand description

A peer, associated with a Host, which may or may not be connected.

To check on the connectivity of a peer, see Peer::state.

Implementations§

Source§

impl<S: Socket> Peer<S>

Source

pub fn id(&self) -> PeerID

Get the PeerID of this peer.

Source

pub fn ping(&mut self)

Sends a ping request to a peer.

Ping requests factor into the mean round trip time as acquired by Peer::round_trip_time. ENet automatically pings all connected peers at regular intervals, however, this function may be called to ensure more frequent ping requests.

The ping interval can be changed with Self::set_ping_interval.

Source

pub fn send( &mut self, channel_id: u8, packet: &Packet, ) -> Result<(), PeerSendError>

Queues a packet to be sent to this peer on the specified channel.

§Errors

May return any of the PeerSendError variants on failure.

Source

pub fn disconnect(&mut self, data: u32)

Request a disconnection from a peer.

An Event::Disconnect event will be generated by Host::service once the disconnection is complete.

Source

pub fn disconnect_now(&mut self, data: u32)

Force an immediate disconnection from a peer.

No Event::Disconnect event will be generated. The foreign peer is not guaranteed to receive the disconnect notification, and is reset immediately upon return from this function.

Source

pub fn disconnect_later(&mut self, data: u32)

Request a disconnection from a peer, but only after all queued outgoing packets are sent.

An Event::Disconnect event will be generated by Host::service once the disconnection is complete.

Source

pub fn reset(&mut self)

Forcefully disconnects a peer.

The foreign host represented by the peer is not notified of the disconnection and will timeout on its connection to the local host.

Source

pub fn set_timeout(&mut self, limit: u32, minimum: u32, maximum: u32)

Timeout parameters to control how and when a peer will timeout from a failure to acknowledge reliable traffic.

Timeout values use an exponential backoff mechanism, where if a reliable packet is not acknowledge within some multiple of the average RTT plus a variance tolerance, the timeout will be doubled until it reaches a set limit. If the timeout is thus at this limit and reliable packets have been sent but not acknowledged within a certain minimum time period, the peer will be disconnected. Alternatively, if reliable packets have been sent but not acknowledged for a certain maximum time period, the peer will be disconnected regardless of the current timeout limit value.

Source

pub fn set_ping_interval(&mut self, ping_interval: u32)

Sets the interval at which pings will be sent to a peer in milliseconds.

Pings are used both to monitor the liveness of the connection and also to dynamically adjust the throttle during periods of low traffic so that the throttle has reasonable responsiveness during traffic spikes.

See Peer::ping.

Source

pub fn set_throttle( &mut self, interval: u32, acceleration: u32, deceleration: u32, )

Configure the peer’s throttle parameters.

Unreliable packets are dropped by ENet in response to the varying conditions of the Internet connection to the peer. The throttle represents a probability that an unreliable packet should not be dropped and thus sent by ENet to the peer. The lowest mean round trip time from the sending of a reliable packet to the receipt of its acknowledgement is measured over an amount of time specified by the interval parameter in milliseconds. If a measured round trip time happens to be significantly less than the mean round trip time measured over the interval, then the throttle probability is increased to allow more traffic by an amount specified in the acceleration parameter, which is a ratio to the PEER_PACKET_THROTTLE_SCALE constant. If a measured round trip time happens to be significantly greater than the mean round trip time measured over the interval, then the throttle probability is decreased to limit traffic by an amount specified in the deceleration parameter, which is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE When the throttle has a value of PEER_PACKET_THROTTLE_SCALE When no unreliable packets are dropped by ENet, and so 100% of all unreliable packets will be sent. When the throttle has a value of 0, all unreliable packets are dropped by ENet, and so 0% of all unreliable packets will be sent. Intermediate values for the throttle represent intermediate probabilities between 0% and 100% of unreliable packets being sent. The bandwidth limits of the local and foreign hosts are taken into account to determine a sensible limit for the throttle probability above which it should not raise even in the best of conditions.

  • interval - interval, in milliseconds, over which to measure lowest mean RTT; the default value is PEER_PACKET_THROTTLE_INTERVAL
  • acceleration - rate at which to increase the throttle probability as mean RTT declines
  • deceleration - rate at which to decrease the throttle probability as mean RTT increases
Source

pub fn mtu(&self) -> u16

The maximum transmission unit of this peer. See Host::mtu.

Source

pub fn set_mtu(&mut self, mtu: u16) -> Result<(), BadParameter>

Set the maximum transmission unit for this peer. See Host::set_mtu.

§Errors

Returns BadParameter if mtu is greater than PROTOCOL_MAXIMUM_MTU or less than PROTOCOL_MINIMUM_MTU.

Source

pub fn state(&self) -> PeerState

Get the current state of the peer.

Source

pub fn connected(&self) -> bool

Check if this peer’s state is PeerState::Connected.

Source

pub fn channel_count(&self) -> usize

Number of channels allocated for communication with peer.

Source

pub fn incoming_bandwidth(&self) -> u32

Downstream bandwidth of the client in bytes/second.

Source

pub fn outgoing_bandwidth(&self) -> u32

Upstream bandwidth of the client in bytes/second.

Source

pub fn incoming_data_total(&self) -> u32

Total amount of downstream data received.

Source

pub fn outgoing_data_total(&self) -> u32

Total amount of upstream data sent.

Source

pub fn packets_sent(&self) -> u32

Total number of packets sent.

Source

pub fn packets_lost(&self) -> u32

Total number of packets lost.

Source

pub fn packet_loss(&self) -> u32

Mean packet loss of reliable packets as a ratio with respect to the constant PEER_PACKET_LOSS_SCALE.

Source

pub fn packet_loss_variance(&self) -> u32

Variance of the mean packet loss.

Source

pub fn ping_interval(&self) -> Duration

Ping interval. See Peer::set_ping_interval.

Source

pub fn round_trip_time(&self) -> Duration

Mean round trip time (RTT), between sending a reliable packet and receiving its acknowledgement.

Source

pub fn round_trip_time_variance(&self) -> Duration

Round trip time (RTT) variance. See Peer::round_trip_time.

Source

pub fn address(&self) -> Option<S::Address>

Address of the remote peer, or None if this peer has never been connected.

If the peer has disconnected, the previously connected peer’s address will be returned.

This value can be safely unwrapped if a peer was obtained from Event.

Trait Implementations§

Source§

impl<S: Socket> Debug for Peer<S>

Source§

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

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

impl<S: Socket> Send for Peer<S>

Source§

impl<S: Socket> Sync for Peer<S>

Auto Trait Implementations§

§

impl<S> Freeze for Peer<S>

§

impl<S> !RefUnwindSafe for Peer<S>

§

impl<S> Unpin for Peer<S>

§

impl<S> !UnwindSafe for Peer<S>

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