rename `NativeString` to `CString`
[nit.git] / lib / socket / socket_c.nit
index 6e0cc55..1486752 100644 (file)
@@ -126,7 +126,7 @@ extern class NativeSocket `{ int* `}
        `}
 
        # Write `length` bytes from `buffer`
-       fun write(buffer: NativeString, length: Int): Int `{
+       fun write(buffer: CString, length: Int): Int `{
                return write(*self, buffer, length);
        `}
 
@@ -137,7 +137,7 @@ extern class NativeSocket `{ int* `}
        `}
 
        # Read `length` bytes into `buffer`, returns the number of bytes read
-       fun read(buffer: NativeString, length: Int): Int `{
+       fun read(buffer: CString, length: Int): Int `{
                return read(*self, buffer, length);
        `}
 
@@ -164,6 +164,15 @@ extern class NativeSocket `{ int* `}
                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:
@@ -202,7 +211,7 @@ extern class NativeSocket `{ int* `}
                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;
@@ -216,6 +225,22 @@ extern class NativeSocket `{ int* `}
                }
                fcntl(*self, F_SETFL, flags);
        `}
+
+       # Send `len` bytes from `buf` to `dest_addr`
+       fun sendto(buf: CString, 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: CString, 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: CString, 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`
@@ -250,7 +275,7 @@ extern class NativeSocketAddrIn `{ struct sockaddr_in* `}
        `}
 
        # Internet address as then IPV4 numbers-and-dots notation
-       fun address: NativeString `{ return (char*)inet_ntoa(self->sin_addr); `}
+       fun address: CString `{ return (char*)inet_ntoa(self->sin_addr); `}
 
        # Set `address` to `INADDR_ANY`
        fun address_any `{ self->sin_addr.s_addr = INADDR_ANY; `}
@@ -273,7 +298,7 @@ end
 
 # Host entry information, a pointer to a `struct hostent`
 extern class NativeSocketHostent `{ struct hostent* `}
-       private fun native_h_aliases(i: Int): NativeString `{
+       private fun native_h_aliases(i: Int): CString `{
                return self->h_aliases[i];
        `}
 
@@ -289,7 +314,7 @@ extern class NativeSocketHostent `{ struct hostent* `}
                return res
        end
 
-       fun h_addr: NativeString `{
+       fun h_addr: CString `{
                return (char*)inet_ntoa(*(struct in_addr*)self->h_addr);
        `}
 
@@ -297,7 +322,7 @@ extern class NativeSocketHostent `{ struct hostent* `}
 
        fun h_length: Int `{ return self->h_length; `}
 
-       fun h_name: NativeString `{ return self->h_name; `}
+       fun h_name: CString `{ return self->h_name; `}
 end
 
 extern class NativeTimeval `{ struct timeval* `}
@@ -404,6 +429,7 @@ extern class NativeSocketProtocolFamilies `{ int `}
        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
@@ -492,7 +518,7 @@ end
 
 redef class Sys
        # Get network host entry
-       fun gethostbyname(name: NativeString): NativeSocketHostent `{
+       fun gethostbyname(name: CString): NativeSocketHostent `{
                return gethostbyname(name);
        `}