2009-09-21 14:39:59 +02:00
|
|
|
/*
|
2015-01-22 10:32:06 +01:00
|
|
|
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2009-09-21 14:39:59 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq 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 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
|
|
|
|
2012-03-20 01:41:20 +01:00
|
|
|
zmq::req_t::req_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
|
2012-03-22 17:36:19 +01:00
|
|
|
dealer_t (parent_, tid_, sid_),
|
2010-03-27 14:24:57 +01:00
|
|
|
receiving_reply (false),
|
2013-07-02 20:05:20 +02:00
|
|
|
message_begins (true),
|
2013-07-26 21:13:43 +02:00
|
|
|
reply_pipe (NULL),
|
|
|
|
request_id_frames_enabled (false),
|
2013-07-21 13:16:47 +02:00
|
|
|
request_id (generate_random()),
|
2013-08-03 14:35:18 +02:00
|
|
|
strict (true)
|
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
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
int zmq::req_t::xsend (msg_t *msg_)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
|
|
|
// If we've sent a request and we still haven't got the reply,
|
2013-08-03 14:35:18 +02:00
|
|
|
// we can't send another request unless the strict option is disabled.
|
2010-03-27 14:24:57 +01:00
|
|
|
if (receiving_reply) {
|
2013-08-03 14:35:18 +02:00
|
|
|
if (strict) {
|
2013-07-21 13:16:47 +02:00
|
|
|
errno = EFSM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
receiving_reply = false;
|
|
|
|
message_begins = true;
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-07-26 21:13:43 +02:00
|
|
|
reply_pipe = NULL;
|
|
|
|
|
|
|
|
if (request_id_frames_enabled) {
|
|
|
|
request_id++;
|
|
|
|
|
|
|
|
msg_t id;
|
|
|
|
int rc = id.init_data (&request_id, sizeof (request_id), NULL, NULL);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
id.set_flags (msg_t::more);
|
|
|
|
|
|
|
|
rc = dealer_t::sendpipe (&id, &reply_pipe);
|
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-01 13:39:54 +01:00
|
|
|
msg_t bottom;
|
|
|
|
int rc = bottom.init ();
|
2011-04-21 22:27:48 +02:00
|
|
|
errno_assert (rc == 0);
|
2011-11-01 13:39:54 +01:00
|
|
|
bottom.set_flags (msg_t::more);
|
2013-07-02 20:05:20 +02:00
|
|
|
|
|
|
|
rc = dealer_t::sendpipe (&bottom, &reply_pipe);
|
2010-08-07 09:52:34 +02:00
|
|
|
if (rc != 0)
|
2011-11-01 13:39:54 +01:00
|
|
|
return -1;
|
2014-01-08 21:11:51 +01:00
|
|
|
zmq_assert (reply_pipe);
|
2013-07-26 21:13:43 +02:00
|
|
|
|
2010-08-07 09:52:34 +02:00
|
|
|
message_begins = false;
|
2013-07-02 20:05:20 +02:00
|
|
|
|
2015-09-06 18:46:32 +02:00
|
|
|
// Eat all currently available messages before the request is fully
|
2013-07-02 20:05:20 +02:00
|
|
|
// sent. This is done to avoid:
|
|
|
|
// REQ sends request to A, A replies, B replies too.
|
|
|
|
// A's reply was first and matches, that is used.
|
|
|
|
// An hour later REQ sends a request to B. B's old reply is used.
|
|
|
|
msg_t drop;
|
|
|
|
while (true) {
|
|
|
|
rc = drop.init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = dealer_t::xrecv (&drop);
|
|
|
|
if (rc != 0)
|
|
|
|
break;
|
|
|
|
drop.close ();
|
|
|
|
}
|
2010-04-27 17:36:00 +02:00
|
|
|
}
|
|
|
|
|
2011-11-01 13:39:54 +01:00
|
|
|
bool more = msg_->flags () & msg_t::more ? true : false;
|
2010-03-27 14:24:57 +01:00
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
int rc = dealer_t::xsend (msg_);
|
2010-08-07 09:52:34 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:17:43 +01:00
|
|
|
int zmq::req_t::xrecv (msg_t *msg_)
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-26 21:13:43 +02:00
|
|
|
// Skip messages until one with the right first frames is found.
|
|
|
|
while (message_begins) {
|
|
|
|
// If enabled, the first frame must have the correct request_id.
|
|
|
|
if (request_id_frames_enabled) {
|
|
|
|
int rc = recv_reply_pipe (msg_);
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (unlikely (!(msg_->flags () & msg_t::more) ||
|
|
|
|
msg_->size () != sizeof (request_id) ||
|
|
|
|
*static_cast<uint32_t *> (msg_->data ()) != request_id)) {
|
|
|
|
// Skip the remaining frames and try the next message
|
|
|
|
while (msg_->flags () & msg_t::more) {
|
|
|
|
rc = recv_reply_pipe (msg_);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The next frame must be 0.
|
|
|
|
// TODO: Failing this check should also close the connection with the peer!
|
2013-07-02 20:05:20 +02:00
|
|
|
int rc = recv_reply_pipe (msg_);
|
2010-08-07 09:52:34 +02:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
2011-09-13 16:27:07 +02:00
|
|
|
|
2011-11-01 13:39:54 +01:00
|
|
|
if (unlikely (!(msg_->flags () & msg_t::more) || msg_->size () != 0)) {
|
2013-07-26 21:13:43 +02:00
|
|
|
// Skip the remaining frames and try the next message
|
|
|
|
while (msg_->flags () & msg_t::more) {
|
|
|
|
rc = recv_reply_pipe (msg_);
|
2011-10-29 14:47:53 +02:00
|
|
|
errno_assert (rc == 0);
|
2011-06-22 11:02:16 +02:00
|
|
|
}
|
2013-07-26 21:13:43 +02:00
|
|
|
continue;
|
2011-06-22 11:02:16 +02:00
|
|
|
}
|
2011-11-01 13:39:54 +01:00
|
|
|
|
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
|
|
|
|
2013-07-02 20:05:20 +02:00
|
|
|
int rc = recv_reply_pipe (msg_);
|
2010-08-07 09:52:34 +02:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
// If the reply is fully received, flip the FSM into request-sending state.
|
2011-11-01 13:39:54 +01:00
|
|
|
if (!(msg_->flags () & msg_t::more)) {
|
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
|
|
|
|
2012-03-22 17:36:19 +01:00
|
|
|
return dealer_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;
|
|
|
|
|
2012-03-22 17:36:19 +01:00
|
|
|
return dealer_t::xhas_out ();
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:33:10 +02:00
|
|
|
int zmq::req_t::xsetsockopt (int option_, const void *optval_, size_t optvallen_)
|
2013-07-26 21:13:43 +02:00
|
|
|
{
|
|
|
|
bool is_int = (optvallen_ == sizeof (int));
|
|
|
|
int value = is_int? *((int *) optval_): 0;
|
|
|
|
switch (option_) {
|
2013-09-20 15:30:04 +02:00
|
|
|
case ZMQ_REQ_CORRELATE:
|
2013-07-26 21:13:43 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-08-24 02:14:03 +02:00
|
|
|
request_id_frames_enabled = (value != 0);
|
2013-07-26 21:13:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-09-20 15:30:04 +02:00
|
|
|
case ZMQ_REQ_RELAXED:
|
2013-07-21 13:16:47 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-09-20 15:30:04 +02:00
|
|
|
strict = (value == 0);
|
2013-07-21 13:16:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-07-26 21:13:43 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-29 09:33:10 +02:00
|
|
|
return dealer_t::xsetsockopt (option_, optval_, optvallen_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::req_t::xpipe_terminated (pipe_t *pipe_)
|
|
|
|
{
|
|
|
|
if (reply_pipe == pipe_)
|
|
|
|
reply_pipe = NULL;
|
|
|
|
dealer_t::xpipe_terminated (pipe_);
|
2013-07-26 21:13:43 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 20:05:20 +02:00
|
|
|
int zmq::req_t::recv_reply_pipe (msg_t *msg_)
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
pipe_t *pipe = NULL;
|
2013-07-29 09:33:10 +02:00
|
|
|
int rc = dealer_t::recvpipe (msg_, &pipe);
|
2013-07-02 20:05:20 +02:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
2013-07-29 09:33:10 +02:00
|
|
|
if (!reply_pipe || pipe == reply_pipe)
|
2013-07-02 20:05:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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_,
|
2014-03-12 17:53:13 +01:00
|
|
|
address_t *addr_) :
|
2013-06-29 11:24:46 +02:00
|
|
|
session_base_t (io_thread_, connect_, socket_, options_, addr_),
|
2013-03-18 02:00:00 +01:00
|
|
|
state (bottom)
|
2011-09-15 10:00:23 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::req_session_t::~req_session_t ()
|
|
|
|
{
|
|
|
|
}
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2012-09-02 18:03:38 +02:00
|
|
|
int zmq::req_session_t::push_msg (msg_t *msg_)
|
2011-09-16 09:29:43 +02:00
|
|
|
{
|
2011-11-04 08:00:47 +01:00
|
|
|
switch (state) {
|
|
|
|
case bottom:
|
2011-11-01 13:39:54 +01:00
|
|
|
if (msg_->flags () == msg_t::more && msg_->size () == 0) {
|
2011-09-16 09:29:43 +02:00
|
|
|
state = body;
|
2013-06-29 11:24:46 +02:00
|
|
|
return session_base_t::push_msg (msg_);
|
2011-09-16 09:29:43 +02:00
|
|
|
}
|
2011-11-04 08:00:47 +01:00
|
|
|
break;
|
|
|
|
case body:
|
2011-09-16 09:29:43 +02:00
|
|
|
if (msg_->flags () == msg_t::more)
|
2013-06-29 11:24:46 +02:00
|
|
|
return session_base_t::push_msg (msg_);
|
2011-09-16 09:29:43 +02:00
|
|
|
if (msg_->flags () == 0) {
|
2011-11-01 13:39:54 +01:00
|
|
|
state = bottom;
|
2013-06-29 11:24:46 +02:00
|
|
|
return session_base_t::push_msg (msg_);
|
2011-09-16 09:29:43 +02:00
|
|
|
}
|
2011-11-04 08:00:47 +01:00
|
|
|
break;
|
2011-09-16 09:29:43 +02:00
|
|
|
}
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-05-29 21:59:22 +02:00
|
|
|
void zmq::req_session_t::reset ()
|
|
|
|
{
|
|
|
|
session_base_t::reset ();
|
2013-03-18 02:00:00 +01:00
|
|
|
state = bottom;
|
2012-05-29 21:59:22 +02:00
|
|
|
}
|