0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-22 07:29:31 +08:00
libzmq/src/pair.cpp

132 lines
3.0 KiB
C++
Raw Normal View History

2009-09-21 14:39:59 +02:00
/*
2014-01-02 12:00:57 +01:00
Copyright (c) 2007-2014 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
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
GNU Lesser General Public License for more details.
2009-09-21 14:39:59 +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/>.
*/
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"
#include "msg.hpp"
2009-09-21 14:39:59 +02:00
zmq::pair_t::pair_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
socket_base_t (parent_, tid_, sid_),
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
{
zmq_assert (!pipe);
2009-09-21 14:39:59 +02:00
}
void zmq::pair_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
2009-09-21 14:39:59 +02:00
{
// subscribe_to_all_ is unused
(void)subscribe_to_all_;
zmq_assert (pipe_ != NULL);
// ZMQ_PAIR socket can only be connected to a single peer.
// The socket rejects any further connection requests.
if (pipe == NULL)
pipe = pipe_;
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
{
if (pipe_ == pipe) {
if (last_in == pipe) {
saved_credential = last_in->get_credential ();
last_in = NULL;
}
pipe = NULL;
}
2009-09-21 14:39:59 +02:00
}
void zmq::pair_t::xread_activated (pipe_t *)
{
// There's just one pipe. No lists of active and inactive pipes.
// There's nothing to do here.
}
void zmq::pair_t::xwrite_activated (pipe_t *)
2009-09-21 14:39:59 +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
{
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))
pipe->flush ();
// Detach the original message from the data buffer.
int rc = msg_->init ();
errno_assert (rc == 0);
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.
int rc = msg_->close ();
errno_assert (rc == 0);
2010-01-30 13:40:50 +01:00
if (!pipe || !pipe->read (msg_)) {
// Initialise the output parameter to be a 0-byte message.
rc = msg_->init ();
errno_assert (rc == 0);
2010-01-30 13:40:50 +01:00
errno = EAGAIN;
return -1;
}
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 ()
{
if (!pipe)
return false;
return pipe->check_read ();
}
2010-04-26 16:51:05 +02:00
bool zmq::pair_t::xhas_out ()
{
if (!pipe)
return false;
return pipe->check_write ();
}
zmq::blob_t zmq::pair_t::get_credential () const
{
return last_in? last_in->get_credential (): saved_credential;
}