libzmq/bindings/cpp/zmq.hpp

263 lines
6.1 KiB
C++
Raw Normal View History

2009-07-29 12:07:54 +02:00
/*
Copyright (c) 2007-2009 FastMQ Inc.
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
#include <assert.h>
#include <errno.h>
2009-08-29 09:41:50 +02:00
#include <string.h>
#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;
inline int poll (zmq_pollitem_t *items_, int nitems_)
{
return zmq_poll (items_, nitems_);
}
2009-07-29 12:07:54 +02:00
2009-08-29 09:41:50 +02:00
class error_t : public std::exception
{
2009-08-29 09:41:50 +02:00
public:
2009-08-29 09:41:50 +02:00
error_t () : errnum (errno) {}
2009-08-29 09:41:50 +02:00
virtual const char *what () const throw ()
{
return zmq_strerror (errnum);
}
2009-08-29 09:41:50 +02:00
private:
int errnum;
};
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-05 11:20:57 +01:00
inline message_t (void *data_, size_t size_, free_fn *ffn_)
2009-07-29 12:07:54 +02:00
{
2009-08-03 11:30:13 +02:00
int rc = zmq_msg_init_data (this, data_, size_, ffn_);
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);
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 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
}
inline void rebuild (void *data_, size_t size_, free_fn *ffn_)
{
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_data (this, data_, size_, ffn_);
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:
2009-09-20 10:14:21 +02:00
inline context_t (int app_threads_, int io_threads_, int flags_ = 0)
2009-07-29 12:07:54 +02:00
{
2009-09-20 10:14:21 +02:00
ptr = zmq_init (app_threads_, io_threads_, flags_);
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);
2009-08-29 09:41:50 +02:00
if (rc != 0)
throw error_t ();
2009-07-29 12:07:54 +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 ();
}
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
}
inline void connect (const char *addr_)
2009-07-29 12:07:54 +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;
if (rc == -1 && errno == EAGAIN)
return false;
throw error_t ();
2009-07-29 12:07:54 +02:00
}
inline void flush ()
{
2009-08-03 11:30:13 +02:00
int rc = zmq_flush (ptr);
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 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;
if (rc == -1 && errno == EAGAIN)
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