summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorunknown <sustrik@.(none)>2009-10-01 13:48:04 +0200
committerunknown <sustrik@.(none)>2009-10-01 13:48:04 +0200
commit49a9ef5fcb661827ee174415b4608e609bd0a65b (patch)
treee487422695b0b4774d42569b1fbbfa1e6a848206 /src
parentcc631c4c6649b0d67114db13386a949426e35dbf (diff)
windows error handling improved
Diffstat (limited to 'src')
-rw-r--r--src/err.cpp43
-rw-r--r--src/err.hpp1
-rw-r--r--src/tcp_connecter.cpp10
-rw-r--r--src/tcp_listener.cpp17
-rw-r--r--src/zmq.cpp8
5 files changed, 67 insertions, 12 deletions
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";