dgram.cpp 4.38 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 56
void zmq::dgram_t::xattach_pipe (pipe_t *pipe_,
                                 bool subscribe_to_all_,
                                 bool locally_initiated_)
57
{
58
    LIBZMQ_UNUSED (subscribe_to_all_);
59
    LIBZMQ_UNUSED (locally_initiated_);
60 61 62

    zmq_assert (pipe_);

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

void zmq::dgram_t::xpipe_terminated (pipe_t *pipe_)
{
73 74 75
    if (pipe_ == _pipe) {
        if (_last_in == _pipe) {
            _last_in = NULL;
76
        }
77
        _pipe = NULL;
78
    }
79 80
}

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

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

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

102 103
    //  If this is the first part of the message it's the ID of the
    //  peer to send the message to.
104
    if (!_more_out) {
105
        if (!(msg_->flags () & msg_t::more)) {
106 107
            errno = EINVAL;
            return -1;
108
        }
109
    } else {
110 111 112 113 114
        //  dgram messages are two part only, reject part if more is set
        if (msg_->flags () & msg_t::more) {
            errno = EINVAL;
            return -1;
        }
115
    }
116

117
    // Push the message into the pipe.
118
    if (!_pipe->write (msg_)) {
119 120
        errno = EAGAIN;
        return -1;
121
    }
122

123
    if (!(msg_->flags () & msg_t::more))
124
        _pipe->flush ();
125

126 127
    // flip the more flag
    _more_out = !_more_out;
128

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

    return 0;
}

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

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

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

152 153 154 155 156
    return 0;
}

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

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

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

168
    return _pipe->check_write ();
169
}