req.cpp 4.56 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) 2011 VMware, Inc.
5
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
6 7 8 9

    This file is part of 0MQ.

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

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

#include "req.hpp"
#include "err.hpp"
25
#include "msg.hpp"
26 27 28
#include "wire.hpp"
#include "random.hpp"
#include "likely.hpp"
29

30
zmq::req_t::req_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
31
    dealer_t (parent_, tid_, sid_),
32
    receiving_reply (false),
33
    message_begins (true)
34
{
35
    options.type = ZMQ_REQ;
36 37 38 39
}

zmq::req_t::~req_t ()
{
Martin Hurton's avatar
Martin Hurton committed
40 41
}

42
int zmq::req_t::xsend (msg_t *msg_, int flags_)
43 44 45
{
    //  If we've sent a request and we still haven't got the reply,
    //  we can't send another request.
46
    if (receiving_reply) {
47
        errno = EFSM;
48 49 50
        return -1;
    }

51
    //  First part of the request is the request identity.
52
    if (message_begins) {
53 54
        msg_t bottom;
        int rc = bottom.init ();
55
        errno_assert (rc == 0);
56
        bottom.set_flags (msg_t::more);
57
        rc = dealer_t::xsend (&bottom, 0);
58
        if (rc != 0)
59
            return -1;
60
        message_begins = false;
61 62
    }

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

65
    int rc = dealer_t::xsend (msg_, flags_);
66 67
    if (rc != 0)
        return rc;
68

69 70 71 72
    //  If the request was fully sent, flip the FSM into reply-receiving state.
    if (!more) {
        receiving_reply = true;
        message_begins = true;
73
    }
74 75 76 77

    return 0;
}

78
int zmq::req_t::xrecv (msg_t *msg_, int flags_)
79 80
{
    //  If request wasn't send, we can't wait for reply.
81
    if (!receiving_reply) {
82
        errno = EFSM;
83 84 85
        return -1;
    }

86
    //  First part of the reply should be the original request ID.
87
    if (message_begins) {
88
        int rc = dealer_t::xrecv (msg_, flags_);
89 90
        if (rc != 0)
            return rc;
91 92

        // TODO: This should also close the connection with the peer!
93
        if (unlikely (!(msg_->flags () & msg_t::more) || msg_->size () != 0)) {
94
            while (true) {
95
                int rc = dealer_t::xrecv (msg_, flags_);
96
                errno_assert (rc == 0);
97
                if (!(msg_->flags () & msg_t::more))
98 99 100 101 102 103 104
                    break;
            }
            msg_->close ();
            msg_->init ();
            errno = EAGAIN;
            return -1;
        }
105

106
        message_begins = false;
107
    }
108

109
    int rc = dealer_t::xrecv (msg_, flags_);
110 111 112 113
    if (rc != 0)
        return rc;

    //  If the reply is fully received, flip the FSM into request-sending state.
114
    if (!(msg_->flags () & msg_t::more)) {
115
        receiving_reply = false;
116
        message_begins = true;
117 118
    }

119 120 121
    return 0;
}

122 123
bool zmq::req_t::xhas_in ()
{
124 125
    //  TODO: Duplicates should be removed here.

126
    if (!receiving_reply)
127
        return false;
128

129
    return dealer_t::xhas_in ();
130 131 132 133
}

bool zmq::req_t::xhas_out ()
{
134
    if (receiving_reply)
135 136
        return false;

137
    return dealer_t::xhas_out ();
138 139
}

140 141
zmq::req_session_t::req_session_t (io_thread_t *io_thread_, bool connect_,
      socket_base_t *socket_, const options_t &options_,
142
      const address_t *addr_) :
143
    dealer_session_t (io_thread_, connect_, socket_, options_, addr_),
144
    state (identity)
145 146 147 148 149
{
}

zmq::req_session_t::~req_session_t ()
{
150
    state = options.recv_identity ? identity : bottom;
151
}
152

153 154
int zmq::req_session_t::write (msg_t *msg_)
{
155 156
    switch (state) {
    case bottom:
157
        if (msg_->flags () == msg_t::more && msg_->size () == 0) {
158
            state = body;
159
            return dealer_session_t::write (msg_);
160
        }
161 162
        break;
    case body:
163
        if (msg_->flags () == msg_t::more)
164
            return dealer_session_t::write (msg_);
165
        if (msg_->flags () == 0) {
166
            state = bottom;
167
            return dealer_session_t::write (msg_);
168
        }
169 170 171 172
        break;
    case identity:
        if (msg_->flags () == 0) {
            state = bottom;
173
            return dealer_session_t::write (msg_);
174 175
        }
        break;
176 177 178 179 180
    }
    errno = EFAULT;
    return -1;
}