stream.cpp 9.6 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 40
#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_) :
    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 58
    zmq_assert (_outpipes.empty ());
    _prefetched_routing_id.close ();
    _prefetched_msg.close ();
59 60
}

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

    zmq_assert (pipe_);

67
    identify_peer (pipe_);
68
    _fq.attach (pipe_);
69 70 71 72
}

void zmq::stream_t::xpipe_terminated (pipe_t *pipe_)
{
73 74 75 76 77 78
    outpipes_t::iterator it = _outpipes.find (pipe_->get_routing_id ());
    zmq_assert (it != _outpipes.end ());
    _outpipes.erase (it);
    _fq.pipe_terminated (pipe_);
    if (pipe_ == _current_out)
        _current_out = NULL;
79 80 81 82
}

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

void zmq::stream_t::xwrite_activated (pipe_t *pipe_)
{
    outpipes_t::iterator it;
89
    for (it = _outpipes.begin (); it != _outpipes.end (); ++it)
90 91 92
        if (it->second.pipe == pipe_)
            break;

93
    zmq_assert (it != _outpipes.end ());
94 95 96 97 98 99 100 101
    zmq_assert (!it->second.active);
    it->second.active = true;
}

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.
102 103
    if (!_more_out) {
        zmq_assert (!_current_out);
104 105 106 107 108

        //  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) {
109
            //  Find the pipe associated with the routing id stored in the prefix.
110
            //  If there's no such pipe return an error
111 112
            blob_t routing_id (static_cast<unsigned char *> (msg_->data ()),
                               msg_->size ());
113
            outpipes_t::iterator it = _outpipes.find (routing_id);
114

115 116 117
            if (it != _outpipes.end ()) {
                _current_out = it->second.pipe;
                if (!_current_out->check_write ()) {
118
                    it->second.active = false;
119
                    _current_out = NULL;
120 121 122
                    errno = EAGAIN;
                    return -1;
                }
123
            } else {
124 125 126 127 128
                errno = EHOSTUNREACH;
                return -1;
            }
        }

129
        //  Expect one more message frame.
130
        _more_out = true;
131

132 133 134 135 136 137 138
        int rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
        return 0;
    }

139
    //  Ignore the MORE flag
140 141
    msg_->reset_flags (msg_t::more);

142
    //  This is the last part of the message.
143
    _more_out = false;
144 145

    //  Push the message into the pipe. If there's no out pipe, just drop it.
146
    if (_current_out) {
147 148 149
        // 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)
150
        if (msg_->size () == 0) {
151
            _current_out->terminate (false);
152 153
            int rc = msg_->close ();
            errno_assert (rc == 0);
154 155
            rc = msg_->init ();
            errno_assert (rc == 0);
156
            _current_out = NULL;
157 158
            return 0;
        }
159
        bool ok = _current_out->write (msg_);
160
        if (likely (ok))
161 162
            _current_out->flush ();
        _current_out = NULL;
163
    } else {
164 165 166 167 168 169 170 171 172 173
        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;
}
174

175 176 177
int zmq::stream_t::xsetsockopt (int option_,
                                const void *optval_,
                                size_t optvallen_)
178 179
{
    switch (option_) {
180
        case ZMQ_CONNECT_ROUTING_ID:
181 182
            // TODO why isn't it possible to set an empty connect_routing_id
            //   (which is the default value)
183
            if (optval_ && optvallen_) {
184
                connect_routing_id.assign ((char *) optval_, optvallen_);
185 186 187
                return 0;
            }
            break;
188

189
        case ZMQ_STREAM_NOTIFY:
190 191
            return do_setsockopt_int_as_bool_strict (optval_, optvallen_,
                                                     &options.raw_notify);
192 193
            break;

194 195 196 197 198 199
        default:
            break;
    }
    errno = EINVAL;
    return -1;
}
200

