-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specifying custom interest for TcpStream #5796
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,13 +158,58 @@ impl TcpListener { | |
/// } | ||
/// ``` | ||
pub async fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { | ||
self.accept_with_interest(Default::default()).await | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if this is a general practice, but I'd prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is also assuming that the |
||
} | ||
|
||
/// Accepts a new incoming connection from this listener with custom | ||
/// interest registration. | ||
/// | ||
/// This function will yield once a new TCP connection is established. When | ||
/// established, the corresponding [`TcpStream`] and the remote peer's | ||
/// address will be returned. | ||
/// | ||
/// # Cancel safety | ||
/// | ||
/// This method is cancel safe. If the method is used as the event in a | ||
/// [`tokio::select!`](crate::select) statement and some other branch | ||
/// completes first, then it is guaranteed that no new connections were | ||
/// accepted by this method. | ||
/// | ||
/// [`TcpStream`]: struct@crate::net::TcpStream | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use tokio::{io::Interest, net::TcpListener}; | ||
/// | ||
/// use std::io; | ||
/// | ||
/// #[tokio::main] | ||
/// async fn main() -> io::Result<()> { | ||
/// let listener = TcpListener::bind("127.0.0.1:8080").await?; | ||
/// | ||
/// match listener | ||
/// .accept_with_interest(Interest::PRIORITY.add(Default::default())) | ||
/// .await | ||
/// { | ||
/// Ok((_socket, addr)) => println!("new client: {:?}", addr), | ||
/// Err(e) => println!("couldn't get client: {:?}", e), | ||
/// } | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
pub async fn accept_with_interest( | ||
&self, | ||
interest: Interest, | ||
) -> io::Result<(TcpStream, SocketAddr)> { | ||
let (mio, addr) = self | ||
.io | ||
.registration() | ||
.async_io(Interest::READABLE, || self.io.accept()) | ||
.await?; | ||
|
||
let stream = TcpStream::new(mio)?; | ||
let stream = TcpStream::new_with_interest(mio, interest)?; | ||
Ok((stream, addr)) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this truly a reasonable default for
Interest
? I thinkInterest::READABLE | Interest::WRITABLE
written out in full is always clearer.