Skip to content

Commit 2497129

Browse files
committed
Refactor Socket::send
1 parent 1f4d0d1 commit 2497129

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

src/socket.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,15 @@ impl Socket {
411411
///
412412
/// On success returns the number of bytes that were sent.
413413
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
414-
self.inner().send(buf, 0)
414+
self.send_with_flags(buf, 0)
415415
}
416416

417417
/// Identical to [`send`] but allows for specification of arbitrary flags to the underlying
418418
/// `send` call.
419419
///
420420
/// [`send`]: #method.send
421421
pub fn send_with_flags(&self, buf: &[u8], flags: i32) -> io::Result<usize> {
422-
self.inner().send(buf, flags)
422+
sys::send(self.inner, buf, flags)
423423
}
424424

425425
/// Identical to [`send_with_flags`] but writes from a slice of buffers.
@@ -439,7 +439,7 @@ impl Socket {
439439
/// [`out_of_band_inline`]: #method.out_of_band_inline
440440
#[cfg(all(feature = "all", not(target_os = "redox")))]
441441
pub fn send_out_of_band(&self, buf: &[u8]) -> io::Result<usize> {
442-
self.inner().send(buf, sys::MSG_OOB)
442+
self.send_with_flags(buf, sys::MSG_OOB)
443443
}
444444

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

src/sys/unix.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub(crate) fn recv_vectored(
435435
}
436436

437437
#[cfg(not(target_os = "redox"))]
438-
pub fn recv_from_vectored(
438+
pub(crate) fn recv_from_vectored(
439439
fd: SysSocket,
440440
bufs: &mut [IoSliceMut<'_>],
441441
flags: c_int,
@@ -475,6 +475,16 @@ fn recvmsg(
475475
.map(|n| (n as usize, msg.msg_namelen, RecvFlags(msg.msg_flags)))
476476
}
477477

478+
pub(crate) fn send(fd: SysSocket, buf: &[u8], flags: c_int) -> io::Result<usize> {
479+
syscall!(send(
480+
fd,
481+
buf.as_ptr().cast(),
482+
min(buf.len(), MAX_BUF_LEN),
483+
flags,
484+
))
485+
.map(|n| n as usize)
486+
}
487+
478488
/// Unix only API.
479489
impl crate::Socket {
480490
/// Accept a new incoming connection from this listener.
@@ -610,16 +620,6 @@ pub struct Socket {
610620
}
611621

612622
impl Socket {
613-
pub fn send(&self, buf: &[u8], flags: c_int) -> io::Result<usize> {
614-
let n = syscall!(send(
615-
self.fd,
616-
buf.as_ptr() as *const c_void,
617-
cmp::min(buf.len(), max_len()),
618-
flags,
619-
))?;
620-
Ok(n as usize)
621-
}
622-
623623
pub fn send_to(&self, buf: &[u8], flags: c_int, addr: &SockAddr) -> io::Result<usize> {
624624
let n = syscall!(sendto(
625625
self.fd,
@@ -1039,7 +1039,7 @@ impl Write for Socket {
10391039

10401040
impl<'a> Write for &'a Socket {
10411041
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1042-
self.send(buf, 0)
1042+
send(self.fd, buf, 0)
10431043
}
10441044

10451045
fn flush(&mut self) -> io::Result<()> {

src/sys/windows.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,20 @@ pub(crate) fn recv_from_vectored(
407407
}
408408
}
409409

410+
pub(crate) fn send(socket: SysSocket, buf: &[u8], flags: c_int) -> io::Result<usize> {
411+
syscall!(
412+
send(
413+
socket,
414+
buf.as_ptr().cast(),
415+
min(buf.len(), MAX_BUF_LEN) as c_int,
416+
flags,
417+
),
418+
PartialEq::eq,
419+
sock::SOCKET_ERROR
420+
)
421+
.map(|n| n as usize)
422+
}
423+
410424
/// Caller must ensure `T` is the correct type for `opt` and `val`.
411425
unsafe fn getsockopt<T>(socket: SysSocket, opt: c_int, val: c_int) -> io::Result<T> {
412426
let mut payload: MaybeUninit<T> = MaybeUninit::uninit();
@@ -460,24 +474,6 @@ pub struct Socket {
460474
}
461475

462476
impl Socket {
463-
pub fn send(&self, buf: &[u8], flags: c_int) -> io::Result<usize> {
464-
unsafe {
465-
let n = {
466-
sock::send(
467-
self.socket,
468-
buf.as_ptr() as *const c_char,
469-
clamp(buf.len()),
470-
flags,
471-
)
472-
};
473-
if n == sock::SOCKET_ERROR {
474-
Err(last_error())
475-
} else {
476-
Ok(n as usize)
477-
}
478-
}
479-
}
480-
481477
pub fn send_to(&self, buf: &[u8], flags: c_int, addr: &SockAddr) -> io::Result<usize> {
482478
unsafe {
483479
let n = {
@@ -896,7 +892,7 @@ impl Write for Socket {
896892

897893
impl<'a> Write for &'a Socket {
898894
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
899-
self.send(buf, 0)
895+
send(self.socket, buf, 0)
900896
}
901897

902898
fn flush(&mut self) -> io::Result<()> {

0 commit comments

Comments
 (0)