mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
* lib/std/net: add Network, Socket and Listener Signed-off-by: Pierre Curto <pierre.curto@gmail.com> * lib/std/net: add SocketOption Signed-off-by: Pierre Curto <pierre.curto@gmail.com> * lib/std/net: fixes for win32 and wasm Signed-off-by: Pierre Curto <pierre.curto@gmail.com> --------- Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
39 lines
1.2 KiB
C
39 lines
1.2 KiB
C
module std::net::os @if(env::POSIX && SUPPORTS_INET);
|
|
import libc;
|
|
|
|
const int F_GETFL = 3;
|
|
const int F_SETFL = 4;
|
|
|
|
def NativeSocket = distinct inline Fd;
|
|
|
|
extern fn CInt connect(NativeSocket socket, SockAddrPtr address, Socklen_t address_len);
|
|
extern fn NativeSocket socket(CInt af, CInt type, CInt protocol) @extern("socket");
|
|
extern fn int fcntl(NativeSocket socket, int cmd, ...) @extern("fcntl");
|
|
extern fn CInt bind(NativeSocket socket, SockAddrPtr address, Socklen_t address_len) @extern("bind");
|
|
extern fn CInt listen(NativeSocket socket, CInt backlog) @extern("listen");
|
|
extern fn NativeSocket accept(NativeSocket socket, SockAddrPtr address, Socklen_t* address_len) @extern("accept");
|
|
|
|
macro void! NativeSocket.close(self)
|
|
{
|
|
if (libc::close(self))
|
|
{
|
|
if (libc::errno() == errno::EBADF) return NetError.INVALID_SOCKET?;
|
|
return NetError.GENERAL_ERROR?;
|
|
}
|
|
}
|
|
|
|
macro void! NativeSocket.set_non_blocking(self)
|
|
{
|
|
int flags = fcntl(self, F_GETFL, 0);
|
|
if (fcntl(self, F_SETFL, flags | O_NONBLOCK) == -1)
|
|
{
|
|
if (libc::errno() == errno::EBADF) return NetError.INVALID_SOCKET?;
|
|
return NetError.GENERAL_ERROR?;
|
|
}
|
|
}
|
|
|
|
macro bool NativeSocket.is_non_blocking(self)
|
|
{
|
|
return fcntl(self, F_GETFL, 0) & O_NONBLOCK == O_NONBLOCK;
|
|
}
|