1/*
2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#ifndef __ZMQ_XSUB_HPP_INCLUDED__
31#define __ZMQ_XSUB_HPP_INCLUDED__
32
33#include "socket_base.hpp"
34#include "session_base.hpp"
35#include "dist.hpp"
36#include "fq.hpp"
37#ifdef ZMQ_USE_RADIX_TREE
38#include "radix_tree.hpp"
39#else
40#include "trie.hpp"
41#endif
42
43namespace zmq
44{
45class ctx_t;
46class pipe_t;
47class io_thread_t;
48
49class xsub_t : public socket_base_t
50{
51 public:
52 xsub_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
53 ~xsub_t ();
54
55 protected:
56 // Overrides of functions from socket_base_t.
57 void xattach_pipe (zmq::pipe_t *pipe_,
58 bool subscribe_to_all_,
59 bool locally_initiated_);
60 int xsetsockopt (int option_, const void *optval_, size_t optvallen_);
61 int xsend (zmq::msg_t *msg_);
62 bool xhas_out ();
63 int xrecv (zmq::msg_t *msg_);
64 bool xhas_in ();
65 void xread_activated (zmq::pipe_t *pipe_);
66 void xwrite_activated (zmq::pipe_t *pipe_);
67 void xhiccuped (pipe_t *pipe_);
68 void xpipe_terminated (zmq::pipe_t *pipe_);
69
70 private:
71 // Check whether the message matches at least one subscription.
72 bool match (zmq::msg_t *msg_);
73
74 // Function to be applied to the trie to send all the subsciptions
75 // upstream.
76 static void
77 send_subscription (unsigned char *data_, size_t size_, void *arg_);
78
79 // Fair queueing object for inbound pipes.
80 fq_t _fq;
81
82 // Object for distributing the subscriptions upstream.
83 dist_t _dist;
84
85 // The repository of subscriptions.
86#ifdef ZMQ_USE_RADIX_TREE
87 radix_tree_t _subscriptions;
88#else
89 trie_t _subscriptions;
90#endif
91
92 // If true, 'message' contains a matching message to return on the
93 // next recv call.
94 bool _has_message;
95 msg_t _message;
96
97 // If true, part of a multipart message was already sent, but
98 // there are following parts still waiting.
99 bool _more_send;
100
101 // If true, part of a multipart message was already received, but
102 // there are following parts still waiting.
103 bool _more_recv;
104
105 // If true, subscribe and cancel messages are processed for the rest
106 // of multipart message.
107 bool _process_subscribe;
108
109 // This option is enabled with ZMQ_ONLY_FIRST_SUBSCRIBE.
110 // If true, messages following subscribe/unsubscribe in a multipart
111 // message are treated as user data regardless of the first byte.
112 bool _only_first_subscribe;
113
114 ZMQ_NON_COPYABLE_NOR_MOVABLE (xsub_t)
115};
116}
117
118#endif
119