rep.cpp 3.17 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2012 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3
    Copyright (c) 2009-2011 250bpm s.r.o.
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 22 23
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "rep.hpp"
#include "err.hpp"
24
#include "msg.hpp"
25

26
zmq::rep_t::rep_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
27
    router_t (parent_, tid_, sid_),
28
    sending_reply (false),
29
    request_begins (true)
30
{
31
    options.type = ZMQ_REP;
32 33 34 35 36 37
}

zmq::rep_t::~rep_t ()
{
}

38
int zmq::rep_t::xsend (msg_t *msg_, int flags_)
39
{
40
    //  If we are in the middle of receiving a request, we cannot send reply.
41
    if (!sending_reply) {
42
        errno = EFSM;
43 44 45
        return -1;
    }

46
    bool more = msg_->flags () & msg_t::more ? true : false;
47

48
    //  Push message to the reply pipe.
49
    int rc = router_t::xsend (msg_, flags_);
50 51
    if (rc != 0)
        return rc;
52

53 54
    //  If the reply is complete flip the FSM back to request receiving state.
    if (!more)
55
        sending_reply = false;
Martin Sustrik's avatar
Martin Sustrik committed
56 57

    return 0;
58 59
}

60
int zmq::rep_t::xrecv (msg_t *msg_, int flags_)
61
{
62
    //  If we are in middle of sending a reply, we cannot receive next request.
63
    if (sending_reply) {
64
        errno = EFSM;
65 66 67
        return -1;
    }

68 69
    //  First thing to do when receiving a request is to copy all the labels
    //  to the reply pipe.
70
    if (request_begins) {
71
        while (true) {
72
            int rc = router_t::xrecv (msg_, flags_);
73 74
            if (rc != 0)
                return rc;
75 76
            zmq_assert (msg_->flags () & msg_t::more);
            bool bottom = (msg_->size () == 0);
77
            rc = router_t::xsend (msg_, flags_);
78
            errno_assert (rc == 0);
79 80
            if (bottom)
                break;
81
        }
82
        request_begins = false;
83
    }
84 85

    //  Get next message part to return to the user.
86
    int rc = router_t::xrecv (msg_, flags_);
87 88
    if (rc != 0)
       return rc;
89 90

    //  If whole request is read, flip the FSM to reply-sending state.
91
    if (!(msg_->flags () & msg_t::more)) {
92
        sending_reply = true;
93
        request_begins = true;
94
    }
95

96
    return 0;
97 98
}

99 100
bool zmq::rep_t::xhas_in ()
{
101 102 103
    if (sending_reply)
        return false;

104
    return router_t::xhas_in ();
105 106 107 108
}

bool zmq::rep_t::xhas_out ()
{
109 110 111
    if (!sending_reply)
        return false;

112
    return router_t::xhas_out ();
113
}
114

115 116
zmq::rep_session_t::rep_session_t (io_thread_t *io_thread_, bool connect_,
      socket_base_t *socket_, const options_t &options_,
117
      const address_t *addr_) :
118
    router_session_t (io_thread_, connect_, socket_, options_, addr_)
119 120 121 122 123 124 125
{
}

zmq::rep_session_t::~rep_session_t ()
{
}