2009-09-21 14:39:59 +02:00
|
|
|
/*
|
2011-03-02 16:30:40 +01:00
|
|
|
Copyright (c) 2007-2011 iMatix Corporation
|
|
|
|
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
|
2009-09-21 14:39:59 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
2010-10-30 15:08:28 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by
|
2009-09-21 14:39:59 +02:00
|
|
|
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
|
2010-10-30 15:08:28 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-09-21 14:39:59 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "req.hpp"
|
|
|
|
#include "err.hpp"
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2011-06-22 11:02:16 +02:00
|
|
|
#include "wire.hpp"
|
|
|
|
#include "random.hpp"
|
|
|
|
#include "likely.hpp"
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2010-11-05 16:38:52 +01:00
|
|
|
zmq::req_t::req_t (class ctx_t *parent_, uint32_t tid_) :
|
|
|
|
xreq_t (parent_, tid_),
|
2010-03-27 14:24:57 +01:00
|
|
|
receiving_reply (false),
|
2011-06-22 11:02:16 +02:00
|
|
|
message_begins (true),
|
2011-07-15 11:24:33 +02:00
|
|
|
request_id (generate_random ())
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2010-09-28 15:27:45 +02:00
|
|
|
options.type = ZMQ_REQ;
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::req_t::~req_t ()
|
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
}
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int zmq::req_t::xsend (msg_t *msg_, int flags_)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
|
|
|
// If we've sent a request and we still haven't got the reply,
|
|
|
|
// we can't send another request.
|
2010-03-27 14:24:57 +01:00
|
|
|
if (receiving_reply) {
|
2009-09-22 11:52:35 +02:00
|
|
|
errno = EFSM;
|
2009-09-21 14:39:59 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-22 11:02:16 +02:00
|
|
|
// First part of the request is the request identity.
|
2010-08-07 09:52:34 +02:00
|
|
|
if (message_begins) {
|
2011-04-21 22:27:48 +02:00
|
|
|
msg_t prefix;
|
2011-06-22 11:02:16 +02:00
|
|
|
int rc = prefix.init_size (4);
|
2011-04-21 22:27:48 +02:00
|
|
|
errno_assert (rc == 0);
|
2011-06-20 11:33:54 +02:00
|
|
|
prefix.set_flags (msg_t::label);
|
2011-06-22 11:02:16 +02:00
|
|
|
unsigned char *data = (unsigned char*) prefix.data ();
|
|
|
|
put_uint32 (data, request_id);
|
2010-08-07 09:52:34 +02:00
|
|
|
rc = xreq_t::xsend (&prefix, flags_);
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
message_begins = false;
|
2010-04-27 17:36:00 +02:00
|
|
|
}
|
|
|
|
|
2011-06-23 08:51:48 +02:00
|
|
|
bool more = msg_->flags () & (msg_t::more | msg_t::label) ? true : false;
|
2010-03-27 14:24:57 +01:00
|
|
|
|
2010-08-07 09:52:34 +02:00
|
|
|
int rc = xreq_t::xsend (msg_, flags_);
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
2010-03-27 14:24:57 +01:00
|
|
|
|
2010-08-07 09:52:34 +02:00
|
|
|
// If the request was fully sent, flip the FSM into reply-receiving state.
|
|
|
|
if (!more) {
|
|
|
|
receiving_reply = true;
|
|
|
|
message_begins = true;
|
2010-03-27 14:24:57 +01:00
|
|
|
}
|
2009-09-21 14:39:59 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int zmq::req_t::xrecv (msg_t *msg_, int flags_)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
|
|
|
// If request wasn't send, we can't wait for reply.
|
2010-03-27 14:24:57 +01:00
|
|
|
if (!receiving_reply) {
|
2009-09-22 11:52:35 +02:00
|
|
|
errno = EFSM;
|
2009-09-21 14:39:59 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-22 11:02:16 +02:00
|
|
|
// First part of the reply should be the original request ID.
|
2010-08-07 09:52:34 +02:00
|
|
|
if (message_begins) {
|
|
|
|
int rc = xreq_t::xrecv (msg_, flags_);
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
2011-09-13 16:27:07 +02:00
|
|
|
|
|
|
|
// TODO: This should also close the connection with the peer!
|
|
|
|
if (unlikely (!(msg_->flags () & msg_t::label) || msg_->size () != 4)) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-22 11:02:16 +02:00
|
|
|
unsigned char *data = (unsigned char*) msg_->data ();
|
|
|
|
if (unlikely (get_uint32 (data) != request_id)) {
|
|
|
|
|
|
|
|
// The request ID does not match. Drop the entire message.
|
|
|
|
while (true) {
|
|
|
|
int rc = xreq_t::xrecv (msg_, flags_);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
if (!(msg_->flags () & (msg_t::label | msg_t::more)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
msg_->close ();
|
|
|
|
msg_->init ();
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-17 12:32:46 +02:00
|
|
|
message_begins = false;
|
2010-05-31 09:28:36 +02:00
|
|
|
}
|
2010-04-27 17:36:00 +02:00
|
|
|
|
2010-08-07 09:52:34 +02:00
|
|
|
int rc = xreq_t::xrecv (msg_, flags_);
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
// If the reply is fully received, flip the FSM into request-sending state.
|
2011-06-20 11:33:54 +02:00
|
|
|
if (!(msg_->flags () & (msg_t::more | msg_t::label))) {
|
2011-06-22 11:02:16 +02:00
|
|
|
request_id++;
|
2010-03-27 14:24:57 +01:00
|
|
|
receiving_reply = false;
|
2010-08-07 09:52:34 +02:00
|
|
|
message_begins = true;
|
2010-03-27 14:24:57 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-01 10:56:17 +02:00
|
|
|
bool zmq::req_t::xhas_in ()
|
|
|
|
{
|
2011-06-22 11:02:16 +02:00
|
|
|
// TODO: Duplicates should be removed here.
|
|
|
|
|
2010-08-07 09:52:34 +02:00
|
|
|
if (!receiving_reply)
|
2010-02-23 22:13:56 +01:00
|
|
|
return false;
|
2009-10-01 10:56:17 +02:00
|
|
|
|
2010-08-07 09:52:34 +02:00
|
|
|
return xreq_t::xhas_in ();
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::req_t::xhas_out ()
|
|
|
|
{
|
2010-03-27 14:24:57 +01:00
|
|
|
if (receiving_reply)
|
2010-03-02 22:23:34 +01:00
|
|
|
return false;
|
|
|
|
|
2010-08-07 09:52:34 +02:00
|
|
|
return xreq_t::xhas_out ();
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 10:00:23 +02:00
|
|
|
zmq::req_session_t::req_session_t (io_thread_t *io_thread_, bool connect_,
|
|
|
|
socket_base_t *socket_, const options_t &options_,
|
|
|
|
const char *protocol_, const char *address_) :
|
|
|
|
xreq_session_t (io_thread_, connect_, socket_, options_, protocol_,
|
|
|
|
address_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::req_session_t::~req_session_t ()
|
|
|
|
{
|
|
|
|
}
|
2009-09-21 14:39:59 +02:00
|
|
|
|