[m-rev.] diff 1/2: [net] Refactoring

Paul Bone paul at bone.id.au
Tue Apr 7 16:42:01 AEST 2015


Branches: master

---

[net] Refactoring

Move type definitions from netdb.m and sockets.m to types.m.  This makes the
coupling between these modules looser.

netdb.m:
    Move port type to types.m

sockets.m:
    Move family and socktype types into types.m

types.m:
    Add the above types.
---
 extras/net/netdb.m   |  5 ++---
 extras/net/sockets.m | 44 -------------------------------------
 extras/net/types.m   | 62 +++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 59 insertions(+), 52 deletions(-)

diff --git a/extras/net/netdb.m b/extras/net/netdb.m
index e4784bf..73bfa85 100644
--- a/extras/net/netdb.m
+++ b/extras/net/netdb.m
@@ -21,12 +21,13 @@
 
 :- interface.
 
-:- import_module int.
 :- import_module io.
 :- import_module list.
 :- import_module maybe.
 :- import_module string.
 
+:- import_module net.types.
+
 %-----------------------------------------------------------------------------%
 
 :- type protocol
@@ -36,8 +37,6 @@
                 p_num           :: protocol_num
             ).
 
-:- type protocol_num == int.
-
     % Lookup a protocol entry by name.
     %
 :- pred getprotobyname(string::in, maybe(protocol)::out,
diff --git a/extras/net/sockets.m b/extras/net/sockets.m
index ac41202..636cb12 100644
--- a/extras/net/sockets.m
+++ b/extras/net/sockets.m
@@ -24,28 +24,10 @@
 :- import_module io.
 :- import_module maybe.
 
-:- import_module net.netdb.
 :- import_module net.types.
 
 %-----------------------------------------------------------------------------%
 
-    % The socket family.  This type is incomplete, support for socket
-    % families such as IPX or appletalk will probably never be added.
-    % However Unix domain sockets may be added in the future.
-    %
-:- type family
-    --->    fam_inet
-    ;       fam_inet6.
-
-    % The socket type.  Informally (for fam_inet and fam_inet6) these
-    % correspond to TCP and UDP respectively.  More precicely these specify
-    % the socket's behavour, the protocol is optionally specified
-    % seperately.
-    %
-:- type socktype
-    --->    sock_stream
-    ;       sock_dgram.
-
 :- type socket.
 
 %-----------------------------------------------------------------------------%
@@ -148,32 +130,6 @@
 
 %-----------------------------------------------------------------------------%
 
-    % This list of address families is from socket(2) on linux.
-    %
-:- pragma foreign_enum("C", family/0,
-    [fam_inet       - "AF_INET",
-     fam_inet6      - "AF_INET6"]).
-%     fam_unix       - "AF_UNIX",
-%     fam_ipx        - "AF_IPX",
-%     fam_netlink    - "AF_NETLINK",
-%     fam_x25        - "AF_X25",
-%     fam_ax25       - "AF_AX25",
-%     fam_atmpvc     - "AF_ATMPVC",
-%     fam_appletalk  - "AF_APPLETALK",
-%     fam_packet     - "AF_PACKET",
-
-:- pragma foreign_enum("C", socktype/0,
-    [sock_stream    - "SOCK_STREAM",
-     sock_dgram     - "SOCK_DGRAM"]).
-% See socket(2) for the meaning of these values.
-%     sock_seqpacket - "SOCK_SEQPACKET",
-%     sock_raw       - "SOCK_RAW",
-%     sock_rdm       - "SOCK_RDM",
-    % Note: sock_packet is obosolete.
-    % Note: We deleberately do not support the non-portable SOCK_NONBLOCK
-    % and SOCK_CLOEXEC values, this functionality should be accessed via
-    % setsocketopt.
-
 :- pragma foreign_type("C", socket, "MR_Integer", [can_pass_as_mercury_type]).
 
 %-----------------------------------------------------------------------------%
diff --git a/extras/net/types.m b/extras/net/types.m
index d2ced08..b9ecc81 100644
--- a/extras/net/types.m
+++ b/extras/net/types.m
@@ -18,12 +18,40 @@
 
 :- interface.
 
+:- import_module int.
 :- import_module string.
 
-:- import_module net.sockets.
+%-----------------------------------------------------------------------------%
+
+    % A port number.
+    %
+:- type port == int.
+
+    % Protocol number.
+    %
+    % See getprotobyname/4.
+    %
+:- type protocol_num == int.
 
 %-----------------------------------------------------------------------------%
 
+    % The socket family.  This type is incomplete, support for socket
+    % families such as IPX or appletalk will probably never be added.
+    % However Unix domain sockets may be added in the future.
+    %
+:- type family
+    --->    fam_inet
+    ;       fam_inet6.
+
+    % The socket type.  Informally (for fam_inet and fam_inet6) these
+    % correspond to TCP and UDP respectively.  More precicely these specify
+    % the socket's behavour, the protocol is optionally specified
+    % seperately.
+    %
+:- type socktype
+    --->    sock_stream
+    ;       sock_dgram.
+
 	% An IPv4 Address.
 	%
 :- type in_addr.
@@ -58,10 +86,6 @@
 :- pred to_string(in_addr::in, string::uo) is det.
 :- func to_string(in_addr) = string.
 
-    % A port number.
-    %
-:- type port == int.
-
 %-----------------------------------------------------------------------------%
 
     % A socket address, for example in ipv4 this is an IP address and a port
@@ -116,6 +140,34 @@
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
+    % This list of address families is from socket(2) on linux.
+    %
+:- pragma foreign_enum("C", family/0,
+    [fam_inet       - "AF_INET",
+     fam_inet6      - "AF_INET6"]).
+%     fam_unix       - "AF_UNIX",
+%     fam_ipx        - "AF_IPX",
+%     fam_netlink    - "AF_NETLINK",
+%     fam_x25        - "AF_X25",
+%     fam_ax25       - "AF_AX25",
+%     fam_atmpvc     - "AF_ATMPVC",
+%     fam_appletalk  - "AF_APPLETALK",
+%     fam_packet     - "AF_PACKET",
+
+:- pragma foreign_enum("C", socktype/0,
+    [sock_stream    - "SOCK_STREAM",
+     sock_dgram     - "SOCK_DGRAM"]).
+% See socket(2) for the meaning of these values.
+%     sock_seqpacket - "SOCK_SEQPACKET",
+%     sock_raw       - "SOCK_RAW",
+%     sock_rdm       - "SOCK_RDM",
+    % Note: sock_packet is obosolete.
+    % Note: We deleberately do not support the non-portable SOCK_NONBLOCK
+    % and SOCK_CLOEXEC values, this functionality should be accessed via
+    % setsocketopt.
+
+%-----------------------------------------------------------------------------%
+
 :- pragma foreign_type("C",
     in_addr,
     "struct in_addr*",
-- 
2.1.4




More information about the reviews mailing list