lb.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2007-2018 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include "lb.hpp"
  26. #include "pipe.hpp"
  27. #include "err.hpp"
  28. #include "msg.hpp"
  29. zmq::lb_t::lb_t () : _active (0), _current (0), _more (false), _dropping (false)
  30. {
  31. }
  32. zmq::lb_t::~lb_t ()
  33. {
  34. zmq_assert (_pipes.empty ());
  35. }
  36. void zmq::lb_t::attach (pipe_t *pipe_)
  37. {
  38. _pipes.push_back (pipe_);
  39. activated (pipe_);
  40. }
  41. void zmq::lb_t::pipe_terminated (pipe_t *pipe_)
  42. {
  43. const pipes_t::size_type index = _pipes.index (pipe_);
  44. // If we are in the middle of multipart message and current pipe
  45. // have disconnected, we have to drop the remainder of the message.
  46. if (index == _current && _more)
  47. _dropping = true;
  48. // Remove the pipe from the list; adjust number of active pipes
  49. // accordingly.
  50. if (index < _active) {
  51. _active--;
  52. _pipes.swap (index, _active);
  53. if (_current == _active)
  54. _current = 0;
  55. }
  56. _pipes.erase (pipe_);
  57. }
  58. void zmq::lb_t::activated (pipe_t *pipe_)
  59. {
  60. // Move the pipe to the list of active pipes.
  61. _pipes.swap (_pipes.index (pipe_), _active);
  62. _active++;
  63. }
  64. int zmq::lb_t::send (msg_t *msg_)
  65. {
  66. return sendpipe (msg_, NULL);
  67. }
  68. int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
  69. {
  70. // Drop the message if required. If we are at the end of the message
  71. // switch back to non-dropping mode.
  72. if (_dropping) {
  73. _more = (msg_->flags () & msg_t::more) != 0;
  74. _dropping = _more;
  75. int rc = msg_->close ();
  76. errno_assert (rc == 0);
  77. rc = msg_->init ();
  78. errno_assert (rc == 0);
  79. return 0;
  80. }
  81. while (_active > 0) {
  82. if (_pipes[_current]->write (msg_)) {
  83. if (pipe_)
  84. *pipe_ = _pipes[_current];
  85. break;
  86. }
  87. // If send fails for multi-part msg rollback other
  88. // parts sent earlier and return EAGAIN.
  89. // Application should handle this as suitable
  90. if (_more) {
  91. _pipes[_current]->rollback ();
  92. // At this point the pipe is already being deallocated
  93. // and the first N frames are unreachable (_outpipe is
  94. // most likely already NULL so rollback won't actually do
  95. // anything and they can't be un-written to deliver later).
  96. // Return EFAULT to socket_base caller to drop current message
  97. // and any other subsequent frames to avoid them being
  98. // "stuck" and received when a new client reconnects, which
  99. // would break atomicity of multi-part messages (in blocking mode
  100. // socket_base just tries again and again to send the same message)
  101. // Note that given dropping mode returns 0, the user will
  102. // never know that the message could not be delivered, but
  103. // can't really fix it without breaking backward compatibility.
  104. // -2/EAGAIN will make sure socket_base caller does not re-enter
  105. // immediately or after a short sleep in blocking mode.
  106. _dropping = (msg_->flags () & msg_t::more) != 0;
  107. _more = false;
  108. errno = EAGAIN;
  109. return -2;
  110. }
  111. _active--;
  112. if (_current < _active)
  113. _pipes.swap (_current, _active);
  114. else
  115. _current = 0;
  116. }
  117. // If there are no pipes we cannot send the message.
  118. if (_active == 0) {
  119. errno = EAGAIN;
  120. return -1;
  121. }
  122. // If it's final part of the message we can flush it downstream and
  123. // continue round-robining (load balance).
  124. _more = (msg_->flags () & msg_t::more) != 0;
  125. if (!_more) {
  126. _pipes[_current]->flush ();
  127. if (++_current >= _active)
  128. _current = 0;
  129. }
  130. // Detach the message from the data buffer.
  131. const int rc = msg_->init ();
  132. errno_assert (rc == 0);
  133. return 0;
  134. }
  135. bool zmq::lb_t::has_out ()
  136. {
  137. // If one part of the message was already written we can definitely
  138. // write the rest of the message.
  139. if (_more)
  140. return true;
  141. while (_active > 0) {
  142. // Check whether a pipe has room for another message.
  143. if (_pipes[_current]->check_write ())
  144. return true;
  145. // Deactivate the pipe.
  146. _active--;
  147. _pipes.swap (_current, _active);
  148. if (_current == _active)
  149. _current = 0;
  150. }
  151. return false;
  152. }