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

@@ -1,5 +1,10 @@
module libc @if(env::POSIX);
const CInt SHUT_RD = 0;
const CInt SHUT_WR = 1;
const CInt SHUT_RDWR = 2;
extern fn CInt shutdown(Fd sockfd, CInt how);
extern fn isz recv(Fd socket, void *buffer, usz length, CInt flags);
extern fn isz send(Fd socket, void *buffer, usz length, CInt flags);

View File

@@ -24,6 +24,11 @@ extern fn CInt _wremove(WString);
extern fn int recv(Win32_SOCKET s, void* buf, int len, int flags);
extern fn int send(Win32_SOCKET s, void* buf, int len, int flags);
const CInt SD_RECEIVE = 0;
const CInt SD_SEND = 1;
const CInt SD_BOTH = 2;
extern fn CInt shutdown(Win32_SOCKET s, CInt how);
struct SystemInfo
{
union {
@@ -51,4 +56,4 @@ macro Tm* localtime_r(Time_t* timer, Tm* buf) => _localtime64_s(buf, timer);
macro CInt setjmp(JmpBuf* buffer) => _setjmp(buffer, null);
macro Tm* gmtime_r(Time_t* timer, Tm* buf) => _gmtime64_s(buf, timer);
macro isz read(Fd fd, void* buffer, usz buffer_size) => _read(fd, buffer, (CUInt)buffer_size);
macro isz write(Fd fd, void* buffer, usz count) => _write(fd, buffer, (CUInt)count);
macro isz write(Fd fd, void* buffer, usz count) => _write(fd, buffer, (CUInt)count);

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()?;
}
}