pair.cpp 2.99 KB
Newer Older
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
10 11 12 13 14 15
    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
16
    GNU Lesser General Public License for more details.
17

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

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

Martin Sustrik's avatar
Martin Sustrik committed
27 28
zmq::pair_t::pair_t (class ctx_t *parent_, uint32_t tid_) :
    socket_base_t (parent_, tid_),
29
    pipe (NULL)
30
{
31
    options.type = ZMQ_PAIR;
32 33
}

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

39
void zmq::pair_t::xattach_pipe (pipe_t *pipe_)
40
{
41 42
    zmq_assert (!pipe);
    pipe = pipe_;
43 44
}

45
void zmq::pair_t::xterminated (pipe_t *pipe_)
46
{
47 48
    zmq_assert (pipe_ == pipe);
    pipe = NULL;
49 50
}

51
void zmq::pair_t::xread_activated (pipe_t *pipe_)
Martin Hurton's avatar
Martin Hurton committed
52
{
53 54
    //  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
55 56
}

57
void zmq::pair_t::xwrite_activated (pipe_t *pipe_)
58
{
59 60
    //  There's just one pipe. No lists of active and inactive pipes.
    //  There's nothing to do here.
61 62
}

63
int zmq::pair_t::xsend (msg_t *msg_, int flags_)
64
{
65
    if (!pipe || !pipe->write (msg_)) {
66 67 68 69
        errno = EAGAIN;
        return -1;
    }

70
    if (!(flags_ & ZMQ_SNDMORE))
71
        pipe->flush ();
72 73

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

Martin Sustrik's avatar
Martin Sustrik committed
77
    return 0;
78 79
}

80
int zmq::pair_t::xrecv (msg_t *msg_, int flags_)
81
{
82
    //  Deallocate old content of the message.
83 84
    int rc = msg_->close ();
    errno_assert (rc == 0);
85

86
    if (!pipe || !pipe->read (msg_)) {
87 88

        //  Initialise the output parameter to be a 0-byte message.
89 90
        rc = msg_->init ();
        errno_assert (rc == 0);
91

92 93 94
        errno = EAGAIN;
        return -1;
    }
Martin Sustrik's avatar
Martin Sustrik committed
95
    return 0;
96 97
}

Martin Sustrik's avatar
Martin Sustrik committed
98
bool zmq::pair_t::xhas_in ()
99
{
100
    if (!pipe)
101 102
        return false;

103
    return pipe->check_read ();
104 105
}

Martin Sustrik's avatar
Martin Sustrik committed
106
bool zmq::pair_t::xhas_out ()
107
{
108
    if (!pipe)
109 110
        return false;

111 112 113
    msg_t msg;
    int rc = msg.init ();
    errno_assert (rc == 0);
114
    bool result = pipe->check_write (&msg);
115 116
    rc = msg.close ();
    errno_assert (rc == 0);
117
    return result;
118
}
119

120 121 122 123 124 125 126 127 128 129 130 131
zmq::pair_session_t::pair_session_t (io_thread_t *io_thread_, bool connect_,
      socket_base_t *socket_, const options_t &options_,
      const char *protocol_, const char *address_) :
    session_base_t (io_thread_, connect_, socket_, options_, protocol_,
         address_)
{
}

zmq::pair_session_t::~pair_session_t ()
{
}