zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
socket.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * This file is part of zmqpp.
7  * Copyright (c) 2011-2015 Contributors as noted in the AUTHORS file.
8  */
9 
17 #ifndef ZMQPP_SOCKET_HPP_
18 #define ZMQPP_SOCKET_HPP_
19 
20 #include <cstring>
21 #include <string>
22 #include <list>
23 
24 #include <zmq.h>
25 
26 #include "compatibility.hpp"
27 
28 #include "socket_mechanisms.hpp"
29 #include "socket_types.hpp"
30 #include "socket_options.hpp"
31 #include "signal.hpp"
32 
33 namespace zmqpp
34 {
35 
36 class context;
37 class message;
38 
39 typedef std::string endpoint_t;
42 
43 #if (ZMQ_VERSION_MAJOR >= 4)
44 namespace event
45 {
46  const int connected = ZMQ_EVENT_CONNECTED; /*<! connection established */
47  const int connect_delayed = ZMQ_EVENT_CONNECT_DELAYED; /*<! synchronous connect failed, it's being polled */
48  const int connect_retried = ZMQ_EVENT_CONNECT_RETRIED; /*<! asynchronous connect / reconnection attempt */
49  const int listening = ZMQ_EVENT_LISTENING; /*<! socket bound to an address, ready to accept connections */
50  const int bind_failed = ZMQ_EVENT_BIND_FAILED; /*<! socket could not bind to an address */
51  const int accepted = ZMQ_EVENT_ACCEPTED; /*<! connection accepted to bound interface */
52  const int accept_failed = ZMQ_EVENT_ACCEPT_FAILED; /*<! could not accept client connection */
53  const int closed = ZMQ_EVENT_CLOSED; /*<! connection closed */
54  const int close_failed = ZMQ_EVENT_CLOSE_FAILED; /*<! connection couldn't be closed */
55  const int disconnected = ZMQ_EVENT_DISCONNECTED; /*<! broken session */
56 
57  const int all = ZMQ_EVENT_ALL; /*<! all event flags */
58 }
59 #endif
60 
75 class socket
76 {
77 public:
78  static const int normal;
79 #if (ZMQ_VERSION_MAJOR == 2)
80  static const int dont_wait;
81 #else
82  static const int dont_wait;
83 #endif
84  static const int send_more;
85 #ifdef ZMQ_EXPERIMENTAL_LABELS
86  static const int send_label;
87 #endif
88 
95  socket(context_t const& context, socket_type const type);
96 
100  ~socket();
101 
108  socket_type type() const { return _type; }
109 
115  void bind(endpoint_t const& endpoint);
116 
117 #if (ZMQ_VERSION_MAJOR > 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR >= 2))
118 
123  void unbind(endpoint_t const& endpoint);
124 #endif
125 
136  void connect(endpoint_t const& endpoint);
137 
151  template<typename InputIterator>
152  void connect(InputIterator const& connections_begin, InputIterator const& connections_end)
153  {
154  for(InputIterator it = connections_begin; it != connections_end; ++it)
155  {
156  connect(*it);
157  }
158  }
159 
160 
166 #if (ZMQ_VERSION_MAJOR > 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR >= 2))
167  void disconnect(endpoint_t const& endpoint);
168 
177  template<typename InputIterator>
178  void disconnect(InputIterator const& disconnections_begin, InputIterator const& disconnections_end)
179  {
180  for(InputIterator it = disconnections_begin; it != disconnections_end; ++it)
181  {
182  disconnect(*it);
183  }
184  }
185 #endif
186 
191  void close();
192 
203  bool send(message_t& message, bool const dont_block = false);
204 
215  bool receive(message_t& message, bool const dont_block = false);
216 
227  bool send(std::string const& string, int const flags = normal);
228 
239  bool receive(std::string& string, int const flags = normal);
240 
251  bool send(signal sig, bool dont_block = false);
252 
253 
264  bool receive(signal &sig, bool dont_block = false);
265 
277  bool send_raw(char const* buffer, size_t const length, int const flags = normal);
278 
294  bool receive_raw(char* buffer, size_t& length, int const flags = normal);
295 
309  void subscribe(std::string const& topic);
310 
329  template<typename InputIterator>
330  void subscribe(InputIterator const& topics_begin, InputIterator const& topics_end)
331  {
332  for(InputIterator it = topics_begin; it != topics_end; ++it)
333  {
334  subscribe(*it);
335  }
336  }
337 
350  void unsubscribe(std::string const& topic);
351 
370  template<typename InputIterator>
371  void unsubscribe(InputIterator const& topics_begin, InputIterator const& topics_end)
372  {
373  for(InputIterator it = topics_begin; it != topics_end; ++it)
374  {
375  unsubscribe(*it);
376  }
377  }
378 
386  bool has_more_parts() const;
387 
394  void set(socket_option const option, int const value);
395 
404  void set(socket_option const option, bool const value);
405 
412  void set(socket_option const option, uint64_t const value);
413 
420  void set(socket_option const option, int64_t const value);
421 
429  void set(socket_option const option, char const* value, size_t const length);
430 
437  inline void set(socket_option const option, char const* value) { set(option, value, strlen(value)); }
438 
445  inline void set(socket_option const option, std::string const value) { set(option, value.c_str(), value.length()); }
446 
453  void get(socket_option const option, int& value) const;
454 
461  void get(socket_option const option, bool& value) const;
462 
469  void get(socket_option const option, uint64_t& value) const;
470 
477  void get(socket_option const option, int64_t& value) const;
478 
485  void get(socket_option const option, std::string& value) const;
486 
494  template<typename Type>
495  Type get(socket_option const option) const
496  {
497  Type value = Type();
498  get(option, value);
499  return value;
500  }
501 
502 #if (ZMQ_VERSION_MAJOR >= 4)
503 
511  void monitor(endpoint_t const monitor_endpoint, int events_required);
512 #endif
513 
521  signal wait();
522 
533  socket(socket&& source) NOEXCEPT;
534 
546  socket& operator=(socket&& source) NOEXCEPT;
547 
557  operator bool() const;
558 
564  operator void*() const;
565 
566 private:
567  void* _socket;
569  zmq_msg_t _recv_buffer;
570 
571  // No copy
572  socket(socket const&) NOEXCEPT ZMQPP_EXPLICITLY_DELETED;
573  socket& operator=(socket const&) NOEXCEPT ZMQPP_EXPLICITLY_DELETED;
574 
575  void track_message(message_t const&, uint32_t const, bool&);
576 };
577 
578 }
579 
580 #endif /* ZMQPP_SOCKET_HPP_ */
const int connected
Definition: socket.hpp:46
~socket()
This will close any socket still open before returning.
Definition: socket.cpp:56
bool has_more_parts() const
If the last receive part call to the socket resulted in a label or a non-terminating part of a multip...
Definition: socket.cpp:369
context context_t
context type
Definition: socket.hpp:40
void close()
Closes the internal zmq socket and marks this instance as invalid.
Definition: socket.cpp:118
socket_type type() const
Get the type of the socket, this works on zmqpp types and not the zmq internal types.
Definition: socket.hpp:108
static const int dont_wait
Definition: socket.hpp:82
const int all
Definition: socket.hpp:57
signal wait()
Wait on signal, this is useful to coordinate thread.
Definition: socket.cpp:846
void disconnect(endpoint_t const &endpoint)
Disconnects a previously connected endpoint.
Definition: socket.cpp:107
#define ZMQPP_EXPLICITLY_DELETED
Definition: compatibility.hpp:100
void * _socket
Definition: socket.hpp:567
C++ wrapper around zmq.
Definition: actor.cpp:29
void unsubscribe(InputIterator const &topics_begin, InputIterator const &topics_end)
Unsubscribe from a topic.
Definition: socket.hpp:371
static const int normal
Definition: socket.hpp:78
zmq_msg_t _recv_buffer
Definition: socket.hpp:569
The socket class represents the zmq sockets.
Definition: socket.hpp:75
void connect(InputIterator const &connections_begin, InputIterator const &connections_end)
Asynchronously connects to multiple endpoints.
Definition: socket.hpp:152
void monitor(endpoint_t const monitor_endpoint, int events_required)
Attach a monitor to this socket that will send events over inproc to the specified endpoint...
Definition: socket.cpp:835
void unsubscribe(std::string const &topic)
Unsubscribe from a topic.
Definition: socket.cpp:364
const int connect_retried
Definition: socket.hpp:48
bool receive_raw(char *buffer, size_t &length, int const flags=normal)
Definition: socket.cpp:331
const int connect_delayed
Definition: socket.hpp:47
static const int send_more
Definition: socket.hpp:84
const int closed
Definition: socket.hpp:53
void bind(endpoint_t const &endpoint)
Asynchronously binds to an endpoint.
Definition: socket.cpp:74
ZMQPP_COMPARABLE_ENUM socket_option
possible Socket options in zmq
Definition: socket_options.hpp:28
#define NOEXCEPT
Definition: compatibility.hpp:104
const int close_failed
Definition: socket.hpp:54
void track_message(message_t const &, uint32_t const, bool &)
Definition: socket.cpp:826
message message_t
message type
Definition: socket.hpp:41
void unbind(endpoint_t const &endpoint)
Unbinds from a previously bound endpoint.
Definition: socket.cpp:85
bool send_raw(char const *buffer, size_t const length, int const flags=normal)
Sends the byte data pointed to by buffer as the next part of the message.
Definition: socket.cpp:298
void connect(endpoint_t const &endpoint)
Asynchronously connects to an endpoint.
Definition: socket.cpp:96
void subscribe(InputIterator const &topics_begin, InputIterator const &topics_end)
Subscribe to a topic.
Definition: socket.hpp:330
void set(socket_option const option, int const value)
Set the value of an option in the underlaying zmq socket.
Definition: socket.cpp:376
socket(context_t const &context, socket_type const type)
Create a socket for a given type.
Definition: socket.cpp:42
void set(socket_option const option, std::string const value)
Set the value of an option in the underlaying zmq socket.
Definition: socket.hpp:445
The context class represents internal zmq context and io threads.
Definition: context.hpp:46
socket_type _type
Definition: socket.hpp:568
a zmq message with optional multipart support
Definition: message.hpp:43
std::string endpoint_t
endpoint type
Definition: socket.hpp:37
signal
Signal is a 8 bytes integer.
Definition: signal.hpp:23
ZMQPP_COMPARABLE_ENUM socket_type
Socket types allowed by zmq.
Definition: socket_types.hpp:30
void subscribe(std::string const &topic)
Subscribe to a topic.
Definition: socket.cpp:359
bool send(message_t &message, bool const dont_block=false)
Sends the message over the connection, this may be a multipart message.
Definition: socket.cpp:148
void disconnect(InputIterator const &disconnections_begin, InputIterator const &disconnections_end)
Disconnects from multiple previously connected endpoints.
Definition: socket.hpp:178
void set(socket_option const option, char const *value)
Set the value of an option in the underlaying zmq socket.
Definition: socket.hpp:437
const int bind_failed
Definition: socket.hpp:50
const int accept_failed
Definition: socket.hpp:52
bool receive(message_t &message, bool const dont_block=false)
Gets a message from the connection, this may be a multipart message.
Definition: socket.cpp:209
const int listening
Definition: socket.hpp:49
const int accepted
Definition: socket.hpp:51
const int disconnected
Definition: socket.hpp:55