return connect(*self, (struct sockaddr*)addrIn, sizeof(*addrIn));
`}
- fun write(buffer: String): Int
- import String.to_cstring, String.length `{
- return write(*self, (char*)String_to_cstring(buffer), String_length(buffer));
+ # Write `length` bytes from `buffer`
+ fun write(buffer: NativeString, length: Int): Int `{
+ return write(*self, buffer, length);
`}
# Write `value` as a single byte
return write(*self, &byt, 1);
`}
- fun read: String import NativeString.to_s_with_length, NativeString `{
- char *c = new_NativeString(1024);
- int n = read(*self, c, 1023);
- if(n < 0) {
- return NativeString_to_s_with_length("",0);
- }
- c[n] = 0;
- return NativeString_to_s_with_length(c, n);
+ # Read `length` bytes into `buffer`, returns the number of bytes read
+ fun read(buffer: NativeString, length: Int): Int `{
+ return read(*self, buffer, length);
`}
# Sets an option for the socket
return filedesc.check_response(result)
end
+ # Poll this socket with `POLLHUP|POLLERR`
+ #
+ # A return value of 0 means there is no errors.
+ fun poll_hup_err: Int `{
+ struct pollfd fd = {*self, POLLHUP|POLLERR, 0};
+ int res = poll(&fd, 1, 0);
+ return res;
+ `}
+
# Call to the poll function of the C socket
#
# Signature:
return new SocketAcceptResult(s, addrIn)
end
- # Set wether this socket is non blocking
+ # Set whether this socket is non blocking
fun non_blocking=(value: Bool) `{
int flags = fcntl(*self, F_GETFL, 0);
if (flags == -1) flags = 0;
}
fcntl(*self, F_SETFL, flags);
`}
+
+ # Send `len` bytes from `buf` to `dest_addr`
+ fun sendto(buf: NativeString, len: Int, flags: Int, dest_addr: NativeSocketAddrIn): Int `{
+ return sendto(*self, buf, len, flags, (struct sockaddr*)dest_addr, sizeof(struct sockaddr_in));
+ `}
+
+ # Receive a message into `buf` of maximum `len` bytes
+ fun recv(buf: NativeString, len: Int, flags: Int): Int `{
+ return recv(*self, buf, len, flags);
+ `}
+
+ # Receive a message into `buf` of maximum `len` bytes and store sender info into `src_addr`
+ fun recvfrom(buf: NativeString, len: Int, flags: Int, src_addr: NativeSocketAddrIn): Int `{
+ socklen_t srclen = sizeof(struct sockaddr_in);
+ return recvfrom(*self, buf, len, flags, (struct sockaddr*)src_addr, &srclen);
+ `}
end
# Result of a call to `NativeSocket::accept`
new pf_key `{ return PF_KEY; `}
new pf_inet6 `{ return PF_INET6; `}
new pf_max `{ return PF_MAX; `}
+ new ipproto_udp `{ return IPPROTO_UDP; `}
end
# Level on which to set options