mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Introduce Socket.shutdown()
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()?;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user