From ce014943eca3b63c249c8b745dc70defbe30ee34 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 8 Jan 2018 09:34:33 -0800 Subject: [PATCH] add TcpStream::peek (#291) --- Cargo.toml | 2 +- src/net/tcp.rs | 18 ++++++++++++++++++ src/reactor/mod.rs | 9 +++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 19fb53985..a31695737 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ appveyor = { repository = "alexcrichton/tokio-core" } [dependencies] bytes = "0.4" log = "0.4" -mio = "0.6.10" +mio = "0.6.12" scoped-tls = "0.1.0" slab = "0.4" iovec = "0.1" diff --git a/src/net/tcp.rs b/src/net/tcp.rs index 7b42fc220..116fcfe8f 100644 --- a/src/net/tcp.rs +++ b/src/net/tcp.rs @@ -364,6 +364,24 @@ impl TcpStream { self.io.get_ref().peer_addr() } + /// Receives data on the socket from the remote address to which it is + /// connected, without removing that data from the queue. On success, + /// returns the number of bytes peeked. + /// + /// Successive calls return the same data. This is accomplished by passing + /// `MSG_PEEK` as a flag to the underlying recv system call. + pub fn peek(&self, buf: &mut [u8]) -> io::Result { + if let Async::NotReady = self.poll_read() { + return Err(io::ErrorKind::WouldBlock.into()) + } + let r = self.io.get_ref().peek(buf); + if is_wouldblock(&r) { + self.io.need_read(); + } + return r + + } + /// Shuts down the read, write, or both halves of this connection. /// /// This function will cause all pending and future I/O on the specified diff --git a/src/reactor/mod.rs b/src/reactor/mod.rs index 140e41d66..a6e05d456 100644 --- a/src/reactor/mod.rs +++ b/src/reactor/mod.rs @@ -289,6 +289,9 @@ impl Core { // Process all the events that came in, dispatching appropriately let mut fired = false; + + // events.len() and .get() deprecated in favor of iter() + #[allow(deprecated)] for i in 0..self.events.len() { let event = self.events.get(i).unwrap(); let token = event.token(); @@ -841,6 +844,8 @@ mod platform { use mio::Ready; use mio::unix::UnixReady; + // aio() deprecated + #[allow(deprecated)] pub fn aio() -> Ready { UnixReady::aio().into() } @@ -857,6 +862,8 @@ mod platform { const ERROR: usize = 1 << 3; const AIO: usize = 1 << 4; + // is_aio() deprecated + #[allow(deprecated)] pub fn ready2usize(ready: Ready) -> usize { let ready = UnixReady::from(ready); let mut bits = 0; @@ -872,6 +879,8 @@ mod platform { bits } + // is_aio() deprecated + #[allow(deprecated)] pub fn usize2ready(bits: usize) -> Ready { let mut ready = UnixReady::from(Ready::empty()); if bits & AIO != 0 {