Commit 49a9ef5f authored by unknown's avatar unknown

windows error handling improved

parent cc631c4c
...@@ -51,6 +51,18 @@ extern "C" { ...@@ -51,6 +51,18 @@ extern "C" {
#ifndef EPROTONOSUPPORT #ifndef EPROTONOSUPPORT
#define EPROTONOSUPPORT (ZMQ_HAUSNUMERO + 2) #define EPROTONOSUPPORT (ZMQ_HAUSNUMERO + 2)
#endif #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. // Native 0MQ error codes.
#define EMTHREAD (ZMQ_HAUSNUMERO + 50) #define EMTHREAD (ZMQ_HAUSNUMERO + 50)
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "../bindings/c/zmq.h"
#include "err.hpp" #include "err.hpp"
#include "platform.hpp" #include "platform.hpp"
...@@ -24,8 +26,6 @@ ...@@ -24,8 +26,6 @@
const char *zmq::wsa_error() const char *zmq::wsa_error()
{ {
int errcode = WSAGetLastError (); int errcode = WSAGetLastError ();
// TODO: This is not a generic way to handle this... // TODO: This is not a generic way to handle this...
if (errcode == WSAEWOULDBLOCK) if (errcode == WSAEWOULDBLOCK)
...@@ -148,4 +148,43 @@ void zmq::win_error (char *buffer_, size_t buffer_size_) ...@@ -148,4 +148,43 @@ void zmq::win_error (char *buffer_, size_t buffer_size_)
zmq_assert (rc); 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 #endif
...@@ -41,6 +41,7 @@ namespace zmq ...@@ -41,6 +41,7 @@ namespace zmq
const char *wsa_error (); const char *wsa_error ();
void win_error (char *buffer_, size_t buffer_size_); void win_error (char *buffer_, size_t buffer_size_);
void wsa_error_to_errno ();
} }
......
...@@ -50,8 +50,10 @@ int zmq::tcp_connecter_t::open () ...@@ -50,8 +50,10 @@ int zmq::tcp_connecter_t::open ()
// Create the socket. // Create the socket.
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
// TODO: Convert error to errno. if (s == INVALID_SOCKET) {
wsa_assert (s != INVALID_SOCKET); wsa_error_to_errno ();
return -1;
}
// Set to non-blocking mode. // Set to non-blocking mode.
unsigned long argp = 1; unsigned long argp = 1;
...@@ -78,9 +80,7 @@ int zmq::tcp_connecter_t::open () ...@@ -78,9 +80,7 @@ int zmq::tcp_connecter_t::open ()
return -1; return -1;
} }
// TODO: Convert error to errno. wsa_error_to_errno ();
wsa_assert (rc == 0);
return -1; return -1;
} }
......
...@@ -48,8 +48,10 @@ int zmq::tcp_listener_t::set_address (const char *addr_) ...@@ -48,8 +48,10 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
// Create a listening socket. // Create a listening socket.
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
// TODO: Convert error code to errno. if (s == INVALID_SOCKET) {
wsa_assert (s != INVALID_SOCKET); wsa_error_to_errno ();
return -1;
}
// Allow reusing of the address. // Allow reusing of the address.
int flag = 1; int flag = 1;
...@@ -65,12 +67,17 @@ int zmq::tcp_listener_t::set_address (const char *addr_) ...@@ -65,12 +67,17 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
// Bind the socket to the network interface and port. // Bind the socket to the network interface and port.
rc = bind (s, (struct sockaddr*) &addr, sizeof (addr)); rc = bind (s, (struct sockaddr*) &addr, sizeof (addr));
// TODO: Convert error code to errno. // 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. // Listen for incomming connections.
rc = listen (s, 1); rc = listen (s, 1);
// TODO: Convert error code to errno. if (rc == SOCKET_ERROR) {
wsa_assert (rc != SOCKET_ERROR); wsa_error_to_errno ();
return -1;
}
return 0; return 0;
} }
......
...@@ -49,6 +49,14 @@ const char *zmq_strerror (int errnum_) ...@@ -49,6 +49,14 @@ const char *zmq_strerror (int errnum_)
return "Not supported"; return "Not supported";
case EPROTONOSUPPORT: case EPROTONOSUPPORT:
return "Protocol not supported"; 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 #endif
case EMTHREAD: case EMTHREAD:
return "Number of preallocated application threads exceeded"; return "Number of preallocated application threads exceeded";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment