Skip to content

Commit 70040ea

Browse files
committed
Refactor Socket::peek
1 parent 1b8067d commit 70040ea

File tree

3 files changed

+14
-41
lines changed

3 files changed

+14
-41
lines changed

src/socket.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ use std::net::{self, Ipv4Addr, Ipv6Addr, Shutdown};
1515
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
1616
use std::time::Duration;
1717

18-
#[cfg(all(unix, feature = "all", not(target_os = "redox")))]
19-
use libc::MSG_OOB;
20-
#[cfg(all(windows, feature = "all"))]
21-
use winapi::um::winsock2::MSG_OOB;
22-
2318
use crate::sys;
2419
#[cfg(not(target_os = "redox"))]
2520
use crate::RecvFlags;
@@ -303,7 +298,7 @@ impl Socket {
303298
/// [`out_of_band_inline`]: Socket::out_of_band_inline
304299
#[cfg(all(feature = "all", not(target_os = "redox")))]
305300
pub fn recv_out_of_band(&self, buf: &mut [u8]) -> io::Result<usize> {
306-
self.recv_with_flags(buf, MSG_OOB)
301+
self.recv_with_flags(buf, sys::MSG_OOB)
307302
}
308303

309304
/// Identical to [`recv`] but allows for specification of arbitrary flags to
@@ -351,7 +346,7 @@ impl Socket {
351346
/// Successive calls return the same data. This is accomplished by passing
352347
/// `MSG_PEEK` as a flag to the underlying `recv` system call.
353348
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
354-
self.inner().peek(buf)
349+
self.recv_with_flags(buf, sys::MSG_PEEK)
355350
}
356351

357352
/// Receives data from the socket. On success, returns the number of bytes
@@ -433,7 +428,7 @@ impl Socket {
433428
/// [`out_of_band_inline`]: #method.out_of_band_inline
434429
#[cfg(all(feature = "all", not(target_os = "redox")))]
435430
pub fn send_out_of_band(&self, buf: &[u8]) -> io::Result<usize> {
436-
self.inner().send(buf, MSG_OOB)
431+
self.inner().send(buf, sys::MSG_OOB)
437432
}
438433

439434
/// Sends data on the socket to the given address. On success, returns the

src/sys/unix.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ pub(crate) use libc::{IPPROTO_ICMP, IPPROTO_ICMPV6, IPPROTO_TCP, IPPROTO_UDP};
4242
pub(crate) use libc::{
4343
sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t,
4444
};
45+
// Used in `RecvFlags`.
46+
#[cfg(not(target_os = "redox"))]
47+
pub(crate) use libc::MSG_TRUNC;
48+
// Used in `Socket`.
49+
#[cfg(all(unix, feature = "all", not(target_os = "redox")))]
50+
pub(crate) use libc::MSG_OOB;
51+
pub(crate) use libc::MSG_PEEK;
4552

4653
cfg_if::cfg_if! {
4754
if #[cfg(any(target_os = "dragonfly", target_os = "freebsd",
@@ -82,10 +89,6 @@ macro_rules! syscall {
8289
}};
8390
}
8491

85-
// Re-export message flags for the RecvFlags struct.
86-
#[cfg(not(target_os = "redox"))]
87-
pub(crate) use libc::MSG_TRUNC;
88-
8992
/// Maximum size of a buffer passed to system call like `recv` and `send`.
9093
#[cfg(not(target_vendor = "apple"))]
9194
const MAX_BUF_LEN: usize = <ssize_t>::max_value() as usize;
@@ -550,16 +553,6 @@ pub struct Socket {
550553
}
551554

552555
impl Socket {
553-
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
554-
let n = syscall!(recv(
555-
self.fd,
556-
buf.as_mut_ptr() as *mut c_void,
557-
cmp::min(buf.len(), max_len()),
558-
libc::MSG_PEEK,
559-
))?;
560-
Ok(n as usize)
561-
}
562-
563556
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SockAddr)> {
564557
self.recv_from(buf, libc::MSG_PEEK)
565558
}

src/sys/windows.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use winapi::um::winsock2::{self as sock, u_long, SD_BOTH, SD_RECEIVE, SD_SEND};
3333

3434
use crate::{RecvFlags, SockAddr, Type};
3535

36-
const MSG_PEEK: c_int = 0x2;
3736
const SIO_KEEPALIVE_VALS: DWORD = 0x98000004;
3837

3938
pub use winapi::ctypes::c_int;
@@ -62,6 +61,10 @@ pub(crate) use winapi::shared::ws2def::{
6261
};
6362
pub(crate) use winapi::shared::ws2ipdef::SOCKADDR_IN6_LH as sockaddr_in6;
6463
pub(crate) use winapi::um::ws2tcpip::socklen_t;
64+
// Used in `Socket`.
65+
#[cfg(all(windows, feature = "all"))]
66+
pub(crate) use winapi::um::winsock2::MSG_OOB;
67+
pub(crate) use winapi::um::winsock2::MSG_PEEK;
6568

6669
/// Maximum size of a buffer passed to system call like `recv` and `send`.
6770
const MAX_BUF_LEN: usize = <c_int>::max_value() as usize;
@@ -375,24 +378,6 @@ pub struct Socket {
375378
}
376379

377380
impl Socket {
378-
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
379-
unsafe {
380-
let n = {
381-
sock::recv(
382-
self.socket,
383-
buf.as_mut_ptr() as *mut c_char,
384-
clamp(buf.len()),
385-
MSG_PEEK,
386-
)
387-
};
388-
match n {
389-
sock::SOCKET_ERROR if sock::WSAGetLastError() == sock::WSAESHUTDOWN as i32 => Ok(0),
390-
sock::SOCKET_ERROR => Err(last_error()),
391-
n => Ok(n as usize),
392-
}
393-
}
394-
}
395-
396381
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SockAddr)> {
397382
self.recv_from(buf, MSG_PEEK)
398383
}

0 commit comments

Comments
 (0)