Struct PingTracker

Source
pub struct PingTracker { /* private fields */ }
Available on crate feature ping only.
Expand description

A simple type to track PINGs and if you should PONG

This requires the ping feature to be enabled

If either sync or parking_lot features are also enabled, then this type is safe to send to other threads

use twitch_message::PingTracker;
// create a new tracker, the `threshold` is used to determine when a connection is dead/stale.
let pt = PingTracker::new(std::time::Duration::from_secs(10 * 60));

// in some loop
// if its been a while (such as if you have a way to keep track of time)
if pt.probably_timed_out() {
    // we should reconnect
    return Err("timed out".into());
}

// this might block for a while
let msg = read_message();
// update the tracker
pt.update(&msg);

// check to see if you should reply.
// this returns a message you can write to your sink
if let Some(pong) = pt.should_pong() {
    io_sink.encode_msg(pong)?;
}

Implementations§

Source§

impl PingTracker

Source

pub const fn new(threshold: Duration) -> Self

Create a new PingTracker with a ‘timeout’ duration

Source

pub const fn threshold(&self) -> Duration

Get the ‘timeout’ duration

Source

pub fn update(&self, msg: &Message<'_>)

Update the tracker with this message

Source

pub fn should_pong(&self) -> Option<Pong<'static>>

Determines whether you should PONG

This returns the message you should encode

Source

pub fn probably_timed_out(&self) -> bool

Determines if the timeout threshold has been reached

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.

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.