sheave_server/net/
rtmp.rs

1use std::{
2    io::{
3        Error as IOError,
4        Result as IOResult
5    },
6    net::{
7        SocketAddr,
8        TcpListener as StdListener
9    },
10    task::{
11        Context,
12        Poll
13    }
14};
15use futures::ready;
16use tokio::net::{
17    TcpListener as TokioListener,
18    ToSocketAddrs
19};
20use sheave_core::net::rtmp::*;
21
22/// The default RTMP listener.
23#[derive(Debug)]
24pub struct RtmpListener {
25    tokio_listener: TokioListener
26}
27
28impl RtmpListener {
29    fn new(tokio_listener: TokioListener) -> Self {
30        Self { tokio_listener }
31    }
32
33    /// Opens a RTMP socket for remote host.
34    ///
35    /// When binding succeeded, this wraps tokio's TcpListener into RtmpListener.
36    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.bind)
37    pub async fn bind<A: ToSocketAddrs>(addr: A) -> IOResult<Self> {
38        TokioListener::bind(addr).await.map(Self::new)
39    }
40
41    /// Accepts a new incoming connection from this listener.
42    ///
43    /// When acceptance succeeded, this wraps tokio's TcpListener into RtmpListener.
44    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.accept)
45    pub async fn accept(&self) -> IOResult<(RtmpStream, SocketAddr)> {
46        let (tokio_stream, addr) = self.tokio_listener.accept().await?;
47        Ok((RtmpStream::from(tokio_stream), addr))
48    }
49
50    /// Polls to accept a new incoming connection to this listener.
51    ///
52    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.poll_accept)
53    pub fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<IOResult<(RtmpStream, SocketAddr)>> {
54        let (tokio_stream, addr) = ready!(self.tokio_listener.poll_accept(cx))?;
55        Poll::Ready(Ok((RtmpStream::from(tokio_stream), addr)))
56    }
57
58    /// Creates new RtmpListener from a `std::net::TcpListener`.
59    ///
60    /// When binding succeeded, this wraps tokio's TcpListener into RtmpListener.
61    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.from_std)
62    pub fn from_std(std_listener: StdListener) -> IOResult<Self> {
63        TokioListener::from_std(std_listener).map(Self::new)
64    }
65
66    /// Turns a `sheave_core::net::rtmp::RtmpListener into `std::net::TcpListener`.
67    ///
68    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.into_std)
69    pub fn into_std(self) -> IOResult<StdListener> {
70        self.tokio_listener.into_std()
71    }
72
73    /// Returns the local address that this listener is bound to.
74    ///
75    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.local_addr)
76    pub fn local_addr(&self) -> IOResult<SocketAddr> {
77        self.tokio_listener.local_addr()
78    }
79
80    /// Gets the value of the IP_TTL option for this socket.
81    ///
82    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.ttl)
83    pub fn ttl(&self) -> IOResult<u32> {
84        self.tokio_listener.ttl()
85    }
86
87    /// Sets the value for the IP_TTL option on this socket.
88    ///
89    /// [Read more](https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.set_ttl)
90    pub fn set_ttl(&self, ttl: u32) -> IOResult<()> {
91        self.tokio_listener.set_ttl(ttl)
92    }
93}
94
95impl TryFrom<StdListener> for RtmpListener {
96    type Error = IOError;
97
98    fn try_from(std_listener: StdListener) -> IOResult<Self> {
99        Self::from_std(std_listener)
100    }
101}
102
103#[cfg(unix)]
104mod sys {
105    use std::os::unix::prelude::*;
106    use super::RtmpListener;
107
108    impl AsRawFd for RtmpListener {
109        fn as_raw_fd(&self) -> RawFd {
110            self.tokio_listener.as_raw_fd()
111        }
112    }
113
114    impl AsFd for RtmpListener {
115        fn as_fd(&self) -> BorrowedFd<'_> {
116            self.tokio_listener.as_fd()
117        }
118    }
119}
120
121#[cfg(any(all(doc, docsrs), windows))]
122#[cdg_attr(docsrs, doc(cfg(windows)))]
123mod sys {
124    use tokio::os::windows::io::{
125        AsRawSocket,
126        AsSocket,
127        BorrowedSocket,
128        Rawsocket
129    };
130    use super::RtmpListener;
131
132    impl AsRawSocket for RtmpListener {
133        fn as_raw_socket(&self) -> RawSocket {
134            self.tokio_listener.as_raw_socket()
135        }
136    }
137
138    impl AsSocket for RtmpListener {
139        fn as_socket(&self) -> BorrowedFd<'_> {
140            self.tokio_listener.as_socket()
141        }
142    }
143}
144
145#[cfg(all(tokio_unstable, target_os = "wasi"))]
146#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
147mod sys {
148    use std::os::wasi::prelude::*;
149    use super::RtmpListener;
150
151    impl AsRawFd for RtmpListener {
152        fn as_raw_fd(&self) -> RawFd {
153            self.tokio_listener.as_raw_fd()
154        }
155    }
156
157    impl AsFd for RtmpListener {
158        fn as_fd(&self) -> BorrowedFd<'_> {
159            self.tokio_listener.as_fd()
160        }
161    }
162}