server.cpp 5.16 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 "server.hpp"
#include "pipe.hpp"
#include "wire.hpp"
#include "random.hpp"
#include "likely.hpp"
#include "err.hpp"

zmq::server_t::server_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
somdoron's avatar
somdoron committed
40
    socket_base_t (parent_, tid_, sid_, true),
41
    next_routing_id (generate_random ())
42
{
43
    options.type = ZMQ_SERVER;
44 45 46
}

zmq::server_t::~server_t ()
47
{
48 49 50 51 52
    zmq_assert (outpipes.empty ());
}

void zmq::server_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
{
53
    LIBZMQ_UNUSED (subscribe_to_all_);
54

55
    zmq_assert (pipe_);
56

57
    uint32_t routing_id = next_routing_id++;
58
    if (!routing_id)
59
        routing_id = next_routing_id++;        //  Never use Routing ID zero
60

61
    pipe_->set_server_socket_routing_id (routing_id);
62 63
    //  Add the record into output pipes lookup table
    outpipe_t outpipe = {pipe_, true};
64
    bool ok = outpipes.ZMQ_MAP_INSERT_OR_EMPLACE (routing_id, outpipe).second;
65
    zmq_assert (ok);
66 67

    fq.attach (pipe_);
68 69 70 71
}

void zmq::server_t::xpipe_terminated (pipe_t *pipe_)
{
72
    outpipes_t::iterator it = outpipes.find (pipe_->get_server_socket_routing_id ());
73 74
    zmq_assert (it != outpipes.end ());
    outpipes.erase (it);
75
    fq.pipe_terminated (pipe_);
76 77 78
}

void zmq::server_t::xread_activated (pipe_t *pipe_)
79 80
{
    fq.activated (pipe_);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

void zmq::server_t::xwrite_activated (pipe_t *pipe_)
{
    outpipes_t::iterator it;
    for (it = outpipes.begin (); it != outpipes.end (); ++it)
        if (it->second.pipe == pipe_)
            break;

    zmq_assert (it != outpipes.end ());
    zmq_assert (!it->second.active);
    it->second.active = true;
}

int zmq::server_t::xsend (msg_t *msg_)
{
97 98 99 100 101
    //  SERVER sockets do not allow multipart data (ZMQ_SNDMORE)
    if (msg_->flags () & msg_t::more) {
        errno = EINVAL;
        return -1;
    }
102
    //  Find the pipe associated with the routing stored in the message.
103
    uint32_t routing_id = msg_->get_routing_id ();
104
    outpipes_t::iterator it = outpipes.find (routing_id);
105 106

    if (it != outpipes.end ()) {
107
        if (!it->second.pipe->check_write ()) {
108
            it->second.active = false;
109
            errno = EAGAIN;
110
            return -1;
111 112
        }
    }
113
    else {
114 115 116
        errno = EHOSTUNREACH;
        return -1;
    }
117

Constantin Rack's avatar
Constantin Rack committed
118
    //  Message might be delivered over inproc, so we reset routing id
119 120 121
    int rc = msg_->reset_routing_id ();
    errno_assert (rc == 0);

122 123 124
    bool ok = it->second.pipe->write (msg_);
    if (unlikely (!ok)) {
        // Message failed to send - we must close it ourselves.
125
        rc = msg_->close ();
126 127
        errno_assert (rc == 0);
    }
128 129 130
    else
        it->second.pipe->flush ();

131
    //  Detach the message from the data buffer.
132
    rc = msg_->init ();
133 134 135 136 137 138
    errno_assert (rc == 0);

    return 0;
}

int zmq::server_t::xrecv (msg_t *msg_)
139
{
140 141 142
    pipe_t *pipe = NULL;
    int rc = fq.recvpipe (msg_, &pipe);

143 144
    // Drop any messages with more flag
    while (rc == 0 && msg_->flags () & msg_t::more) {
145

146 147
        // drop all frames of the current multi-frame message
        rc = fq.recvpipe (msg_, NULL);
148

149
        while (rc == 0 && msg_->flags () & msg_t::more)
150
            rc = fq.recvpipe (msg_, NULL);
151

152
        // get the new message
153
        if (rc == 0)
154
            rc = fq.recvpipe (msg_, &pipe);
155
    }
156

157
    if (rc != 0)
158
        return rc;
159 160

    zmq_assert (pipe != NULL);
161

162
    uint32_t routing_id = pipe->get_server_socket_routing_id ();
163
    msg_->set_routing_id (routing_id);
164 165 166 167 168 169 170 171 172 173 174 175

    return 0;
}

bool zmq::server_t::xhas_in ()
{
    return fq.has_in ();
}

bool zmq::server_t::xhas_out ()
{
    //  In theory, SERVER socket is always ready for writing. Whether actual
176
    //  attempt to write succeeds depends on which pipe the message is going
177 178 179 180
    //  to be routed to.
    return true;
}

181
const zmq::blob_t &zmq::server_t::get_credential () const
182 183 184
{
    return fq.get_credential ();
}