stream.cpp 9.06 KB
Newer Older
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 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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 GNU Lesser General Public
    License for more details.
25 26 27 28 29

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31
#include "macros.hpp"
32 33 34 35 36 37 38 39
#include "stream.hpp"
#include "pipe.hpp"
#include "wire.hpp"
#include "random.hpp"
#include "likely.hpp"
#include "err.hpp"

zmq::stream_t::stream_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
40
    routing_socket_base_t (parent_, tid_, sid_),
41 42 43 44 45
    _prefetched (false),
    _routing_id_sent (false),
    _current_out (NULL),
    _more_out (false),
    _next_integral_routing_id (generate_random ())
46 47
{
    options.type = ZMQ_STREAM;
48
    options.raw_socket = true;
49

50 51
    _prefetched_routing_id.init ();
    _prefetched_msg.init ();
52 53 54 55
}

zmq::stream_t::~stream_t ()
{
56 57
    _prefetched_routing_id.close ();
    _prefetched_msg.close ();
58 59
}

60 61 62
void zmq::stream_t::xattach_pipe (pipe_t *pipe_,
                                  bool subscribe_to_all_,
                                  bool locally_initiated_)
63
{
64
    LIBZMQ_UNUSED (subscribe_to_all_);
65 66 67

    zmq_assert (pipe_);

68
    identify_peer (pipe_, locally_initiated_);
69
    _fq.attach (pipe_);
70 71 72 73
}

void zmq::stream_t::xpipe_terminated (pipe_t *pipe_)
{
74
    erase_out_pipe (pipe_);
75
    _fq.pipe_terminated (pipe_);
76 77
    // TODO router_t calls pipe_->rollback() here; should this be done here as
    // well? then xpipe_terminated could be pulled up to routing_socket_base_t
78 79
    if (pipe_ == _current_out)
        _current_out = NULL;
80 81 82 83
}

void zmq::stream_t::xread_activated (pipe_t *pipe_)
{
84
    _fq.activated (pipe_);
85 86 87 88 89 90
}

int zmq::stream_t::xsend (msg_t *msg_)
{
    //  If this is the first part of the message it's the ID of the
    //  peer to send the message to.
91 92
    if (!_more_out) {
        zmq_assert (!_current_out);
93 94 95 96 97

        //  If we have malformed message (prefix with no subsequent message)
        //  then just silently ignore it.
        //  TODO: The connections should be killed instead.
        if (msg_->flags () & msg_t::more) {
98
            //  Find the pipe associated with the routing id stored in the prefix.
99 100
            //  If there's no such pipe return an error

101 102 103 104 105 106
            out_pipe_t *out_pipe = lookup_out_pipe (
              blob_t (static_cast<unsigned char *> (msg_->data ()),
                      msg_->size (), reference_tag_t ()));

            if (out_pipe) {
                _current_out = out_pipe->pipe;
107
                if (!_current_out->check_write ()) {
108
                    out_pipe->active = false;
109
                    _current_out = NULL;
110 111 112
                    errno = EAGAIN;
                    return -1;
                }
113
            } else {
114 115 116 117 118
                errno = EHOSTUNREACH;
                return -1;
            }
        }

119
        //  Expect one more message frame.
120
        _more_out = true;
121

122 123 124 125 126 127 128
        int rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
        return 0;
    }

129
    //  Ignore the MORE flag
130 131
    msg_->reset_flags (msg_t::more);

132
    //  This is the last part of the message.
133
    _more_out = false;
134 135

    //  Push the message into the pipe. If there's no out pipe, just drop it.
136
    if (_current_out) {
137 138 139
        // Close the remote connection if user has asked to do so
        // by sending zero length message.
        // Pending messages in the pipe will be dropped (on receiving term- ack)
140
        if (msg_->size () == 0) {
141
            _current_out->terminate (false);
142 143
            int rc = msg_->close ();
            errno_assert (rc == 0);
144 145
            rc = msg_->init ();
            errno_assert (rc == 0);
146
            _current_out = NULL;
147 148
            return 0;
        }
149
        bool ok = _current_out->write (msg_);
150
        if (likely (ok))
151 152
            _current_out->flush ();
        _current_out = NULL;
153
    } else {
154 155 156 157 158 159 160 161 162 163
        int rc = msg_->close ();
        errno_assert (rc == 0);
    }

    //  Detach the message from the data buffer.
    int rc = msg_->init ();
    errno_assert (rc == 0);

    return 0;
}
164

165 166 167
int zmq::stream_t::xsetsockopt (int option_,
                                const void *optval_,
                                size_t optvallen_)
