lib/socket: revamp services of `NativeSocketAddrIn`
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 5 Jul 2015 17:54:22 +0000 (13:54 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 17 Jul 2015 12:15:35 +0000 (08:15 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/socket/socket.nit
lib/socket/socket_c.nit

index f318dcc..515f368 100644 (file)
@@ -72,8 +72,8 @@ class TCPStream
                        return
                end
 
-               var hostname = sys.gethostbyname(host.to_cstring)
-               if hostname.address_is_null then
+               var hostent = sys.gethostbyname(host.to_cstring)
+               if hostent.address_is_null then
                        # Error in name lookup
                        var err = sys.h_errno
                        last_error = new IOError(err.to_s)
@@ -84,9 +84,12 @@ class TCPStream
                        return
                end
 
-               addrin = new NativeSocketAddrIn.with_hostent(hostname, port)
+               addrin = new NativeSocketAddrIn
+               addrin.fill_from_hostent hostent
+               addrin.port = port
+
                address = addrin.address.to_s
-               init(addrin.port, hostname.h_name)
+               init(addrin.port, hostent.h_name.to_s)
 
                closed = not internal_connect
                end_reached = closed
@@ -240,7 +243,12 @@ class TCPServer
                        closed = true
                        return
                end
-               addrin = new NativeSocketAddrIn.with_port(port, new NativeSocketAddressFamilies.af_inet)
+
+               addrin = new NativeSocketAddrIn
+               addrin.family = new NativeSocketAddressFamilies.af_inet
+               addrin.port = port
+               addrin.address_any
+
                address = addrin.address.to_s
 
                # Bind it
index ea562b0..de1ba30 100644 (file)
@@ -233,38 +233,47 @@ class SocketAcceptResult
        var addr_in: NativeSocketAddrIn
 end
 
+# Socket address in the Internet namespace, pointer to a `struct sockaddr_in`
 extern class NativeSocketAddrIn `{ struct sockaddr_in* `}
-       new `{
-               struct sockaddr_in *sai = NULL;
-               sai = malloc(sizeof(struct sockaddr_in));
-               return sai;
-       `}
 
-       new with_port(port: Int, family: NativeSocketAddressFamilies) `{
+       # `NULL` pointer
+       new nul `{ return NULL; `}
+
+       # `malloc` a new instance
+       new `{
                struct sockaddr_in *sai = NULL;
                sai = malloc(sizeof(struct sockaddr_in));
-               sai->sin_family = family;
-               sai->sin_port = htons(port);
-               sai->sin_addr.s_addr = INADDR_ANY;
                return sai;
        `}
 
-       new with_hostent(hostent: NativeSocketHostent, port: Int) `{
-               struct sockaddr_in *sai = NULL;
-               sai = malloc(sizeof(struct sockaddr_in));
-               sai->sin_family = hostent->h_addrtype;
-               sai->sin_port = htons(port);
-               memcpy((char*)&sai->sin_addr.s_addr, (char*)hostent->h_addr, hostent->h_length);
-               return sai;
+       # Set `address` and `family` from `hostent` (to use with `Sys::gethostbyname`)
+       fun fill_from_hostent(hostent: NativeSocketHostent) `{
+               self->sin_family = hostent->h_addrtype;
+               memcpy((char*)&self->sin_addr.s_addr,
+                      (char*)hostent->h_addr,
+                          hostent->h_length);
        `}
 
+       # Internet address as then IPV4 numbers-and-dots notation
        fun address: NativeString `{ return (char*)inet_ntoa(self->sin_addr); `}
 
+       # Set `address` to `INADDR_ANY`
+       fun address_any `{ self->sin_addr.s_addr = INADDR_ANY; `}
+
+       # Set `address` to `INADDR_BROADCAST`
+       fun address_broadcast `{ self->sin_addr.s_addr = INADDR_BROADCAST; `}
+
+       # Address family
        fun family: NativeSocketAddressFamilies `{ return self->sin_family; `}
 
+       # Address family
+       fun family=(value: NativeSocketAddressFamilies) `{ self->sin_family = value; `}
+
+       # Port
        fun port: Int `{ return ntohs(self->sin_port); `}
 
-       fun destroy `{ free(self); `}
+       # Port
+       fun port=(value: Int) `{ self->sin_port = htons(value); `}
 end
 
 # Host entry information, a pointer to a `struct hostent`