1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use ;
use crate::;
/// 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
///
/// ```rust
/// # use twitch_message::messages::Message;
/// # use twitch_message::encode::Encode as _;
/// # fn read_message() -> Message<'static> { twitch_message::parse(":tmi.twitch.tv PING :1234567890\r\n").unwrap().message }
/// # let mut io_sink = vec![];
/// 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)?;
/// }
/// # Ok::<(),Box<dyn std::error::Error>>(())
/// ```