1 /*
2 Copyright (c) 2007-2011 iMatix Corporation
3 Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4
5 This file is part of 0MQ.
6
7 0MQ is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 0MQ is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "../include/zmq.h"
22
23 #include "sub.hpp"
24
sub_t(class ctx_t * parent_,uint32_t tid_)25 zmq::sub_t::sub_t (class ctx_t *parent_, uint32_t tid_) :
26 xsub_t (parent_, tid_)
27 {
28 options.type = ZMQ_SUB;
29 }
30
~sub_t()31 zmq::sub_t::~sub_t ()
32 {
33 }
34
xsetsockopt(int option_,const void * optval_,size_t optvallen_)35 int zmq::sub_t::xsetsockopt (int option_, const void *optval_,
36 size_t optvallen_)
37 {
38 if (option_ != ZMQ_SUBSCRIBE && option_ != ZMQ_UNSUBSCRIBE) {
39 errno = EINVAL;
40 return -1;
41 }
42
43 // Create the subscription message.
44 zmq_msg_t msg;
45 zmq_msg_init_size (&msg, optvallen_ + 1);
46 unsigned char *data = (unsigned char*) zmq_msg_data (&msg);
47 if (option_ == ZMQ_SUBSCRIBE)
48 *data = 1;
49 else if (option_ == ZMQ_UNSUBSCRIBE)
50 *data = 0;
51 memcpy (data + 1, optval_, optvallen_);
52
53 // Pass it further on in the stack.
54 int err = 0;
55 int rc = xsub_t::xsend (&msg, 0);
56 if (rc != 0)
57 err = errno;
58 zmq_msg_close (&msg);
59 if (rc != 0)
60 errno = err;
61 return rc;
62 }
63
xsend(zmq_msg_t * msg_,int options_)64 int zmq::sub_t::xsend (zmq_msg_t *msg_, int options_)
65 {
66 // Overload the XSUB's send.
67 errno = ENOTSUP;
68 return -1;
69 }
70
xhas_out()71 bool zmq::sub_t::xhas_out ()
72 {
73 // Overload the XSUB's send.
74 return false;
75 }
76