From e136d923b7060ef64d44264f88e49057e6638f31 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Tue, 22 Sep 2009 11:52:35 +0200 Subject: ZMQ-specific error codes added --- bindings/c/zmq.h | 34 +++++++++++++++++++++++++++++----- src/dispatcher.cpp | 2 +- src/err.cpp | 5 +++++ src/pub.cpp | 2 +- src/rep.cpp | 6 +++--- src/req.cpp | 8 ++++---- src/socket_base.cpp | 10 +++++----- src/sub.cpp | 4 ++-- src/zmq.cpp | 15 +++++++++++++++ 9 files changed, 65 insertions(+), 21 deletions(-) diff --git a/bindings/c/zmq.h b/bindings/c/zmq.h index b65a771..3931039 100644 --- a/bindings/c/zmq.h +++ b/bindings/c/zmq.h @@ -35,6 +35,21 @@ extern "C" { #define ZMQ_EXPORT #endif +//////////////////////////////////////////////////////////////////////////////// +// 0MQ errors. +//////////////////////////////////////////////////////////////////////////////// + +// A number random anough not to collide with different errno ranges on +// different OSes. The assumption is that error_t is at least 32-bit type. +#define ZMQ_HAUSNUMERO 156384712 + +#define EMTHREAD (ZMQ_HAUSNUMERO + 1) +#define EFSM (ZMQ_HAUSNUMERO + 2) +#define ENOCOMPATPROTO (ZMQ_HAUSNUMERO + 3) + +// Resolves system errors and 0MQ errors to human-readable string. +ZMQ_EXPORT const char *zmq_strerror (int errnum); + //////////////////////////////////////////////////////////////////////////////// // 0MQ message definition. //////////////////////////////////////////////////////////////////////////////// @@ -154,8 +169,8 @@ ZMQ_EXPORT int zmq_term (void *context); // Open a socket. 'type' is one of the socket types defined above. // // Errors: EINVAL - invalid socket type. -// EMFILE - the number of application threads entitled to hold open -// sockets at the same time was exceeded. +// EMTHREAD - the number of application threads entitled to hold open +// sockets at the same time was exceeded. ZMQ_EXPORT void *zmq_socket (void *context, int type); // Destroying the socket. @@ -276,9 +291,15 @@ ZMQ_EXPORT int zmq_setsockopt (void *s, int option, const void *optval, // "udp://192.168.0.111;224.1.1.1:5555". // Bind the socket to a particular address. +// +// Errors: EPROTONOSUPPORT - unsupported protocol. +// ENOCOMPATPROTO - protocol is not compatible with the socket type. ZMQ_EXPORT int zmq_bind (void *s, const char *addr); // Connect the socket to a particular address. +// +// Errors: EPROTONOSUPPORT - unsupported protocol. +// ENOCOMPATPROTO - protocol is not compatible with the socket type. ZMQ_EXPORT int zmq_connect (void *s, const char *addr); // Sending and receiving messages. @@ -303,12 +324,14 @@ ZMQ_EXPORT int zmq_connect (void *s, const char *addr); // // Errors: EAGAIN - message cannot be sent at the moment (applies only to // non-blocking send). -// EFAULT - function isn't supported by particular socket type. +// ENOTSUP - function isn't supported by particular socket type. +// EFSM - function cannot be called at the moment. ZMQ_EXPORT int zmq_send (void *s, struct zmq_msg_t *msg, int flags); // Flush the messages that were send using ZMQ_NOFLUSH flag down the stream. // -// Errors: FAULT - function isn't supported by particular socket type. +// Errors: ENOTSUP - function isn't supported by particular socket type. +// EFSM - function cannot be called at the moment. ZMQ_EXPORT int zmq_flush (void *s); // Send a message from the socket 's'. 'flags' argument can be combination @@ -316,7 +339,8 @@ ZMQ_EXPORT int zmq_flush (void *s); // // Errors: EAGAIN - message cannot be received at the moment (applies only to // non-blocking receive). -// EFAULT - function isn't supported by particular socket type. +// ENOTSUP - function isn't supported by particular socket type. +// EFSM - function cannot be called at the moment. ZMQ_EXPORT int zmq_recv (void *s, struct zmq_msg_t *msg, int flags); //////////////////////////////////////////////////////////////////////////////// diff --git a/src/dispatcher.cpp b/src/dispatcher.cpp index ef2f7c1..1f6b4f0 100644 --- a/src/dispatcher.cpp +++ b/src/dispatcher.cpp @@ -163,7 +163,7 @@ zmq::app_thread_t *zmq::dispatcher_t::choose_app_thread () return app_threads [i]; // Thread limit was exceeded. - errno = EMFILE; + errno = EMTHREAD; return NULL; } diff --git a/src/err.cpp b/src/err.cpp index bca0c03..ef5b987 100644 --- a/src/err.cpp +++ b/src/err.cpp @@ -24,11 +24,16 @@ const char *zmq::wsa_error() { + + int errcode = WSAGetLastError (); // TODO: This is not a generic way to handle this... if (errcode == WSAEWOULDBLOCK) return NULL; + // TODO: It seems that list of Windows socket errors is longer than this. + // Investigate whether there's a way to convert it into the string + // automatically (wsaError->HRESULT->string?). return (errcode == WSABASEERR) ? "No Error" : diff --git a/src/pub.cpp b/src/pub.cpp index faaa9aa..a403157 100644 --- a/src/pub.cpp +++ b/src/pub.cpp @@ -152,7 +152,7 @@ int zmq::pub_t::xflush () int zmq::pub_t::xrecv (struct zmq_msg_t *msg_, int flags_) { - errno = EFAULT; + errno = ENOTSUP; return -1; } diff --git a/src/rep.cpp b/src/rep.cpp index fcf8058..7ebbc6d 100644 --- a/src/rep.cpp +++ b/src/rep.cpp @@ -137,7 +137,7 @@ int zmq::rep_t::xsetsockopt (int option_, const void *optval_, int zmq::rep_t::xsend (struct zmq_msg_t *msg_, int flags_) { if (!waiting_for_reply) { - errno = EFAULT; + errno = EFSM; return -1; } @@ -161,7 +161,7 @@ int zmq::rep_t::xsend (struct zmq_msg_t *msg_, int flags_) int zmq::rep_t::xflush () { - errno = EFAULT; + errno = ENOTSUP; return -1; } @@ -171,7 +171,7 @@ int zmq::rep_t::xrecv (struct zmq_msg_t *msg_, int flags_) zmq_msg_close (msg_); if (waiting_for_reply) { - errno = EFAULT; + errno = EFSM; return -1; } diff --git a/src/req.cpp b/src/req.cpp index 9b4dffa..3fd9b38 100644 --- a/src/req.cpp +++ b/src/req.cpp @@ -120,12 +120,12 @@ int zmq::req_t::xsend (struct zmq_msg_t *msg_, int flags_) // If we've sent a request and we still haven't got the reply, // we can't send another request. if (waiting_for_reply) { - errno = EFAULT; + errno = EFSM; return -1; } if (out_pipes.empty ()) { - errno = EFAULT; + errno = EAGAIN; return -1; } @@ -166,7 +166,7 @@ int zmq::req_t::xsend (struct zmq_msg_t *msg_, int flags_) int zmq::req_t::xflush () { - errno = EFAULT; + errno = ENOTSUP; return -1; } @@ -178,7 +178,7 @@ int zmq::req_t::xrecv (struct zmq_msg_t *msg_, int flags_) // If request wasn't send, we can't wait for reply. if (!waiting_for_reply) { zmq_msg_init (msg_); - errno = EFAULT; + errno = EFSM; return -1; } diff --git a/src/socket_base.cpp b/src/socket_base.cpp index 0452993..6763167 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -100,8 +100,8 @@ int zmq::socket_base_t::bind (const char *addr_) } #endif - // Unknown address type. - errno = EFAULT; + // Unknown protocol. + errno = EPROTONOSUPPORT; return -1; } @@ -185,7 +185,7 @@ int zmq::socket_base_t::connect (const char *addr_) // If the socket type requires bi-directional communication // multicast is not an option (it is uni-directional). if (options.requires_in && options.requires_out) { - errno = EFAULT; + errno = ENOCOMPATPROTO; return -1; } @@ -235,8 +235,8 @@ int zmq::socket_base_t::connect (const char *addr_) } #endif - // Unknown address type. - errno = EFAULT; + // Unknown protoco. + errno = EPROTONOSUPPORT; return -1; } diff --git a/src/sub.cpp b/src/sub.cpp index 66aa8fd..abd9deb 100644 --- a/src/sub.cpp +++ b/src/sub.cpp @@ -124,13 +124,13 @@ int zmq::sub_t::xsetsockopt (int option_, const void *optval_, int zmq::sub_t::xsend (struct zmq_msg_t *msg_, int flags_) { - errno = EFAULT; + errno = ENOTSUP; return -1; } int zmq::sub_t::xflush () { - errno = EFAULT; + errno = ENOTSUP; return -1; } diff --git a/src/zmq.cpp b/src/zmq.cpp index e831046..d6f7023 100644 --- a/src/zmq.cpp +++ b/src/zmq.cpp @@ -19,6 +19,7 @@ #include "../bindings/c/zmq.h" +#include #include #include #include @@ -35,6 +36,20 @@ #include #endif +const char *zmq_strerror (int errnum_) +{ + switch (errnum_) { + case EMTHREAD: + return "Number of preallocated application threads exceeded"; + case EFSM: + return "Operation cannot be accomplished in current state"; + case ENOCOMPATPROTO: + return "The protocol is not compatible with the socket type"; + default: + return strerror (errnum_); + } +} + int zmq_msg_init (zmq_msg_t *msg_) { msg_->content = (zmq::msg_content_t*) ZMQ_VSM; -- cgit v1.2.3