diff options
author | Carl Lerche <me@carllerche.com> | 2020-10-08 12:12:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-08 12:12:56 -0700 |
commit | 066965cd59d01fd9d999152e32169a24dfe434fa (patch) | |
tree | eef03ca071b8d9f285954a1f98fd85e3e188c98b /tokio/src/net | |
parent | 6259893094ebcdfecb107fcf3682eaad1bd1903b (diff) |
net: use &self with TcpListener::accept (#2919)
Uses the infrastructure added by #2828 to enable switching
`TcpListener::accept` to use `&self`.
This also switches `poll_accept` to use `&self`. While doing introduces
a hazard, `poll_*` style functions are considered low-level. Most users
will use the `async fn` variants which are more misuse-resistant.
TcpListener::incoming() is temporarily removed as it has the same
problem as `TcpSocket::by_ref()` and will be implemented later.
Diffstat (limited to 'tokio/src/net')
-rw-r--r-- | tokio/src/net/tcp/incoming.rs | 42 | ||||
-rw-r--r-- | tokio/src/net/tcp/listener.rs | 75 | ||||
-rw-r--r-- | tokio/src/net/tcp/mod.rs | 4 | ||||
-rw-r--r-- | tokio/src/net/tcp/stream.rs | 4 |
4 files changed, 21 insertions, 104 deletions
diff --git a/tokio/src/net/tcp/incoming.rs b/tokio/src/net/tcp/incoming.rs deleted file mode 100644 index 062be1e9..00000000 --- a/tokio/src/net/tcp/incoming.rs +++ /dev/null @@ -1,42 +0,0 @@ -use crate::net::tcp::{TcpListener, TcpStream}; - -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -/// Stream returned by the `TcpListener::incoming` function representing the -/// stream of sockets received from a listener. -#[must_use = "streams do nothing unless polled"] -#[derive(Debug)] -pub struct Incoming<'a> { - inner: &'a mut TcpListener, -} - -impl Incoming<'_> { - pub(crate) fn new(listener: &mut TcpListener) -> Incoming<'_> { - Incoming { inner: listener } - } - - /// Attempts to poll `TcpStream` by polling inner `TcpListener` to accept - /// connection. - /// - /// If `TcpListener` isn't ready yet, `Poll::Pending` is returned and - /// current task will be notified by a waker. - pub fn poll_accept( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll<io::Result<TcpStream>> { - let (socket, _) = ready!(self.inner.poll_accept(cx))?; - Poll::Ready(Ok(socket)) - } -} - -#[cfg(feature = "stream")] -impl crate::stream::Stream for Incoming<'_> { - type Item = io::Result<TcpStream>; - - fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { - let (socket, _) = ready!(self.inner.poll_accept(cx))?; - Poll::Ready(Some(Ok(socket))) - } -} diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index 133852d2..98c8961e 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -1,6 +1,5 @@ -use crate::future::poll_fn; use crate::io::PollEvented; -use crate::net::tcp::{Incoming, TcpStream}; +use crate::net::tcp::TcpStream; use crate::net::{to_socket_addrs, ToSocketAddrs}; use std::convert::TryFrom; @@ -40,7 +39,7 @@ cfg_tcp! { /// /// #[tokio::main] /// async fn main() -> io::Result<()> { - /// let mut listener = TcpListener::bind("127.0.0.1:8080").await?; + /// let listener = TcpListener::bind("127.0.0.1:8080").await?; /// /// loop { /// let (socket, _) = listener.accept().await?; @@ -171,7 +170,7 @@ impl TcpListener { /// /// #[tokio::main] /// async fn main() -> io::Result<()> { - /// let mut listener = TcpListener::bind("127.0.0.1:8080").await?; + /// let listener = TcpListener::bind("127.0.0.1:8080").await?; /// /// match listener.accept().await { /// Ok((_socket, addr)) => println!("new client: {:?}", addr), @@ -181,18 +180,25 @@ impl TcpListener { /// Ok(()) /// } /// ``` - pub async fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> { - poll_fn(|cx| self.poll_accept(cx)).await + pub async fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { + let (mio, addr) = self + .io + .async_io(mio::Interest::READABLE, |sock| sock.accept()) + .await?; + + let stream = TcpStream::new(mio)?; + Ok((stream, addr)) } /// Polls to accept a new incoming connection to this listener. /// - /// If there is no connection to accept, `Poll::Pending` is returned and - /// the current task will be notified by a waker. - pub fn poll_accept( - &mut self, - cx: &mut Context<'_>, - ) -> Poll<io::Result<(TcpStream, SocketAddr)>> { + /// If there is no connection to accept, `Poll::Pending` is returned and the + /// current task will be notified by a waker. + /// + /// When ready, the most recent task that called `poll_accept` is notified. + /// The caller is responsble to ensure that `poll_accept` is called from a + /// single task. Failing to do this could result in tasks hanging. + pub fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<io::Result<(TcpStream, SocketAddr)>> { loop { let ev = ready!(self.io.poll_read_ready(cx))?; @@ -293,46 +299,6 @@ impl TcpListener { self.io.get_ref().local_addr() } - /// Returns a stream over the connections being received on this listener. - /// - /// Note that `TcpListener` also directly implements `Stream`. - /// - /// The returned stream will never return `None` and will also not yield the - /// peer's `SocketAddr` structure. Iterating over it is equivalent to - /// calling accept in a loop. - /// - /// # Errors - /// - /// Note that accepting a connection can lead to various errors and not all - /// of them are necessarily fatal ‒ for example having too many open file - /// descriptors or the other side closing the connection while it waits in - /// an accept queue. These would terminate the stream if not handled in any - /// way. - /// - /// # Examples - /// - /// ```no_run - /// use tokio::{net::TcpListener, stream::StreamExt}; - /// - /// #[tokio::main] - /// async fn main() { - /// let mut listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); - /// let mut incoming = listener.incoming(); - /// - /// while let Some(stream) = incoming.next().await { - /// match stream { - /// Ok(stream) => { - /// println!("new client!"); - /// } - /// Err(e) => { /* connection failed */ } - /// } - /// } - /// } - /// ``` - pub fn incoming(&mut self) -> Incoming<'_> { - Incoming::new(self) - } - /// Gets the value of the `IP_TTL` option for this socket. /// /// For more information about this option, see [`set_ttl`]. @@ -390,10 +356,7 @@ impl TcpListener { impl crate::stream::Stream for TcpListener { type Item = io::Result<TcpStream>; - fn poll_next( - mut self: std::pin::Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll<Option<Self::Item>> { + fn poll_next(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { let (socket, _) = ready!(self.poll_accept(cx))?; Poll::Ready(Some(Ok(socket))) } diff --git a/tokio/src/net/tcp/mod.rs b/tokio/src/net/tcp/mod.rs index c27038f9..7f0f6d91 100644 --- a/tokio/src/net/tcp/mod.rs +++ b/tokio/src/net/tcp/mod.rs @@ -1,10 +1,6 @@ //! TCP utility types pub(crate) mod listener; -pub(crate) use listener::TcpListener; - -mod incoming; -pub use incoming::Incoming; pub(crate) mod socket; diff --git a/tokio/src/net/tcp/stream.rs b/tokio/src/net/tcp/stream.rs index 4349ea80..3f9d6670 100644 --- a/tokio/src/net/tcp/stream.rs +++ b/tokio/src/net/tcp/stream.rs @@ -22,8 +22,8 @@ cfg_tcp! { /// traits. Examples import these traits through [the prelude]. /// /// [`connect`]: method@TcpStream::connect - /// [accepting]: method@super::TcpListener::accept - /// [listener]: struct@super::TcpListener + /// [accepting]: method@crate::net::TcpListener::accept + /// [listener]: struct@crate::net::TcpListener /// [`AsyncReadExt`]: trait@crate::io::AsyncReadExt /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt /// [the prelude]: crate::prelude |