Commit 6996ef6f authored by Martin Sustrik's avatar Martin Sustrik

improved error handling

parent cb09c695
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include <exception> #include <exception>
namespace zmq namespace zmq
...@@ -38,36 +39,21 @@ namespace zmq ...@@ -38,36 +39,21 @@ namespace zmq
message_delimiter = 1 << ZMQ_DELIMITER message_delimiter = 1 << ZMQ_DELIMITER
}; };
class no_memory : public std::exception // The class masquerades POSIX-style errno error as a C++ exception.
class error_t : public std::exception
{ {
virtual const char *what () public:
{
return "Out of memory";
}
};
class invalid_argument : public std::exception error_t () : errnum (errno) {}
{
virtual const char *what ()
{
return "Invalid argument";
}
};
class too_many_threads : public std::exception virtual const char *what () const throw ()
{
virtual const char *what ()
{ {
return "Too many threads"; return strerror (errnum);
} }
};
class address_in_use : public std::exception private:
{
virtual const char *what () int errnum;
{
return "Address in use";
}
}; };
// A message. Caution: Don't change the body of the message once you've // A message. Caution: Don't change the body of the message once you've
...@@ -84,10 +70,8 @@ namespace zmq ...@@ -84,10 +70,8 @@ namespace zmq
inline message_t (size_t size_ = 0) inline message_t (size_t size_ = 0)
{ {
int rc = zmq_msg_init_size (this, size_); int rc = zmq_msg_init_size (this, size_);
if (rc == -1) { if (rc != 0)
assert (errno == ENOMEM); throw error_t ();
throw no_memory ();
}
} }
// Creates message from the supplied buffer. 0MQ takes care of // Creates message from the supplied buffer. 0MQ takes care of
...@@ -98,14 +82,16 @@ namespace zmq ...@@ -98,14 +82,16 @@ namespace zmq
free_fn *ffn_) free_fn *ffn_)
{ {
int rc = zmq_msg_init_data (this, data_, size_, ffn_); int rc = zmq_msg_init_data (this, data_, size_, ffn_);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
// Destroys the message. // Destroys the message.
inline ~message_t () inline ~message_t ()
{ {
int rc = zmq_msg_close (this); int rc = zmq_msg_close (this);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
// Destroys old content of the message and allocates buffer for the // Destroys old content of the message and allocates buffer for the
...@@ -114,12 +100,11 @@ namespace zmq ...@@ -114,12 +100,11 @@ namespace zmq
inline void rebuild (size_t size_) inline void rebuild (size_t size_)
{ {
int rc = zmq_msg_close (this); int rc = zmq_msg_close (this);
assert (rc == 0); if (rc != 0)
throw error_t ();
rc = zmq_msg_init_size (this, size_); rc = zmq_msg_init_size (this, size_);
if (rc == -1) { if (rc != 0)
assert (errno == ENOMEM); throw error_t ();
throw no_memory ();
}
} }
// Same as above, however, the message is rebuilt from the supplied // Same as above, however, the message is rebuilt from the supplied
...@@ -128,9 +113,11 @@ namespace zmq ...@@ -128,9 +113,11 @@ namespace zmq
inline void rebuild (void *data_, size_t size_, free_fn *ffn_) inline void rebuild (void *data_, size_t size_, free_fn *ffn_)
{ {
int rc = zmq_msg_close (this); int rc = zmq_msg_close (this);
assert (rc == 0); if (rc != 0)
throw error_t ();
rc = zmq_msg_init_data (this, data_, size_, ffn_); rc = zmq_msg_init_data (this, data_, size_, ffn_);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
// Moves the message content from one message to the another. If the // Moves the message content from one message to the another. If the
...@@ -140,7 +127,8 @@ namespace zmq ...@@ -140,7 +127,8 @@ namespace zmq
inline void move_to (message_t *msg_) inline void move_to (message_t *msg_)
{ {
int rc = zmq_msg_move (this, (zmq_msg_t*) msg_); int rc = zmq_msg_move (this, (zmq_msg_t*) msg_);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
// Copies the message content from one message to the another. If the // Copies the message content from one message to the another. If the
...@@ -149,7 +137,8 @@ namespace zmq ...@@ -149,7 +137,8 @@ namespace zmq
inline void copy_to (message_t *msg_) inline void copy_to (message_t *msg_)
{ {
int rc = zmq_msg_copy (this, (zmq_msg_t*) msg_); int rc = zmq_msg_copy (this, (zmq_msg_t*) msg_);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
// Returns message type. // Returns message type.
...@@ -187,10 +176,8 @@ namespace zmq ...@@ -187,10 +176,8 @@ namespace zmq
inline context_t (int app_threads_, int io_threads_) inline context_t (int app_threads_, int io_threads_)
{ {
ptr = zmq_init (app_threads_, io_threads_); ptr = zmq_init (app_threads_, io_threads_);
if (ptr == NULL) { if (ptr == NULL)
assert (errno == EINVAL); throw error_t ();
throw invalid_argument ();
}
} }
inline ~context_t () inline ~context_t ()
...@@ -215,70 +202,64 @@ namespace zmq ...@@ -215,70 +202,64 @@ namespace zmq
inline socket_t (context_t &context_, int type_ = 0) inline socket_t (context_t &context_, int type_ = 0)
{ {
ptr = zmq_socket (context_.ptr, type_); ptr = zmq_socket (context_.ptr, type_);
if (ptr == NULL) { if (ptr == NULL)
assert (errno == EMFILE || errno == EINVAL); throw error_t ();
if (errno == EMFILE)
throw too_many_threads ();
else
throw invalid_argument ();
}
} }
inline ~socket_t () inline ~socket_t ()
{ {
int rc = zmq_close (ptr); int rc = zmq_close (ptr);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
inline void setsockopt (int option_, const void *optval_, inline void setsockopt (int option_, const void *optval_,
size_t optvallen_) size_t optvallen_)
{ {
int rc = zmq_setsockopt (ptr, option_, optval_, optvallen_); int rc = zmq_setsockopt (ptr, option_, optval_, optvallen_);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
inline void bind (const char *addr_) inline void bind (const char *addr_)
{ {
int rc = zmq_bind (ptr, addr_); int rc = zmq_bind (ptr, addr_);
if (rc == -1) { if (rc != 0)
assert (errno == EINVAL || errno == EADDRINUSE); throw error_t ();
if (errno == EINVAL)
throw invalid_argument ();
else
throw address_in_use ();
}
} }
inline void connect (const char *addr_) inline void connect (const char *addr_)
{ {
int rc = zmq_connect (ptr, addr_); int rc = zmq_connect (ptr, addr_);
if (rc == -1) { if (rc != 0)
assert (errno == EINVAL || errno == EADDRINUSE); throw error_t ();
if (errno == EINVAL)
throw invalid_argument ();
else
throw address_in_use ();
}
} }
inline int send (message_t &msg_, int flags_ = 0) inline bool send (message_t &msg_, int flags_ = 0)
{ {
int rc = zmq_send (ptr, &msg_, flags_); int rc = zmq_send (ptr, &msg_, flags_);
assert (rc == 0 || (rc == -1 && errno == EAGAIN)); if (rc == 0)
return rc; return true;
if (rc == -1 && errno == EAGAIN)
return false;
throw error_t ();
} }
inline void flush () inline void flush ()
{ {
int rc = zmq_flush (ptr); int rc = zmq_flush (ptr);
assert (rc == 0); if (rc != 0)
throw error_t ();
} }
inline int recv (message_t *msg_, int flags_ = 0) inline bool recv (message_t *msg_, int flags_ = 0)
{ {
int rc = zmq_recv (ptr, msg_, flags_); int rc = zmq_recv (ptr, msg_, flags_);
assert (rc == 0 || (rc == -1 && errno == EAGAIN)); if (rc == 0)
return rc; return true;
if (rc == -1 && errno == EAGAIN)
return false;
throw error_t ();
} }
private: private:
......
...@@ -285,7 +285,8 @@ int zmq::resolve_ip_hostname (sockaddr_in *addr_, const char *hostname_) ...@@ -285,7 +285,8 @@ int zmq::resolve_ip_hostname (sockaddr_in *addr_, const char *hostname_)
// Separate the hostname. // Separate the hostname.
std::string hostname (hostname_, delimiter - hostname_); std::string hostname (hostname_, delimiter - hostname_);
// Resolve host name. // Resolve host name. Some of the error info is lost in case of error,
// however, there's no way to report EAI errors via errno.
addrinfo req; addrinfo req;
memset (&req, 0, sizeof (req)); memset (&req, 0, sizeof (req));
req.ai_family = AF_INET; req.ai_family = AF_INET;
......
...@@ -55,11 +55,10 @@ zmq::tcp_listener_t::~tcp_listener_t () ...@@ -55,11 +55,10 @@ zmq::tcp_listener_t::~tcp_listener_t ()
int zmq::tcp_listener_t::set_address (const char *addr_) int zmq::tcp_listener_t::set_address (const char *addr_)
{ {
// Convert the interface into sockaddr_in structure. // Convert the interface into sockaddr_in structure.
return resolve_ip_interface (&addr, addr_); int rc = resolve_ip_interface (&addr, addr_);
} if (rc != 0)
return rc;
int zmq::tcp_listener_t::open ()
{
// Create a listening socket. // Create a listening socket.
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1) if (s == -1)
...@@ -67,7 +66,7 @@ int zmq::tcp_listener_t::open () ...@@ -67,7 +66,7 @@ int zmq::tcp_listener_t::open ()
// Allow reusing of the address. // Allow reusing of the address.
int flag = 1; int flag = 1;
int rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int)); rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
errno_assert (rc == 0); errno_assert (rc == 0);
// Set the non-blocking flag. // Set the non-blocking flag.
......
...@@ -35,14 +35,11 @@ namespace zmq ...@@ -35,14 +35,11 @@ namespace zmq
tcp_listener_t (); tcp_listener_t ();
~tcp_listener_t (); ~tcp_listener_t ();
// Set up the address to listen on. Address is in // Start listening on the interface. Address is in
// <interface-name>:<port-number> format. Interface name may be '*' // <interface-name>:<port-number> format. Interface name may be '*'
// to bind to all the interfaces. // to bind to all the interfaces.
int set_address (const char *addr_); int set_address (const char *addr_);
// Open TCP listining socket.
int open ();
// Close the listening socket. // Close the listening socket.
int close (); int close ();
......
...@@ -41,10 +41,6 @@ int zmq::zmq_listener_t::set_address (const char *addr_) ...@@ -41,10 +41,6 @@ int zmq::zmq_listener_t::set_address (const char *addr_)
void zmq::zmq_listener_t::process_plug () void zmq::zmq_listener_t::process_plug ()
{ {
// Open the listening socket.
int rc = tcp_listener.open ();
zmq_assert (rc == 0);
// Start polling for incoming connections. // Start polling for incoming connections.
handle = add_fd (tcp_listener.get_fd ()); handle = add_fd (tcp_listener.get_fd ());
set_pollin (handle); set_pollin (handle);
......
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