]> git.tdb.fi Git - libs/net.git/commitdiff
Hide AF_* constants from the public header
authorMikko Rasa <tdb@tdb.fi>
Sat, 6 Aug 2011 12:03:04 +0000 (15:03 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 6 Aug 2011 16:20:37 +0000 (19:20 +0300)
source/constants.cpp [new file with mode: 0644]
source/constants.h
source/resolve.cpp
source/socket.cpp

diff --git a/source/constants.cpp b/source/constants.cpp
new file mode 100644 (file)
index 0000000..e57897d
--- /dev/null
@@ -0,0 +1,39 @@
+#include <stdexcept>
+#ifdef WIN32
+#include <winsock2.h>
+#else
+#include <sys/socket.h>
+#endif
+#include "constants.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Net {
+
+int family_to_sys(Family f)
+{
+       switch(f)
+       {
+       case UNSPEC: return AF_UNSPEC;
+       case INET:   return AF_INET;
+       case INET6:  return AF_INET6;
+       case UNIX:   return AF_UNIX;
+       default: throw invalid_argument("family_to_sys");
+       }
+}
+
+Family family_from_sys(int f)
+{
+       switch(f)
+       {
+       case AF_UNSPEC: return UNSPEC;
+       case AF_INET:   return INET;
+       case AF_INET6:  return INET6;
+       case AF_UNIX:   return UNIX;
+       default: throw invalid_argument("family_from_sys");
+       }
+}
+
+} // namespace Net
+} // namespace Msp
index 4feefcc25c46fe50628454cfc5895f4db82225ff..0d94661584d1027e2ffbe39b98c52a71da7f8974 100644 (file)
@@ -1,23 +1,20 @@
 #ifndef MSP_NET_CONSTANTS_H_
 #define MSP_NET_CONSTANTS_H_
 
-#ifdef WIN32
-#include <winsock2.h>
-#else
-#include <sys/socket.h>
-#endif
-
 namespace Msp {
 namespace Net {
 
 enum Family
 {
-       UNSPEC = AF_UNSPEC,
-       INET = AF_INET,
-       INET6 = AF_INET6,
-       UNIF = AF_UNIX
+       UNSPEC,
+       INET,
+       INET6,
+       UNIX
 };
 
+int family_to_sys(Family);
+Family family_from_sys(int);
+
 } // namespace Net
 } // namespace Msp
 
index 31b15fb9dd40ece09c09d2e25515bce25c2e00fd..3896db7a81a5d1c0a3be8ccef6667947e4b814f5 100644 (file)
@@ -37,7 +37,7 @@ SockAddr *resolve(const string &s, Family family)
                        host = s;
        }
 
-       addrinfo hints = {0, family, 0, 0, 0, 0, 0, 0};
+       addrinfo hints = { 0, family_to_sys(family), 0, 0, 0, 0, 0, 0 };
        addrinfo *res;
        const char *chost = (host.empty() ? 0 : host.c_str());
        const char *cserv = (serv.empty() ? 0 : serv.c_str());
index e91b684be8187407a6d323ff276f692756041a2e..8fa910cbd5a97eb52479a0e18192f18a6425633b 100644 (file)
@@ -66,7 +66,7 @@ Socket::Socket(Family af, int type, int proto):
        local_addr(0),
        peer_addr(0)
 {
-       priv->handle = socket(af, type, proto);
+       priv->handle = socket(family_to_sys(af), type, proto);
 
 #ifdef WIN32
        *priv->event = CreateEvent(0, false, false, 0);