2009-09-21 14:39:59 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 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/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-18 10:56:52 -06:00
|
|
|
#include "precompiled.hpp"
|
2015-08-21 16:12:22 -07:00
|
|
|
#include "macros.hpp"
|
2010-04-26 16:51:05 +02:00
|
|
|
#include "pair.hpp"
|
2009-09-21 14:39:59 +02:00
|
|
|
#include "err.hpp"
|
2010-01-30 13:40:50 +01:00
|
|
|
#include "pipe.hpp"
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2012-03-19 19:41:20 -05:00
|
|
|
zmq::pair_t::pair_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
|
|
|
|
socket_base_t (parent_, tid_, sid_),
|
2018-05-27 11:10:39 +02:00
|
|
|
_pipe (NULL),
|
|
|
|
_last_in (NULL)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2010-09-28 15:27:45 +02:00
|
|
|
options.type = ZMQ_PAIR;
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2010-04-26 16:51:05 +02:00
|
|
|
zmq::pair_t::~pair_t ()
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
zmq_assert (!_pipe);
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 13:48:43 -05:00
|
|
|
void zmq::pair_t::xattach_pipe (pipe_t *pipe_,
|
|
|
|
bool subscribe_to_all_,
|
|
|
|
bool locally_initiated_)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2015-09-11 13:03:31 -04:00
|
|
|
LIBZMQ_UNUSED (subscribe_to_all_);
|
2018-07-24 13:48:43 -05:00
|
|
|
LIBZMQ_UNUSED (locally_initiated_);
|
2012-08-27 16:05:51 -07:00
|
|
|
|
2012-04-30 00:48:07 +02:00
|
|
|
zmq_assert (pipe_ != NULL);
|
|
|
|
|
|
|
|
// ZMQ_PAIR socket can only be connected to a single peer.
|
|
|
|
// The socket rejects any further connection requests.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_pipe == NULL)
|
|
|
|
_pipe = pipe_;
|
2012-04-30 00:48:07 +02:00
|
|
|
else
|
|
|
|
pipe_->terminate (false);
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:49:24 +02:00
|
|
|
void zmq::pair_t::xpipe_terminated (pipe_t *pipe_)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
if (pipe_ == _pipe) {
|
|
|
|
if (_last_in == _pipe) {
|
|
|
|
_last_in = NULL;
|
2014-01-12 21:58:36 +01:00
|
|
|
}
|
2018-05-27 11:10:39 +02:00
|
|
|
_pipe = NULL;
|
2014-01-12 21:58:36 +01:00
|
|
|
}
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2012-08-27 16:05:51 -07:00
|
|
|
void zmq::pair_t::xread_activated (pipe_t *)
|
2010-03-01 10:13:26 +01:00
|
|
|
{
|
2011-05-22 17:26:53 +02:00
|
|
|
// There's just one pipe. No lists of active and inactive pipes.
|
|
|
|
// There's nothing to do here.
|
2010-03-01 10:13:26 +01:00
|
|
|
}
|
|
|
|
|
2012-08-27 16:05:51 -07:00
|
|
|
void zmq::pair_t::xwrite_activated (pipe_t *)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2011-05-22 17:26:53 +02:00
|
|
|
// There's just one pipe. No lists of active and inactive pipes.
|
|
|
|
// There's nothing to do here.
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
int zmq::pair_t::xsend (msg_t *msg_)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
if (!_pipe || !_pipe->write (msg_)) {
|
2010-01-30 13:40:50 +01:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
if (!(msg_->flags () & msg_t::more))
|
2018-05-27 11:10:39 +02:00
|
|
|
_pipe->flush ();
|
2010-02-07 11:31:05 +01:00
|
|
|
|
|
|
|
// Detach the original message from the data buffer.
|
2019-12-25 13:51:21 +01:00
|
|
|
const int rc = msg_->init ();
|
2011-04-21 22:27:48 +02:00
|
|
|
errno_assert (rc == 0);
|
2010-02-07 11:31:05 +01:00
|
|
|
|
2009-09-22 08:30:15 +02:00
|
|
|
return 0;
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-09 17:17:43 +01:00
|
|
|
int zmq::pair_t::xrecv (msg_t *msg_)
|
2009-09-21 14:39:59 +02:00
|
|
|
{
|
2010-01-30 13:40:50 +01:00
|
|
|
// Deallocate old content of the message.
|
2011-04-21 22:27:48 +02:00
|
|
|
int rc = msg_->close ();
|
|
|
|
errno_assert (rc == 0);
|
2010-01-30 13:40:50 +01:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
if (!_pipe || !_pipe->read (msg_)) {
|
2010-08-06 17:49:37 +02:00
|
|
|
// Initialise the output parameter to be a 0-byte message.
|
2011-04-21 22:27:48 +02:00
|
|
|
rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
2011-05-22 17:26:53 +02:00
|
|
|
|
2010-01-30 13:40:50 +01:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-27 11:10:39 +02:00
|
|
|
_last_in = _pipe;
|
2009-09-22 08:30:15 +02:00
|
|
|
return 0;
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2010-04-26 16:51:05 +02:00
|
|
|
bool zmq::pair_t::xhas_in ()
|
2009-10-01 10:56:17 +02:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
if (!_pipe)
|
2010-08-06 17:49:37 +02:00
|
|
|
return false;
|
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
return _pipe->check_read ();
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
|
|
|
|
2010-04-26 16:51:05 +02:00
|
|
|
bool zmq::pair_t::xhas_out ()
|
2009-10-01 10:56:17 +02:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
if (!_pipe)
|
2010-03-01 16:55:13 +01:00
|
|
|
return false;
|
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
return _pipe->check_write ();
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|