Commit 49a9ef5f authored by unknown's avatar unknown

windows error handling improved

parent cc631c4c
......@@ -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)
......
......@@ -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
......@@ -41,6 +41,7 @@ namespace zmq
const char *wsa_error ();
void win_error (char *buffer_, size_t buffer_size_);
void wsa_error_to_errno ();
}
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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";
......
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