lib/socket: add UDP related services to the native layer
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 5 Jul 2015 17:53:48 +0000 (13:53 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 17 Jul 2015 12:15:36 +0000 (08:15 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/socket/socket_c.nit

index 6e0cc55..ea0b832 100644 (file)
@@ -202,7 +202,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 +216,22 @@ extern class NativeSocket `{ int* `}
                }
                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`
@@ -404,6 +420,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