2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the Lesser GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#ifndef __ZMQ_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#include "zmq.h"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-28 08:41:28 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2009-08-03 11:59:55 +02:00
|
|
|
#include <exception>
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
typedef zmq_free_fn free_fn;
|
2009-12-05 11:20:57 +01:00
|
|
|
typedef zmq_pollitem_t pollitem_t;
|
|
|
|
|
2009-08-29 09:41:50 +02:00
|
|
|
class error_t : public std::exception
|
2009-08-03 11:59:55 +02:00
|
|
|
{
|
2009-08-29 09:41:50 +02:00
|
|
|
public:
|
2009-08-03 11:59:55 +02:00
|
|
|
|
2010-02-23 23:43:31 +01:00
|
|
|
error_t () : errnum (zmq_errno ()) {}
|
2009-08-03 11:59:55 +02:00
|
|
|
|
2009-08-29 09:41:50 +02:00
|
|
|
virtual const char *what () const throw ()
|
2009-08-03 11:59:55 +02:00
|
|
|
{
|
2009-09-22 13:00:05 +02:00
|
|
|
return zmq_strerror (errnum);
|
2009-08-03 11:59:55 +02:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:41:50 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
int errnum;
|
2009-08-03 11:59:55 +02:00
|
|
|
};
|
|
|
|
|
2010-01-22 13:29:46 +01:00
|
|
|
inline int poll (zmq_pollitem_t *items_, int nitems_, long timeout_ = -1)
|
|
|
|
{
|
|
|
|
int rc = zmq_poll (items_, nitems_, timeout_);
|
|
|
|
if (rc < 0)
|
|
|
|
throw error_t ();
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-04-07 08:20:01 +02:00
|
|
|
inline void device (int device_, void * insocket_, void* outsocket_)
|
|
|
|
{
|
|
|
|
int rc = zmq_device (device_, insocket_, outsocket_);
|
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
class message_t : private zmq_msg_t
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
friend class socket_t;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2009-12-05 11:20:57 +01:00
|
|
|
inline message_t ()
|
|
|
|
{
|
|
|
|
int rc = zmq_msg_init (this);
|
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline message_t (size_t size_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_msg_init_size (this, size_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 15:08:37 +01:00
|
|
|
inline message_t (void *data_, size_t size_, free_fn *ffn_,
|
|
|
|
void *hint_ = NULL)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-12-16 15:08:37 +01:00
|
|
|
int rc = zmq_msg_init_data (this, data_, size_, ffn_, hint_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ~message_t ()
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_msg_close (this);
|
2010-09-29 10:09:03 +02:00
|
|
|
assert (rc == 0);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-12-05 11:20:57 +01:00
|
|
|
inline void rebuild ()
|
|
|
|
{
|
|
|
|
int rc = zmq_msg_close (this);
|
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
|
|
|
rc = zmq_msg_init (this);
|
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
|
|
|
}
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
inline void rebuild (size_t size_)
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_msg_close (this);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-08-03 11:30:13 +02:00
|
|
|
rc = zmq_msg_init_size (this, size_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 15:08:37 +01:00
|
|
|
inline void rebuild (void *data_, size_t size_, free_fn *ffn_,
|
|
|
|
void *hint_ = NULL)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_msg_close (this);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-12-16 15:08:37 +01:00
|
|
|
rc = zmq_msg_init_data (this, data_, size_, ffn_, hint_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-12-05 11:20:57 +01:00
|
|
|
inline void move (message_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
int rc = zmq_msg_move (this, (zmq_msg_t*) msg_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-12-05 11:20:57 +01:00
|
|
|
inline void copy (message_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
int rc = zmq_msg_copy (this, (zmq_msg_t*) msg_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void *data ()
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
return zmq_msg_data (this);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t size ()
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
return zmq_msg_size (this);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Disable implicit message copying, so that users won't use shared
|
|
|
|
// messages (less efficient) without being aware of the fact.
|
|
|
|
message_t (const message_t&);
|
|
|
|
void operator = (const message_t&);
|
|
|
|
};
|
|
|
|
|
|
|
|
class context_t
|
|
|
|
{
|
|
|
|
friend class socket_t;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2010-06-04 15:24:06 +02:00
|
|
|
inline context_t (int io_threads_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2010-06-04 15:24:06 +02:00
|
|
|
ptr = zmq_init (io_threads_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (ptr == NULL)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ~context_t ()
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_term (ptr);
|
2009-07-29 12:07:54 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
context_t (const context_t&);
|
|
|
|
void operator = (const context_t&);
|
|
|
|
};
|
|
|
|
|
|
|
|
class socket_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-12-05 11:20:57 +01:00
|
|
|
inline socket_t (context_t &context_, int type_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
ptr = zmq_socket (context_.ptr, type_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (ptr == NULL)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ~socket_t ()
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_close (ptr);
|
2010-09-29 10:09:03 +02:00
|
|
|
assert (rc == 0);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-10-01 10:56:17 +02:00
|
|
|
inline operator void* ()
|
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
inline void setsockopt (int option_, const void *optval_,
|
|
|
|
size_t optvallen_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
int rc = zmq_setsockopt (ptr, option_, optval_, optvallen_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
2010-04-11 07:59:03 +02:00
|
|
|
inline void getsockopt (int option_, void *optval_,
|
|
|
|
size_t *optvallen_)
|
|
|
|
{
|
|
|
|
int rc = zmq_getsockopt (ptr, option_, optval_, optvallen_);
|
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
inline void bind (const char *addr_)
|
|
|
|
{
|
|
|
|
int rc = zmq_bind (ptr, addr_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
inline void connect (const char *addr_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 09:24:48 +02:00
|
|
|
int rc = zmq_connect (ptr, addr_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc != 0)
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:41:50 +02:00
|
|
|
inline bool send (message_t &msg_, int flags_ = 0)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_send (ptr, &msg_, flags_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc == 0)
|
|
|
|
return true;
|
2010-02-23 23:43:31 +01:00
|
|
|
if (rc == -1 && zmq_errno () == EAGAIN)
|
2009-08-29 09:41:50 +02:00
|
|
|
return false;
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:41:50 +02:00
|
|
|
inline bool recv (message_t *msg_, int flags_ = 0)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
int rc = zmq_recv (ptr, msg_, flags_);
|
2009-08-29 09:41:50 +02:00
|
|
|
if (rc == 0)
|
|
|
|
return true;
|
2010-02-23 23:43:31 +01:00
|
|
|
if (rc == -1 && zmq_errno () == EAGAIN)
|
2009-08-29 09:41:50 +02:00
|
|
|
return false;
|
|
|
|
throw error_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
socket_t (const socket_t&);
|
|
|
|
void operator = (const socket_t&);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|