168 169
{
    switch (option_) {
170
        case ZMQ_STREAM_NOTIFY:
171 172
            return do_setsockopt_int_as_bool_strict (optval_, optvallen_,
                                                     &options.raw_notify);
173

174
        default:
175 176
            return routing_socket_base_t::xsetsockopt (option_, optval_,
                                                       optvallen_);
177 178
    }
}
179

180 181
int zmq::stream_t::xrecv (msg_t *msg_)
{
182 183 184
    if (_prefetched) {
        if (!_routing_id_sent) {
            int rc = msg_->move (_prefetched_routing_id);
185
            errno_assert (rc == 0);
186
            _routing_id_sent = true;
187
        } else {
188
            int rc = msg_->move (_prefetched_msg);
189
            errno_assert (rc == 0);
190
            _prefetched = false;
191 192 193 194 195
        }
        return 0;
    }

    pipe_t *pipe = NULL;
196
    int rc = _fq.recvpipe (&_prefetched_msg, &pipe);
197 198 199 200
    if (rc != 0)
        return -1;

    zmq_assert (pipe != NULL);
201
    zmq_assert ((_prefetched_msg.flags () & msg_t::more) == 0);
202

203
    //  We have received a frame with TCP data.
204
    //  Rather than sending this frame, we keep it in prefetched
205
    //  buffer and send a frame with peer's ID.
206
    const blob_t &routing_id = pipe->get_routing_id ();
207
    rc = msg_->close ();
208
    errno_assert (rc == 0);
209
    rc = msg_->init_size (routing_id.size ());
210
    errno_assert (rc == 0);
211 212

    // forward metadata (if any)
213
    metadata_t *metadata = _prefetched_msg.metadata ();
214
    if (metadata)
215
        msg_->set_metadata (metadata);
216

217
    memcpy (msg_->data (), routing_id.data (), routing_id.size ());
218
    msg_->set_flags (msg_t::more);
219

220 221
    _prefetched = true;
    _routing_id_sent = true;
222 223 224 225 226 227 228

    return 0;
}

bool zmq::stream_t::xhas_in ()
{
    //  We may already have a message pre-fetched.
229
    if (_prefetched)
230 231 232 233 234
        return true;

    //  Try to read the next message.
    //  The message, if read, is kept in the pre-fetch buffer.
    pipe_t *pipe = NULL;
235
    int rc = _fq.recvpipe (&_prefetched_msg, &pipe);
236 237 238 239
    if (rc != 0)
        return false;

    zmq_assert (pipe != NULL);
240
    zmq_assert ((_prefetched_msg.flags () & msg_t::more) == 0);
241

242
    const blob_t &routing_id = pipe->get_routing_id ();
243
    rc = _prefetched_routing_id.init_size (routing_id.size ());
244
    errno_assert (rc == 0);
245 246

    // forward metadata (if any)
247
    metadata_t *metadata = _prefetched_msg.metadata ();
248
    if (metadata)
249
        _prefetched_routing_id.set_metadata (metadata);
250

251
    memcpy (_prefetched_routing_id.data (), routing_id.data (),
252
            routing_id.size ());
253
    _prefetched_routing_id.set_flags (msg_t::more);
254

255 256
    _prefetched = true;
    _routing_id_sent = false;
257 258 259 260 261 262

    return true;
}

bool zmq::stream_t::xhas_out ()
{
263
    //  In theory, STREAM socket is always ready for writing. Whether actual
264 265 266 267 268
    //  attempt to write succeeds depends on which pipe the message is going
    //  to be routed to.
    return true;
}

269
void zmq::stream_t::identify_peer (pipe_t *pipe_, bool locally_initiated_)
270
{
271
    //  Always assign routing id for raw-socket
272 273
    unsigned char buffer[5];
    buffer[0] = 0;
274
    blob_t routing_id;
275 276
    if (locally_initiated_ && connect_routing_id_is_set ()) {
        const std::string connect_routing_id = extract_connect_routing_id ();
277 278 279 280
        routing_id.set (
          reinterpret_cast<const unsigned char *> (connect_routing_id.c_str ()),
          connect_routing_id.length ());
        //  Not allowed to duplicate an existing rid
281
        zmq_assert (!has_out_pipe (routing_id));
282
    } else {
283
        put_uint32 (buffer + 1, _next_integral_routing_id++);
284
        routing_id.set (buffer, sizeof buffer);
285
        memcpy (options.routing_id, routing_id.data (), routing_id.size ());
286 287
        options.routing_id_size =
          static_cast<unsigned char> (routing_id.size ());
288
    }
289
    pipe_->set_router_socket_routing_id (routing_id);
290
    add_out_pipe (ZMQ_MOVE (routing_id), pipe_);
291
}