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

@@ -116,7 +116,7 @@ impl BorrowedSocket<'_> {
let mut info = unsafe { mem::zeroed::<sys::c::WSAPROTOCOL_INFOW>() }; let mut info = unsafe { mem::zeroed::<sys::c::WSAPROTOCOL_INFOW>() };
let result = unsafe { let result = unsafe {
sys::c::WSADuplicateSocketW( sys::c::WSADuplicateSocketW(
self.as_raw_socket(), self.as_raw_socket() as sys::c::SOCKET,
sys::c::GetCurrentProcessId(), sys::c::GetCurrentProcessId(),
&mut info, &mut info,
) )
@@ -134,7 +134,7 @@ impl BorrowedSocket<'_> {
}; };
if socket != sys::c::INVALID_SOCKET { if socket != sys::c::INVALID_SOCKET {
unsafe { Ok(OwnedSocket::from_raw_socket(socket)) } unsafe { Ok(OwnedSocket::from_raw_socket(socket as RawSocket)) }
} else { } else {
let error = unsafe { sys::c::WSAGetLastError() }; let error = unsafe { sys::c::WSAGetLastError() };
@@ -158,7 +158,7 @@ impl BorrowedSocket<'_> {
} }
unsafe { unsafe {
let socket = OwnedSocket::from_raw_socket(socket); let socket = OwnedSocket::from_raw_socket(socket as RawSocket);
socket.set_no_inherit()?; socket.set_no_inherit()?;
Ok(socket) Ok(socket)
} }
@@ -211,7 +211,7 @@ impl Drop for OwnedSocket {
#[inline] #[inline]
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let _ = sys::c::closesocket(self.socket); let _ = sys::c::closesocket(self.socket as sys::c::SOCKET);
} }
} }
} }

View File

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