sheave_core/net.rs
1pub mod rtmp;
2mod await_until_receiving;
3mod try_read_after;
4
5use std::time::Duration;
6use tokio::io::AsyncRead;
7use self::{
8 await_until_receiving::*,
9 try_read_after::*
10};
11
12/// Reader extensions for RTMP.
13///
14/// In almost cases, the network communication is enough just to await until receiving some message.
15/// But RTMP clients are required to be able to read messages both `Acknowledgement` and stream closing from servers, and these messages aren't necessarily always sent.
16/// In this moment, clients will be stayed their processings if await receiving forever.
17///
18/// For solving above timing mismatches, to prepare several choices to receive chunks are required.
19///
20/// This trait provides several flexibility to read chunk by preparing following methods.
21///
22/// * [`await_until_receiving`]: The default of receiving behavior.
23/// * [`try_read_after`]: Currently for clients.
24///
25/// [`await_until_receiving`]: RtmpReadExt::await_until_receiving
26/// [`try_read_after`]: RtmpReadExt::try_read_after
27pub trait RtmpReadExt: AsyncRead {
28 /// Makes a stream awaiting until receiving some message.
29 ///
30 /// # Examples
31 ///
32 /// ```no_run
33 /// use tokio::io::{
34 /// AsyncRead,
35 /// AsyncReadExt,
36 /// AsyncWrite,
37 /// AsyncWriteExt
38 /// };
39 /// use sheave_core::{
40 /// handlers::VecStream,
41 /// net::RtmpReadExt
42 /// };
43 ///
44 /// #[tokio::main]
45 /// async fn main() {
46 /// let mut stream = VecStream::default();
47 /// stream.write_u8(1).await.unwrap();
48 /// assert!(stream.await_until_receiving().read_u8().await.is_ok())
49 /// }
50 /// ```
51 fn await_until_receiving<'a>(&'a mut self) -> AwaitUntilReceiving<'a, Self>
52 where Self: Sized + Unpin
53 {
54 await_until_receiving(self)
55 }
56
57 /// Makes a stream sleeping during specified duration.
58 ///
59 /// # Examples
60 ///
61 /// ```no_run
62 /// use std::time::Duration;
63 /// use tokio::io::{
64 /// AsyncRead,
65 /// AsyncReadExt,
66 /// AsyncWrite,
67 /// AsyncWriteExt
68 /// };
69 /// use sheave_core::{
70 /// handlers::VecStream,
71 /// net::RtmpReadExt
72 /// };
73 ///
74 /// #[tokio::main]
75 /// async fn main() {
76 /// let mut stream = VecStream::default();
77 /// stream.write_u8(1).await.unwrap();
78 /// assert!(stream.try_read_after(Duration::from_secs(1)).read_u8().await.is_ok())
79 /// }
80 /// ```
81 fn try_read_after<'a>(&'a mut self, await_duration: Duration) -> TryReadAfter<'a, Self>
82 where Self: Sized + Unpin
83 {
84 try_read_after(self, await_duration)
85 }
86}
87
88impl<R: AsyncRead> RtmpReadExt for R {}