Introduce Socket.shutdown()

This commit is contained in:
rexim
2025-02-06 12:27:04 +07:00
committed by Christoffer Lerno
parent 27e76fe59e
commit 122179980c
3 changed files with 44 additions and 1 deletions

View File

@@ -155,3 +155,36 @@ fn void! Socket.close(&self) @inline @dynamic
{
self.sock.close()!;
}
enum SocketShutdownHow
{
RECEIVE,
SEND,
BOTH,
}
fn CInt SocketShutdownHow.os_value(self)
{
$switch
$case env::WIN32:
switch (self) {
case RECEIVE: return libc::SD_RECEIVE;
case SEND: return libc::SD_SEND;
case BOTH: return libc::SD_BOTH;
}
$case env::POSIX:
switch (self) {
case RECEIVE: return libc::SHUT_RD;
case SEND: return libc::SHUT_WR;
case BOTH: return libc::SHUT_RDWR;
}
$default: $error("Unsupported environment");
$endswitch
}
fn void! Socket.shutdown(&self, SocketShutdownHow how)
{
if (libc::shutdown(self.sock, how.os_value()) < 0) {
return os::socket_error()?;
}
}