dgram.cpp 4.54 KB
Newer Older
1
/*
Bitiquinho's avatar
Bitiquinho committed
2
    Copyright (c) 2016 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

    This file is part of libzmq, the ZeroMQ core engine in C++.

    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
    (at your option) any later version.

    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.

    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/>.
*/

#include "precompiled.hpp"
#include "macros.hpp"
#include "dgram.hpp"
#include "pipe.hpp"
#include "wire.hpp"
#include "random.hpp"
#include "likely.hpp"
#include "err.hpp"

zmq::dgram_t::dgram_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
    socket_base_t (parent_, tid_, sid_),
41 42 43
    _pipe (NULL),
    _last_in (NULL),
    _more_out (false)
44 45 46 47 48 49 50
{
    options.type = ZMQ_DGRAM;
    options.raw_socket = true;
}

zmq::dgram_t::~dgram_t ()
{
51
    zmq_assert (!_pipe);
52 53 54 55
}

void zmq::dgram_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
{
56
    LIBZMQ_UNUSED (subscribe_to_all_);
57 58 59

    zmq_assert (pipe_);

60 61
    //  ZMQ_DGRAM socket can only be connected to a single peer.
    //  The socket rejects any further connection requests.
62 63
    if (_pipe == NULL)
        _pipe = pipe_;
64 65
    else
        pipe_->terminate (false);
66 67 68 69
}

void zmq::dgram_t::xpipe_terminated (pipe_t *pipe_)
{
70 71 72 73
    if (pipe_ == _pipe) {
        if (_last_in == _pipe) {
            _saved_credential.set_deep_copy (_last_in->get_credential ());
            _last_in = NULL;
74
        }
75
        _pipe = NULL;
76
    }
77 78
}

79
void zmq::dgram_t::xread_activated (pipe_t *)
80
{
81 82
    //  There's just one pipe. No lists of active and inactive pipes.
    //  There's nothing to do here.
83 84
}

85
void zmq::dgram_t::xwrite_activated (pipe_t *)
86
{
87 88
    //  There's just one pipe. No lists of active and inactive pipes.
    //  There's nothing to do here.
89 90 91 92
}

int zmq::dgram_t::xsend (msg_t *msg_)
{
93
    // If there's no out pipe, just drop it.
94
    if (!_pipe) {
95 96 97 98
        int rc = msg_->close ();
        errno_assert (rc == 0);
        return -1;
    }
99

100 101
    //  If this is the first part of the message it's the ID of the
    //  peer to send the message to.
102
    if (!_more_out) {
103
        if (!(msg_->flags () & msg_t::more)) {
104 105
            errno = EINVAL;
            return -1;
106
        }
107

108
        //  Expect one more message frame.
109
        _more_out = true;
110
    } else {
111 112 113 114 115 116
        //  dgram messages are two part only, reject part if more is set
        if (msg_->flags () & msg_t::more) {
            errno = EINVAL;
            return -1;
        }

117
        //  This is the last part of the message.
118
        _more_out = false;
119
    }
120

121
    // Push the message into the pipe.
122
    if (!_pipe->write (msg_)) {
123 124
        errno = EAGAIN;
        return -1;
125
    }
126

127
    if (!(msg_->flags () & msg_t::more))
128
        _pipe->flush ();
129 130 131 132 133 134 135 136

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

    return 0;
}

137
int zmq::dgram_t::xrecv (msg_t *msg_)
138
{
139 140 141
    //  Deallocate old content of the message.
    int rc = msg_->close ();
    errno_assert (rc == 0);
142

143
    if (!_pipe || !_pipe->read (msg_)) {
144 145 146
        //  Initialise the output parameter to be a 0-byte message.
        rc = msg_->init ();
        errno_assert (rc == 0);
147

148 149
        errno = EAGAIN;
        return -1;
150
    }
151
    _last_in = _pipe;
152

153 154 155 156 157
    return 0;
}

bool zmq::dgram_t::xhas_in ()
{
158
    if (!_pipe)
159 160
        return false;

161
    return _pipe->check_read ();
162 163 164 165
}

bool zmq::dgram_t::xhas_out ()
{
166
    if (!_pipe)
167 168
        return false;

169
    return _pipe->check_write ();
170 171
}

172
const zmq::blob_t &zmq::dgram_t::get_credential () const
173
{
174
    return _last_in ? _last_in->get_credential () : _saved_credential;
175
}