pair.cpp 2.99 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
8 9 10 11 12 13
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ 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
14
    GNU Lesser General Public License for more details.
15

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

Martin Sustrik's avatar
Martin Sustrik committed
20
#include "pair.hpp"
21
#include "err.hpp"
22
#include "pipe.hpp"
23
#include "msg.hpp"
24

25 26
zmq::pair_t::pair_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
    socket_base_t (parent_, tid_, sid_),
27 28
    pipe (NULL),
    last_in (NULL)
29
{
30
    options.type = ZMQ_PAIR;
31 32
}

Martin Sustrik's avatar
Martin Sustrik committed
33
zmq::pair_t::~pair_t ()
34
{
35
    zmq_assert (!pipe);
36 37
}

38
void zmq::pair_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
39
{
40 41
    // subscribe_to_all_ is unused
    (void)subscribe_to_all_;
42

43 44 45 46 47 48 49 50
    zmq_assert (pipe_ != NULL);

    //  ZMQ_PAIR socket can only be connected to a single peer.
    //  The socket rejects any further connection requests.
    if (pipe == NULL)
        pipe = pipe_;
    else
        pipe_->terminate (false);
51 52
}

53
void zmq::pair_t::xpipe_terminated (pipe_t *pipe_)
54
{
55 56 57 58 59
    if (pipe_ == pipe) {
        if (last_in == pipe) {
            saved_credential = last_in->get_credential ();
            last_in = NULL;
        }
60
        pipe = NULL;
61
    }
62 63
}

64
void zmq::pair_t::xread_activated (pipe_t *)
Martin Hurton's avatar
Martin Hurton committed
65
{
66 67
    //  There's just one pipe. No lists of active and inactive pipes.
    //  There's nothing to do here.
Martin Hurton's avatar
Martin Hurton committed
68 69
}

70
void zmq::pair_t::xwrite_activated (pipe_t *)
71
{
72 73
    //  There's just one pipe. No lists of active and inactive pipes.
    //  There's nothing to do here.
74 75
}

76
int zmq::pair_t::xsend (msg_t *msg_)
77
{
78
    if (!pipe || !pipe->write (msg_)) {
79 80 81 82
        errno = EAGAIN;
        return -1;
    }

83
    if (!(msg_->flags () & msg_t::more))
84
        pipe->flush ();
85 86

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

Martin Sustrik's avatar
Martin Sustrik committed
90
    return 0;
91 92
}

93
int zmq::pair_t::xrecv (msg_t *msg_)
94
{
95
    //  Deallocate old content of the message.
96 97
    int rc = msg_->close ();
    errno_assert (rc == 0);
98

99
    if (!pipe || !pipe->read (msg_)) {
100 101

        //  Initialise the output parameter to be a 0-byte message.
102 103
        rc = msg_->init ();
        errno_assert (rc == 0);
104

105 106 107
        errno = EAGAIN;
        return -1;
    }
108
    last_in = pipe;
Martin Sustrik's avatar
Martin Sustrik committed
109
    return 0;
110 111
}

Martin Sustrik's avatar
Martin Sustrik committed
112
bool zmq::pair_t::xhas_in ()
113
{
114
    if (!pipe)
115 116
        return false;

117
    return pipe->check_read ();
118 119
}

Martin Sustrik's avatar
Martin Sustrik committed
120
bool zmq::pair_t::xhas_out ()
121
{
122
    if (!pipe)
123 124
        return false;

125
    return pipe->check_write ();
126
}
127 128 129 130 131

zmq::blob_t zmq::pair_t::get_credential () const
{
    return last_in? last_in->get_credential (): saved_credential;
}