Fixes to the socket functions. Improved output when pointer is out of range. Better error when casting to a distinct fails.

This commit is contained in:
Christoffer Lerno
2024-08-06 17:07:56 +02:00
parent b7381fc075
commit 800f7970a7
18 changed files with 94 additions and 42 deletions

View File

@@ -1,5 +1,7 @@
module libc @if(env::POSIX);
extern fn isz recv(Fd socket, void *buffer, usz length, CInt flags);
extern fn isz send(Fd socket, void *buffer, usz length, CInt flags);
extern fn void* dlopen(ZString path, int flags);
extern fn CInt dlclose(void*);
@@ -188,3 +190,4 @@ struct Termios {
Speed c_ispeed;
Speed c_ospeed;
}

View File

@@ -1,5 +1,5 @@
module libc @if(env::WIN32);
import std::os::win32;
extern fn CFile __acrt_iob_func(CInt c);
extern fn CInt _close(Fd fd); def close = _close;
@@ -15,11 +15,13 @@ extern fn Time_t _mkgmtime64(Tm* timeptr); def timegm = _mkgmtime64;
extern fn Time_t _mktime64(Tm *timeptr); def mktime = _mktime64;
extern fn usz _msize(void* ptr);
extern fn CInt _read(Fd fd, void* buffer, CUInt buffer_size);
extern fn CInt _setjmp(void* frameptr, JmpBuf* buffer) @if(env::WIN32);
extern fn CInt _setjmp(void* frameptr, JmpBuf* buffer);
extern fn CFile _wfopen(WString, WString);
extern fn CFile _wfreopen(WString, WString, CFile);
extern fn CInt _write(Fd fd, void* buffer, CUInt count);
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);
struct SystemInfo
{

View File

@@ -16,11 +16,11 @@ struct Posix_pollfd
def Posix_nfds_t = CUInt;
extern fn CInt connect(NativeSocket socket, SockAddrPtr address, Socklen_t address_len);
extern fn NativeSocket socket(AIFamily af, AISockType type, AIProtocol ip_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");
extern fn NativeSocket socket(AIFamily af, AISockType type, AIProtocol ip_protocol);
extern fn int fcntl(NativeSocket socket, int cmd, ...);
extern fn CInt bind(NativeSocket socket, SockAddrPtr address, Socklen_t address_len);
extern fn CInt listen(NativeSocket socket, CInt backlog);
extern fn NativeSocket accept(NativeSocket socket, SockAddrPtr address, Socklen_t* address_len);
extern fn CInt poll(Posix_pollfd* fds, Posix_nfds_t nfds, CInt timeout);
const CUShort POLLIN = 0x0001;
@@ -55,6 +55,11 @@ fn anyfault socket_error()
return convert_error(libc::errno());
}
macro bool NativeSocket.is_valid(self)
{
return (iptr)self >= 0;
}
macro void! NativeSocket.close(self)
{
if (libc::close(self))

View File

@@ -12,7 +12,7 @@ const int FIONREAD = 1074030207;
const int FIONBIO = -2147195266;
const int FIOASYNC = -2147195267;
distinct NativeSocket = uptr;
distinct NativeSocket = inline Win32_SOCKET;
extern fn CInt ioctlsocket(NativeSocket, CLong cmd, CULong *argp);
extern fn WSAError closesocket(NativeSocket);
@@ -22,6 +22,11 @@ extern fn int bind(NativeSocket, SockAddrPtr address, Socklen_t address_len);
extern fn int listen(NativeSocket, int backlog);
extern fn NativeSocket accept(NativeSocket, SockAddrPtr address, Socklen_t* address_len);
macro bool NativeSocket.is_valid(self)
{
return self != (NativeSocket)(uptr)-1;
}
fn void! NativeSocket.set_non_blocking(self, bool non_blocking)
{
if (ioctlsocket(self, win32::FIONBIO, &&(CULong)non_blocking))

View File

@@ -15,7 +15,7 @@ macro void @loop_over_ai(AddrInfo* ai; @body(NativeSocket fd, AddrInfo* ai))
while (ai)
{
NativeSocket sockfd = os::socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol);
if (sockfd > 0)
if (sockfd.is_valid())
{
@body(sockfd, ai);
}
@@ -120,8 +120,12 @@ fn bool! Socket.get_option(&self, SocketOption option)
fn usz! Socket.read(&self, char[] bytes) @dynamic
{
isz n = libc::read((Fd)self.sock, bytes.ptr, bytes.len);
if (n < 0) return NetError.READ_FAILED?;
$if env::WIN32:
isz n = libc::recv(self.sock, bytes.ptr, (int)bytes.len, 0);
$else
isz n = libc::recv(self.sock, bytes.ptr, bytes.len, 0);
$endif
if (n < 0) return os::socket_error()?;
return (usz)n;
}
@@ -129,8 +133,12 @@ fn char! Socket.read_byte(&self) @dynamic => io::@read_byte_using_read(self);
fn usz! Socket.write(&self, char[] bytes) @dynamic
{
isz n = libc::write((Fd)self.sock, bytes.ptr, bytes.len);
if (n < 0) return NetError.WRITE_FAILED?;
$if env::WIN32:
isz n = libc::send(self.sock, bytes.ptr, (int)bytes.len, 0);
$else
isz n = libc::send(self.sock, bytes.ptr, bytes.len, 0);
$endif
if (n < 0) return os::socket_error()?;
return (usz)n;
}

View File

@@ -44,7 +44,7 @@ fn TcpSocket! accept(TcpServerSocket* server_socket)
{
TcpSocket socket;
socket.sock = os::accept(server_socket.sock, (SockAddrPtr)&socket.ai_addr_storage, &socket.ai_addrlen);
if (socket.sock < 0) return NetError.ACCEPT_FAILED?;
if (!socket.sock.is_valid()) return NetError.ACCEPT_FAILED?;
return socket;
}