add TcpStream::peek (#291)

This commit is contained in:
Sean McArthur 2018-01-08 09:34:33 -08:00 committed by Alex Crichton
parent b395ccb6d9
commit ce014943ec
3 changed files with 28 additions and 1 deletions

View File

@ -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"

View File

@ -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<usize> {
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

View File

@ -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 {