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