Skip to content

Commit ed494d5

Browse files
committed
Refactor Socket::shutdown
1 parent f43253f commit ed494d5

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl Socket {
265265
/// This function will cause all pending and future I/O on the specified
266266
/// portions to return immediately with an appropriate value.
267267
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
268-
self.inner().shutdown(how)
268+
sys::shutdown(self.inner, how)
269269
}
270270

271271
/// Receives data on the socket from the remote address to which it is

src/sys/unix.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ pub(crate) fn set_nonblocking(fd: SysSocket, nonblocking: bool) -> io::Result<()
365365
}
366366
}
367367

368+
pub(crate) fn shutdown(fd: SysSocket, how: Shutdown) -> io::Result<()> {
369+
let how = match how {
370+
Shutdown::Write => libc::SHUT_WR,
371+
Shutdown::Read => libc::SHUT_RD,
372+
Shutdown::Both => libc::SHUT_RDWR,
373+
};
374+
syscall!(shutdown(fd, how)).map(|_| ())
375+
}
376+
368377
/// Unix only API.
369378
impl crate::Socket {
370379
/// Accept a new incoming connection from this listener.
@@ -492,16 +501,6 @@ pub struct Socket {
492501
}
493502

494503
impl Socket {
495-
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
496-
let how = match how {
497-
Shutdown::Write => libc::SHUT_WR,
498-
Shutdown::Read => libc::SHUT_RD,
499-
Shutdown::Both => libc::SHUT_RDWR,
500-
};
501-
syscall!(shutdown(self.fd, how))?;
502-
Ok(())
503-
}
504-
505504
pub fn recv(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
506505
let n = syscall!(recv(
507506
self.fd,

src/sys/windows.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@ use winapi::um::processthreadsapi::GetCurrentProcessId;
3232
#[cfg(feature = "all")]
3333
use winapi::um::winbase;
3434
use winapi::um::winbase::INFINITE;
35-
use winapi::um::winsock2::{self as sock, u_long};
35+
use winapi::um::winsock2::{self as sock, u_long, SD_BOTH, SD_RECEIVE, SD_SEND};
3636

3737
use crate::{RecvFlags, SockAddr, Type};
3838

3939
const MSG_PEEK: c_int = 0x2;
40-
const SD_BOTH: c_int = 2;
41-
const SD_RECEIVE: c_int = 0;
42-
const SD_SEND: c_int = 1;
4340
const SIO_KEEPALIVE_VALS: DWORD = 0x98000004;
4441

4542
pub use winapi::ctypes::c_int;
@@ -266,6 +263,15 @@ pub(crate) fn set_nonblocking(socket: SysSocket, nonblocking: bool) -> io::Resul
266263
ioctlsocket(socket, sock::FIONBIO, &mut nonblocking)
267264
}
268265

266+
pub(crate) fn shutdown(socket: SysSocket, how: Shutdown) -> io::Result<()> {
267+
let how = match how {
268+
Shutdown::Write => SD_SEND,
269+
Shutdown::Read => SD_RECEIVE,
270+
Shutdown::Both => SD_BOTH,
271+
};
272+
syscall!(shutdown(socket, how), PartialEq::eq, sock::SOCKET_ERROR).map(|_| ())
273+
}
274+
269275
/// Caller must ensure `T` is the correct type for `opt` and `val`.
270276
unsafe fn getsockopt<T>(socket: SysSocket, opt: c_int, val: c_int) -> io::Result<T> {
271277
let mut payload: MaybeUninit<T> = MaybeUninit::uninit();
@@ -315,19 +321,6 @@ pub struct Socket {
315321
}
316322

317323
impl Socket {
318-
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
319-
let how = match how {
320-
Shutdown::Write => SD_SEND,
321-
Shutdown::Read => SD_RECEIVE,
322-
Shutdown::Both => SD_BOTH,
323-
};
324-
if unsafe { sock::shutdown(self.socket, how) == 0 } {
325-
Ok(())
326-
} else {
327-
Err(last_error())
328-
}
329-
}
330-
331324
pub fn recv(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
332325
unsafe {
333326
let n = {

0 commit comments

Comments
 (0)