201 202
int zmq::stream_t::xrecv (msg_t *msg_)
{
203 204 205
    if (_prefetched) {
        if (!_routing_id_sent) {
            int rc = msg_->move (_prefetched_routing_id);
206
            errno_assert (rc == 0);
207
            _routing_id_sent = true;
208
        } else {
209
            int rc = msg_->move (_prefetched_msg);
210
            errno_assert (rc == 0);
211
            _prefetched = false;
212 213 214 215 216
        }
        return 0;
    }

    pipe_t *pipe = NULL;
217
    int rc = _fq.recvpipe (&_prefetched_msg, &pipe);
218 219 220 221
    if (rc != 0)
        return -1;

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

224
    //  We have received a frame with TCP data.
225
    //  Rather than sending this frame, we keep it in prefetched
226
    //  buffer and send a frame with peer's ID.
227
    const blob_t &routing_id = pipe->get_routing_id ();
228
    rc = msg_->close ();
229
    errno_assert (rc == 0);
230
    rc = msg_->init_size (routing_id.size ());
231
    errno_assert (rc == 0);
232 233

    // forward metadata (if any)
234
    metadata_t *metadata = _prefetched_msg.metadata ();
235
    if (metadata)
236
        msg_->set_metadata (metadata);
237

238
    memcpy (msg_->data (), routing_id.data (), routing_id.size ());
239
    msg_->set_flags (msg_t::more);
240

241 242
    _prefetched = true;
    _routing_id_sent = true;
243 244 245 246 247 248 249

    return 0;
}

bool zmq::stream_t::xhas_in ()
{
    //  We may already have a message pre-fetched.
250
    if (_prefetched)
251 252 253 254 255
        return true;

    //  Try to read the next message.
    //  The message, if read, is kept in the pre-fetch buffer.
    pipe_t *pipe = NULL;
256
    int rc = _fq.recvpipe (&_prefetched_msg, &pipe);
257 258 259 260
    if (rc != 0)
        return false;

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

263
    const blob_t &routing_id = pipe->get_routing_id ();
264
    rc = _prefetched_routing_id.init_size (routing_id.size ());
265
    errno_assert (rc == 0);
266 267

    // forward metadata (if any)
268
    metadata_t *metadata = _prefetched_msg.metadata ();
269
    if (metadata)
270
        _prefetched_routing_id.set_metadata (metadata);
271

272
    memcpy (_prefetched_routing_id.data (), routing_id.data (),
273
            routing_id.size ());
274
    _prefetched_routing_id.set_flags (msg_t::more);
275

276 277
    _prefetched = true;
    _routing_id_sent = false;
278 279 280 281 282 283

    return true;
}

bool zmq::stream_t::xhas_out ()
{
284
    //  In theory, STREAM socket is always ready for writing. Whether actual
285 286 287 288 289
    //  attempt to write succeeds depends on which pipe the message is going
    //  to be routed to.
    return true;
}

290
void zmq::stream_t::identify_peer (pipe_t *pipe_)
291
{
292
    //  Always assign routing id for raw-socket
293 294
    unsigned char buffer[5];
    buffer[0] = 0;
295
    blob_t routing_id;
296
    if (connect_routing_id.length ()) {
297 298
        routing_id.set ((unsigned char *) connect_routing_id.c_str (),
                        connect_routing_id.length ());
299
        connect_routing_id.clear ();
300 301
        outpipes_t::iterator it = _outpipes.find (routing_id);
        zmq_assert (it == _outpipes.end ());
302
    } else {
303
        put_uint32 (buffer + 1, _next_integral_routing_id++);
304
        routing_id.set (buffer, sizeof buffer);
305
        memcpy (options.routing_id, routing_id.data (), routing_id.size ());
306 307
        options.routing_id_size =
          static_cast<unsigned char> (routing_id.size ());
308
    }
309
    pipe_->set_router_socket_routing_id (routing_id);
310 311
    //  Add the record into output pipes lookup table
    outpipe_t outpipe = {pipe_, true};
312
    const bool ok =
313
      _outpipes.ZMQ_MAP_INSERT_OR_EMPLACE (ZMQ_MOVE (routing_id), outpipe)
314
        .second;
315 316
    zmq_assert (ok);
}