Abstract over internal SOCKET type

This allows `SOCKET` to be `usize` internally
This commit is contained in:
Chris Denton
2023-08-16 15:18:53 +01:00
parent d9c85daa51
commit eb19bd33ba
2 changed files with 26 additions and 20 deletions

View File

@@ -117,7 +117,7 @@ impl Socket {
};
if socket != c::INVALID_SOCKET {
unsafe { Ok(Self::from_raw_socket(socket)) }
unsafe { Ok(Self::from_raw(socket)) }
} else {
let error = unsafe { c::WSAGetLastError() };
@@ -133,7 +133,7 @@ impl Socket {
}
unsafe {
let socket = Self::from_raw_socket(socket);
let socket = Self::from_raw(socket);
socket.0.set_no_inherit()?;
Ok(socket)
}
@@ -144,7 +144,7 @@ impl Socket {
self.set_nonblocking(true)?;
let result = {
let (addr, len) = addr.into_inner();
let result = unsafe { c::connect(self.as_raw_socket(), addr.as_ptr(), len) };
let result = unsafe { c::connect(self.as_raw(), addr.as_ptr(), len) };
cvt(result).map(drop)
};
self.set_nonblocking(false)?;
@@ -170,7 +170,7 @@ impl Socket {
let fds = {
let mut fds = unsafe { mem::zeroed::<c::fd_set>() };
fds.fd_count = 1;
fds.fd_array[0] = self.as_raw_socket();
fds.fd_array[0] = self.as_raw();
fds
};
@@ -202,11 +202,11 @@ impl Socket {
}
pub fn accept(&self, storage: *mut c::SOCKADDR, len: *mut c_int) -> io::Result<Socket> {
let socket = unsafe { c::accept(self.as_raw_socket(), storage, len) };
let socket = unsafe { c::accept(self.as_raw(), storage, len) };
match socket {
c::INVALID_SOCKET => Err(last_error()),
_ => unsafe { Ok(Self::from_raw_socket(socket)) },
_ => unsafe { Ok(Self::from_raw(socket)) },
}
}
@@ -218,9 +218,8 @@ impl Socket {
// On unix when a socket is shut down all further reads return 0, so we
// do the same on windows to map a shut down socket to returning EOF.
let length = cmp::min(buf.capacity(), i32::MAX as usize) as i32;
let result = unsafe {
c::recv(self.as_raw_socket(), buf.as_mut().as_mut_ptr() as *mut _, length, flags)
};
let result =
unsafe { c::recv(self.as_raw(), buf.as_mut().as_mut_ptr() as *mut _, length, flags) };
match result {
c::SOCKET_ERROR => {
@@ -257,7 +256,7 @@ impl Socket {
let mut flags = 0;
let result = unsafe {
c::WSARecv(
self.as_raw_socket(),
self.as_raw(),
bufs.as_mut_ptr() as *mut c::WSABUF,
length,
&mut nread,
@@ -305,7 +304,7 @@ impl Socket {
// do the same on windows to map a shut down socket to returning EOF.
let result = unsafe {
c::recvfrom(
self.as_raw_socket(),
self.as_raw(),
buf.as_mut_ptr() as *mut _,
length,
flags,
@@ -341,7 +340,7 @@ impl Socket {
let mut nwritten = 0;
let result = unsafe {
c::WSASend(
self.as_raw_socket(),
self.as_raw(),
bufs.as_ptr() as *const c::WSABUF as *mut _,
length,
&mut nwritten,
@@ -392,14 +391,14 @@ impl Socket {
Shutdown::Read => c::SD_RECEIVE,
Shutdown::Both => c::SD_BOTH,
};
let result = unsafe { c::shutdown(self.as_raw_socket(), how) };
let result = unsafe { c::shutdown(self.as_raw(), how) };
cvt(result).map(drop)
}
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
let mut nonblocking = nonblocking as c_ulong;
let result =
unsafe { c::ioctlsocket(self.as_raw_socket(), c::FIONBIO as c_int, &mut nonblocking) };
unsafe { c::ioctlsocket(self.as_raw(), c::FIONBIO as c_int, &mut nonblocking) };
cvt(result).map(drop)
}
@@ -433,8 +432,15 @@ impl Socket {
}
// This is used by sys_common code to abstract over Windows and Unix.
pub fn as_raw(&self) -> RawSocket {
self.as_inner().as_raw_socket()
pub fn as_raw(&self) -> c::SOCKET {
debug_assert_eq!(mem::size_of::<c::SOCKET>(), mem::size_of::<RawSocket>());
debug_assert_eq!(mem::align_of::<c::SOCKET>(), mem::align_of::<RawSocket>());
self.as_inner().as_raw_socket() as c::SOCKET
}
pub unsafe fn from_raw(raw: c::SOCKET) -> Self {
debug_assert_eq!(mem::size_of::<c::SOCKET>(), mem::size_of::<RawSocket>());
debug_assert_eq!(mem::align_of::<c::SOCKET>(), mem::align_of::<RawSocket>());
Self::from_raw_socket(raw as RawSocket)
}
}