Skip to content

Commit ca20ab9

Browse files
committed
feat(net): add set_socket_option on Socket
1 parent 48ee224 commit ca20ab9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

compio-net/src/socket.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use compio_driver::{
1010
RecvResultExt, RecvVectored, Send, SendMsg, SendTo, SendToVectored, SendVectored,
1111
ShutdownSocket,
1212
},
13-
ToSharedFd,
13+
syscall, AsRawFd, ToSharedFd,
1414
};
1515
use compio_runtime::Attacher;
1616
use socket2::{Domain, Protocol, SockAddr, Socket as Socket2, Type};
@@ -324,6 +324,33 @@ impl Socket {
324324
let op = SendMsg::new(fd, buffer, control, addr.clone());
325325
compio_runtime::submit(op).await.into_inner()
326326
}
327+
328+
#[cfg(unix)]
329+
pub fn set_socket_option<T>(&self, level: i32, name: i32, value: &T) -> io::Result<()> {
330+
syscall!(libc::setsockopt(
331+
self.socket.as_raw_fd(),
332+
level,
333+
name,
334+
value as *const _ as _,
335+
std::mem::size_of::<T>() as _
336+
))
337+
.map(|_| ())
338+
}
339+
340+
#[cfg(windows)]
341+
pub fn set_socket_option<T>(&self, level: i32, name: i32, value: &T) -> io::Result<()> {
342+
syscall!(
343+
SOCKET,
344+
windows_sys::Win32::Networking::WinSock::setsockopt(
345+
self.socket.as_raw_fd() as _,
346+
level,
347+
name,
348+
value as *const _ as _,
349+
std::mem::size_of::<T>() as _
350+
)
351+
)
352+
.map(|_| ())
353+
}
327354
}
328355

329356
impl_raw_fd!(Socket, Socket2, socket, socket);

compio-net/src/udp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ impl UdpSocket {
315315
)
316316
.await
317317
}
318+
319+
/// Sets a socket option.
320+
pub fn set_socket_option<T>(&self, level: i32, name: i32, value: &T) -> io::Result<()> {
321+
self.inner.set_socket_option(level, name, value)
322+
}
318323
}
319324

320325
impl_raw_fd!(UdpSocket, socket2::Socket, inner, socket);

0 commit comments

Comments
 (0)