2009-12-15 09:09:19 +01:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2009-12-15 09:09:19 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-12-15 09:09:19 +01: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-12-15 09:09:19 +01: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-12-15 09:09:19 +01:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-12-15 09:09:19 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-05-02 16:50:16 +02:00
|
|
|
|
2016-02-18 10:56:52 -06:00
|
|
|
#include "precompiled.hpp"
|
2009-12-15 09:09:19 +01:00
|
|
|
#include "lb.hpp"
|
|
|
|
#include "pipe.hpp"
|
|
|
|
#include "err.hpp"
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2009-12-15 09:09:19 +01:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
zmq::lb_t::lb_t () : active (0), current (0), more (false), dropping (false)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::lb_t::~lb_t ()
|
|
|
|
{
|
2010-08-06 17:49:37 +02:00
|
|
|
zmq_assert (pipes.empty ());
|
2009-12-15 09:09:19 +01:00
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
void zmq::lb_t::attach (pipe_t *pipe_)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
|
|
|
pipes.push_back (pipe_);
|
2012-06-12 15:34:48 +01:00
|
|
|
activated (pipe_);
|
2010-08-06 17:49:37 +02:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:49:24 +02:00
|
|
|
void zmq::lb_t::pipe_terminated (pipe_t *pipe_)
|
2010-08-06 17:49:37 +02:00
|
|
|
{
|
2011-03-20 20:52:54 +01:00
|
|
|
pipes_t::size_type index = pipes.index (pipe_);
|
|
|
|
|
|
|
|
// If we are in the middle of multipart message and current pipe
|
|
|
|
// have disconnected, we have to drop the remainder of the message.
|
|
|
|
if (index == current && more)
|
|
|
|
dropping = true;
|
|
|
|
|
2009-12-15 09:09:19 +01:00
|
|
|
// Remove the pipe from the list; adjust number of active pipes
|
|
|
|
// accordingly.
|
2011-03-20 20:52:54 +01:00
|
|
|
if (index < active) {
|
2009-12-15 09:09:19 +01:00
|
|
|
active--;
|
2012-05-31 15:34:30 +02:00
|
|
|
pipes.swap (index, active);
|
2010-02-10 12:48:04 +01:00
|
|
|
if (current == active)
|
|
|
|
current = 0;
|
|
|
|
}
|
2009-12-15 09:09:19 +01:00
|
|
|
pipes.erase (pipe_);
|
2010-08-06 17:49:37 +02:00
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
void zmq::lb_t::activated (pipe_t *pipe_)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
|
|
|
// Move the pipe to the list of active pipes.
|
|
|
|
pipes.swap (pipes.index (pipe_), active);
|
|
|
|
active++;
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
int zmq::lb_t::send (msg_t *msg_)
|
2013-07-02 20:05:20 +02:00
|
|
|
{
|
|
|
|
return sendpipe (msg_, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
2011-03-20 20:52:54 +01:00
|
|
|
// Drop the message if required. If we are at the end of the message
|
|
|
|
// switch back to non-dropping mode.
|
|
|
|
if (dropping) {
|
2018-05-25 22:59:13 +02:00
|
|
|
more = (msg_->flags () & msg_t::more) != 0;
|
2012-05-31 15:59:59 +02:00
|
|
|
dropping = more;
|
2011-03-20 20:52:54 +01:00
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int rc = msg_->close ();
|
2011-03-20 20:52:54 +01:00
|
|
|
errno_assert (rc == 0);
|
2011-04-21 22:27:48 +02:00
|
|
|
rc = msg_->init ();
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (rc == 0);
|
2011-03-20 20:52:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
while (active > 0) {
|
2018-02-01 11:46:09 +01:00
|
|
|
if (pipes[current]->write (msg_)) {
|
2013-07-02 20:05:20 +02:00
|
|
|
if (pipe_)
|
2018-02-01 11:46:09 +01:00
|
|
|
*pipe_ = pipes[current];
|
2010-03-01 10:13:26 +01:00
|
|
|
break;
|
2013-07-02 20:05:20 +02:00
|
|
|
}
|
2014-05-02 16:50:16 +02:00
|
|
|
|
2015-10-05 11:46:48 -07:00
|
|
|
// If send fails for multi-part msg rollback other
|
|
|
|
// parts sent earlier and return EAGAIN.
|
2016-04-25 12:18:46 +01:00
|
|
|
// Application should handle this as suitable
|
2018-02-01 11:46:09 +01:00
|
|
|
if (more) {
|
|
|
|
pipes[current]->rollback ();
|
2018-05-25 23:03:52 +02:00
|
|
|
more = false;
|
2015-10-05 11:46:48 -07:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
active--;
|
|
|
|
if (current < active)
|
|
|
|
pipes.swap (current, active);
|
|
|
|
else
|
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-15 09:09:19 +01:00
|
|
|
// If there are no pipes we cannot send the message.
|
2010-03-01 10:13:26 +01:00
|
|
|
if (active == 0) {
|
2009-12-15 09:09:19 +01:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-22 13:39:20 +02:00
|
|
|
// If it's final part of the message we can flush it downstream and
|
|
|
|
// continue round-robining (load balance).
|
2018-05-25 22:59:13 +02:00
|
|
|
more = (msg_->flags () & msg_t::more) != 0;
|
2010-03-27 21:25:40 +01:00
|
|
|
if (!more) {
|
2018-02-01 11:46:09 +01:00
|
|
|
pipes[current]->flush ();
|
2017-01-27 17:04:42 -08:00
|
|
|
|
|
|
|
if (++current >= active)
|
|
|
|
current = 0;
|
2010-03-27 09:43:49 +01:00
|
|
|
}
|
2009-12-15 09:09:19 +01:00
|
|
|
|
|
|
|
// Detach the message from the data buffer.
|
2011-04-21 22:27:48 +02:00
|
|
|
int rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
2009-12-15 09:09:19 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::lb_t::has_out ()
|
|
|
|
{
|
2010-03-27 09:43:49 +01:00
|
|
|
// If one part of the message was already written we can definitely
|
|
|
|
// write the rest of the message.
|
2010-03-27 21:25:40 +01:00
|
|
|
if (more)
|
2010-03-27 09:43:49 +01:00
|
|
|
return true;
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
while (active > 0) {
|
2012-03-28 06:38:25 +02:00
|
|
|
// Check whether a pipe has room for another message.
|
2018-02-01 11:46:09 +01:00
|
|
|
if (pipes[current]->check_write ())
|
2009-12-15 09:09:19 +01:00
|
|
|
return true;
|
2010-03-01 10:13:26 +01:00
|
|
|
|
2010-09-07 15:49:54 +02:00
|
|
|
// Deactivate the pipe.
|
2010-03-01 10:13:26 +01:00
|
|
|
active--;
|
2010-09-07 15:49:54 +02:00
|
|
|
pipes.swap (current, active);
|
|
|
|
if (current == active)
|
2009-12-15 09:09:19 +01:00
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|