From 49a9ef5fcb661827ee174415b4608e609bd0a65b Mon Sep 17 00:00:00 2001
From: unknown <sustrik@.(none)>
Date: Thu, 1 Oct 2009 13:48:04 +0200
Subject: windows error handling improved

---
 bindings/c/zmq.h      | 12 ++++++++++++
 src/err.cpp           | 43 +++++++++++++++++++++++++++++++++++++++++--
 src/err.hpp           |  1 +
 src/tcp_connecter.cpp | 10 +++++-----
 src/tcp_listener.cpp  | 17 ++++++++++++-----
 src/zmq.cpp           |  8 ++++++++
 6 files changed, 79 insertions(+), 12 deletions(-)

diff --git a/bindings/c/zmq.h b/bindings/c/zmq.h
index 3eeb3f8..40750ec 100644
--- a/bindings/c/zmq.h
+++ b/bindings/c/zmq.h
@@ -51,6 +51,18 @@ extern "C" {
 #ifndef EPROTONOSUPPORT
 #define EPROTONOSUPPORT (ZMQ_HAUSNUMERO + 2)
 #endif
+#ifndef ENOBUFS
+#define ENOBUFS (ZMQ_HAUSNUMERO + 3)
+#endif
+#ifndef ENETDOWN
+#define ENETDOWN (ZMQ_HAUSNUMERO + 4)
+#endif
+#ifndef EADDRINUSE
+#define EADDRINUSE (ZMQ_HAUSNUMERO + 5)
+#endif
+#ifndef EADDRNOTAVAIL
+#define EADDRNOTAVAIL (ZMQ_HAUSNUMERO + 6)
+#endif
 
 //  Native 0MQ error codes.
 #define EMTHREAD (ZMQ_HAUSNUMERO + 50)
diff --git a/src/err.cpp b/src/err.cpp
index ef5b987..36cb2fc 100644
--- a/src/err.cpp
+++ b/src/err.cpp
@@ -17,6 +17,8 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "../bindings/c/zmq.h"
+
 #include "err.hpp"
 #include "platform.hpp"
 
@@ -24,8 +26,6 @@
 
 const char *zmq::wsa_error()
 {
-
-
     int errcode = WSAGetLastError ();
     //  TODO: This is not a generic way to handle this...
     if (errcode == WSAEWOULDBLOCK)
@@ -148,4 +148,43 @@ void zmq::win_error (char *buffer_, size_t buffer_size_)
     zmq_assert (rc);
 }
 
+void zmq::wsa_error_to_errno ()
+{
+    int errcode = WSAGetLastError ();
+    switch (errcode) {
+    case WSAEINPROGRESS:
+        errno = EAGAIN;
+        return;
+    case WSAEBADF:
+        errno = EBADF;
+        return;
+    case WSAEINVAL:
+        errno = EINVAL;
+        return;
+    case WSAEMFILE:
+        errno = EMFILE;
+        return;
+    case WSAEFAULT:
+        errno = EFAULT;
+        return;
+    case WSAEPROTONOSUPPORT:
+        errno = EPROTONOSUPPORT;
+        return;
+    case WSAENOBUFS:
+        errno = ENOBUFS;
+        return;
+    case WSAENETDOWN:
+        errno = ENETDOWN;
+        return;
+    case WSAEADDRINUSE:
+        errno = EADDRINUSE;
+        return;
+    case WSAEADDRNOTAVAIL:
+        errno = EADDRNOTAVAIL;
+        return;
+    default:
+        wsa_assert (false);
+    }
+}
+
 #endif
diff --git a/src/err.hpp b/src/err.hpp
index c1b2916..fb9195e 100644
--- a/src/err.hpp
+++ b/src/err.hpp
@@ -41,6 +41,7 @@ namespace zmq
 
     const char *wsa_error ();
     void win_error (char *buffer_, size_t buffer_size_);
+    void wsa_error_to_errno ();
   
 }
 
diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp
index 304790d..9bca0f0 100644
--- a/src/tcp_connecter.cpp
+++ b/src/tcp_connecter.cpp
@@ -50,8 +50,10 @@ int zmq::tcp_connecter_t::open ()
 
     //  Create the socket.
     s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
-    //  TODO: Convert error to errno.
-    wsa_assert (s != INVALID_SOCKET);
+    if (s == INVALID_SOCKET) {
+        wsa_error_to_errno ();
+        return -1;
+    }
 
     // Set to non-blocking mode.
     unsigned long argp = 1;
@@ -78,9 +80,7 @@ int zmq::tcp_connecter_t::open ()
         return -1;
     }
     
-    //  TODO: Convert error to errno.
-    wsa_assert (rc == 0);
-
+    wsa_error_to_errno ();
     return -1;
 }
 
diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp
index 9431ccf..383aebe 100644
--- a/src/tcp_listener.cpp
+++ b/src/tcp_listener.cpp
@@ -48,8 +48,10 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
 
     //  Create a listening socket.
     s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
-    //  TODO: Convert error code to errno.
-    wsa_assert (s != INVALID_SOCKET);
+    if (s == INVALID_SOCKET) {
+        wsa_error_to_errno ();
+        return -1;
+    }
 
     //  Allow reusing of the address.
     int flag = 1;
@@ -65,12 +67,17 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
     //  Bind the socket to the network interface and port.
     rc = bind (s, (struct sockaddr*) &addr, sizeof (addr));
     //  TODO: Convert error code to errno.
-    wsa_assert (rc != SOCKET_ERROR);
+    if (rc == SOCKET_ERROR) {
+        wsa_error_to_errno ();
+        return -1;
+    }
 
     //  Listen for incomming connections.
     rc = listen (s, 1);
-    //  TODO: Convert error code to errno.
-    wsa_assert (rc != SOCKET_ERROR);
+    if (rc == SOCKET_ERROR) {
+        wsa_error_to_errno ();
+        return -1;
+    }
 
     return 0;
 }
diff --git a/src/zmq.cpp b/src/zmq.cpp
index 21ac612..7952b61 100644
--- a/src/zmq.cpp
+++ b/src/zmq.cpp
@@ -49,6 +49,14 @@ const char *zmq_strerror (int errnum_)
         return "Not supported";
     case EPROTONOSUPPORT:
         return "Protocol not supported";
+    case ENOBUFS:
+        return "No buffer space available";
+    case ENETDOWN:
+        return "Network is down";
+    case EADDRINUSE:
+        return "Address in use";
+    case EADDRNOTAVAIL:
+        return "Address not available";
 #endif
     case EMTHREAD:
         return "Number of preallocated application threads exceeded";
-- 
cgit v1.2.3