zmqpp 4.1.2
C++ bindings for 0mq (libzmq)
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
33namespace zmqpp
34{
35
36class context;
37class message;
38
39typedef std::string endpoint_t;
42
43#if (ZMQ_VERSION_MAJOR >= 4)
44namespace 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 const int monitor_stopped = ZMQ_EVENT_MONITOR_STOPPED; /*<! this monitor socket will not receive event anymore */
57 const int all = ZMQ_EVENT_ALL; /*<! all event flags */
58}
59#endif
60
76{
77public:
78 enum {
79 normal = 0,
80#if (ZMQ_VERSION_MAJOR == 2)
81 dont_wait = ZMQ_NOBLOCK,
82#else
83 dont_wait = ZMQ_DONTWAIT,
84#endif
85 send_more = ZMQ_SNDMORE,
86#ifdef ZMQ_EXPERIMENTAL_LABELS
87 send_label = ZMQ_SNDLABEL
88#endif
89 };
90
97 socket(context_t const& context, socket_type const type);
98
102 ~socket();
103
110 socket_type type() const { return _type; }
111
117 void bind(endpoint_t const& endpoint);
118
119#if (ZMQ_VERSION_MAJOR > 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR >= 2))
125 void unbind(endpoint_t const& endpoint);
126#endif
127
138 void connect(endpoint_t const& endpoint);
139
153 template<typename InputIterator>
154 void connect(InputIterator const& connections_begin, InputIterator const& connections_end)
155 {
156 for(InputIterator it = connections_begin; it != connections_end; ++it)
157 {
158 connect(*it);
159 }
160 }
161
162
168#if (ZMQ_VERSION_MAJOR > 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR >= 2))
169 void disconnect(endpoint_t const& endpoint);
170
179 template<typename InputIterator>
180 void disconnect(InputIterator const& disconnections_begin, InputIterator const& disconnections_end)
181 {
182 for(InputIterator it = disconnections_begin; it != disconnections_end; ++it)
183 {
184 disconnect(*it);
185 }
186 }
187#endif
188
193 void close();
194
207 bool send(message_t& message, bool const dont_block = false);
208
221 bool receive(message_t& message, bool const dont_block = false);
222
235 bool send(std::string const& string, bool const dont_block = false);
236
249 bool receive(std::string& string, bool const dont_block = false);
250
263 bool send(signal sig, bool dont_block = false);
264
277 bool receive(signal &sig, bool dont_block = false);
278
291 ZMQPP_DEPRECATED("prefer using zmqpp::message for multipart messages")
292 bool send(std::string const& string, int const flags);
293
306 ZMQPP_DEPRECATED("prefer using zmqpp::message for multipart messages")
307 bool receive(std::string& string, int const flags);
308
322 bool send_raw(char const* buffer, size_t const length, int const flags = normal);
323
341 bool receive_raw(char* buffer, size_t& length, int const flags = normal);
342
356 void subscribe(std::string const& topic);
357
376 template<typename InputIterator>
377 void subscribe(InputIterator const& topics_begin, InputIterator const& topics_end)
378 {
379 for(InputIterator it = topics_begin; it != topics_end; ++it)
380 {
381 subscribe(*it);
382 }
383 }
384
397 void unsubscribe(std::string const& topic);
398
417 template<typename InputIterator>
418 void unsubscribe(InputIterator const& topics_begin, InputIterator const& topics_end)
419 {
420 for(InputIterator it = topics_begin; it != topics_end; ++it)
421 {
422 unsubscribe(*it);
423 }
424 }
425
433 bool has_more_parts() const;
434
441 void set(socket_option const option, int const value);
442
451 void set(socket_option const option, bool const value);
452
459 void set(socket_option const option, uint64_t const value);
460
467 void set(socket_option const option, int64_t const value);
468
476 void set(socket_option const option, char const* value, size_t const length);
477
484 inline void set(socket_option const option, char const* value) { set(option, value, strlen(value)); }
485
492 inline void set(socket_option const option, std::string const value) { set(option, value.c_str(), value.length()); }
493
500 void get(socket_option const option, int& value) const;
501
508 void get(socket_option const option, bool& value) const;
509
516 void get(socket_option const option, uint64_t& value) const;
517
524 void get(socket_option const option, int64_t& value) const;
525
532 void get(socket_option const option, std::string& value) const;
533
541 template<typename Type>
542 Type get(socket_option const option) const
543 {
544 Type value = Type();
545 get(option, value);
546 return value;
547 }
548
549#if (ZMQ_VERSION_MAJOR >= 4)
558 void monitor(endpoint_t const monitor_endpoint, int events_required);
559
564 void unmonitor();
565#endif
566
574 signal wait();
575
586 socket(socket&& source) NOEXCEPT;
587
599 socket& operator=(socket&& source) NOEXCEPT;
600
610 operator bool() const;
611
617 operator void*() const;
618
619private:
620 void* _socket;
622 zmq_msg_t _recv_buffer;
623
624 // No copy
627
628 void track_message(message_t const&, uint32_t const, bool&);
629};
630
631}
632
633#endif /* ZMQPP_SOCKET_HPP_ */
The context class represents internal zmq context and io threads.
Definition: context.hpp:47
a zmq message with optional multipart support
Definition: message.hpp:44
The socket class represents the zmq sockets.
Definition: socket.hpp:76
void unsubscribe(InputIterator const &topics_begin, InputIterator const &topics_end)
Unsubscribe from a topic.
Definition: socket.hpp:418
socket & operator=(socket const &) NOEXCEPT ZMQPP_EXPLICITLY_DELETED
socket(socket const &) NOEXCEPT ZMQPP_EXPLICITLY_DELETED
zmq_msg_t _recv_buffer
Definition: socket.hpp:622
void set(socket_option const option, std::string const value)
Set the value of an option in the underlaying zmq socket.
Definition: socket.hpp:492
socket_type type() const
Get the type of the socket, this works on zmqpp types and not the zmq internal types.
Definition: socket.hpp:110
socket_type _type
Definition: socket.hpp:621
void connect(InputIterator const &connections_begin, InputIterator const &connections_end)
Asynchronously connects to multiple endpoints.
Definition: socket.hpp:154
void disconnect(InputIterator const &disconnections_begin, InputIterator const &disconnections_end)
Disconnects from multiple previously connected endpoints.
Definition: socket.hpp:180
Type get(socket_option const option) const
For those that don't want to get into a referenced value this templated method will return the value ...
Definition: socket.hpp:542
void * _socket
Definition: socket.hpp:620
void set(socket_option const option, char const *value)
Set the value of an option in the underlaying zmq socket.
Definition: socket.hpp:484
#define NOEXCEPT
Definition: compatibility.hpp:122
#define ZMQPP_DEPRECATED(reason)
Definition: compatibility.hpp:118
#define ZMQPP_EXPORT
Definition: compatibility.hpp:39
#define ZMQPP_EXPLICITLY_DELETED
Definition: compatibility.hpp:107
STL namespace.
const int accept_failed
Definition: socket.hpp:52
const int monitor_stopped
Definition: socket.hpp:56
const int connect_delayed
Definition: socket.hpp:47
const int listening
Definition: socket.hpp:49
const int connected
Definition: socket.hpp:46
const int close_failed
Definition: socket.hpp:54
const int connect_retried
Definition: socket.hpp:48
const int all
Definition: socket.hpp:57
const int closed
Definition: socket.hpp:53
const int bind_failed
Definition: socket.hpp:50
const int accepted
Definition: socket.hpp:51
const int disconnected
Definition: socket.hpp:55
C++ wrapper around zmq.
Definition: actor.cpp:30
message message_t
message type
Definition: socket.hpp:41
std::string endpoint_t
endpoint type
Definition: socket.hpp:37
signal
Signal is a 8 bytes integer.
Definition: signal.hpp:25
ZMQPP_COMPARABLE_ENUM socket_type
Socket types allowed by zmq.
Definition: socket_types.hpp:30
ZMQPP_COMPARABLE_ENUM socket_option
possible Socket options in zmq
Definition: socket_options.hpp:28
context context_t
context type
Definition: socket.hpp:40