stream.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Copyright (c) 2007-2016 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 "macros.hpp"
  26. #include "stream.hpp"
  27. #include "pipe.hpp"
  28. #include "wire.hpp"
  29. #include "random.hpp"
  30. #include "likely.hpp"
  31. #include "err.hpp"
  32. zmq::stream_t::stream_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
  33. routing_socket_base_t (parent_, tid_, sid_),
  34. _prefetched (false),
  35. _routing_id_sent (false),
  36. _current_out (NULL),
  37. _more_out (false),
  38. _next_integral_routing_id (generate_random ())
  39. {
  40. options.type = ZMQ_STREAM;
  41. options.raw_socket = true;
  42. _prefetched_routing_id.init ();
  43. _prefetched_msg.init ();
  44. }
  45. zmq::stream_t::~stream_t ()
  46. {
  47. _prefetched_routing_id.close ();
  48. _prefetched_msg.close ();
  49. }
  50. void zmq::stream_t::xattach_pipe (pipe_t *pipe_,
  51. bool subscribe_to_all_,
  52. bool locally_initiated_)
  53. {
  54. LIBZMQ_UNUSED (subscribe_to_all_);
  55. zmq_assert (pipe_);
  56. identify_peer (pipe_, locally_initiated_);
  57. _fq.attach (pipe_);
  58. }
  59. void zmq::stream_t::xpipe_terminated (pipe_t *pipe_)
  60. {
  61. erase_out_pipe (pipe_);
  62. _fq.pipe_terminated (pipe_);
  63. // TODO router_t calls pipe_->rollback() here; should this be done here as
  64. // well? then xpipe_terminated could be pulled up to routing_socket_base_t
  65. if (pipe_ == _current_out)
  66. _current_out = NULL;
  67. }
  68. void zmq::stream_t::xread_activated (pipe_t *pipe_)
  69. {
  70. _fq.activated (pipe_);
  71. }
  72. int zmq::stream_t::xsend (msg_t *msg_)
  73. {
  74. // If this is the first part of the message it's the ID of the
  75. // peer to send the message to.
  76. if (!_more_out) {
  77. zmq_assert (!_current_out);
  78. // If we have malformed message (prefix with no subsequent message)
  79. // then just silently ignore it.
  80. // TODO: The connections should be killed instead.
  81. if (msg_->flags () & msg_t::more) {
  82. // Find the pipe associated with the routing id stored in the prefix.
  83. // If there's no such pipe return an error
  84. out_pipe_t *out_pipe = lookup_out_pipe (
  85. blob_t (static_cast<unsigned char *> (msg_->data ()),
  86. msg_->size (), reference_tag_t ()));
  87. if (out_pipe) {
  88. _current_out = out_pipe->pipe;
  89. if (!_current_out->check_write ()) {
  90. out_pipe->active = false;
  91. _current_out = NULL;
  92. errno = EAGAIN;
  93. return -1;
  94. }
  95. } else {
  96. errno = EHOSTUNREACH;
  97. return -1;
  98. }
  99. }
  100. // Expect one more message frame.
  101. _more_out = true;
  102. int rc = msg_->close ();
  103. errno_assert (rc == 0);
  104. rc = msg_->init ();
  105. errno_assert (rc == 0);
  106. return 0;
  107. }
  108. // Ignore the MORE flag
  109. msg_->reset_flags (msg_t::more);
  110. // This is the last part of the message.
  111. _more_out = false;
  112. // Push the message into the pipe. If there's no out pipe, just drop it.
  113. if (_current_out) {
  114. // Close the remote connection if user has asked to do so
  115. // by sending zero length message.
  116. // Pending messages in the pipe will be dropped (on receiving term- ack)
  117. if (msg_->size () == 0) {
  118. _current_out->terminate (false);
  119. int rc = msg_->close ();
  120. errno_assert (rc == 0);
  121. rc = msg_->init ();
  122. errno_assert (rc == 0);
  123. _current_out = NULL;
  124. return 0;
  125. }
  126. const bool ok = _current_out->write (msg_);
  127. if (likely (ok))
  128. _current_out->flush ();
  129. _current_out = NULL;
  130. } else {
  131. const int rc = msg_->close ();
  132. errno_assert (rc == 0);
  133. }
  134. // Detach the message from the data buffer.
  135. const int rc = msg_->init ();
  136. errno_assert (rc == 0);
  137. return 0;
  138. }
  139. int zmq::stream_t::xsetsockopt (int option_,
  140. const void *optval_,
  141. size_t optvallen_)
  142. {
  143. switch (option_) {
  144. case ZMQ_STREAM_NOTIFY:
  145. return do_setsockopt_int_as_bool_strict (optval_, optvallen_,
  146. &options.raw_notify);
  147. default:
  148. return routing_socket_base_t::xsetsockopt (option_, optval_,
  149. optvallen_);
  150. }
  151. }
  152. int zmq::stream_t::xrecv (msg_t *msg_)
  153. {
  154. if (_prefetched) {
  155. if (!_routing_id_sent) {
  156. const int rc = msg_->move (_prefetched_routing_id);
  157. errno_assert (rc == 0);
  158. _routing_id_sent = true;
  159. } else {
  160. const int rc = msg_->move (_prefetched_msg);
  161. errno_assert (rc == 0);
  162. _prefetched = false;
  163. }
  164. return 0;
  165. }
  166. pipe_t *pipe = NULL;
  167. int rc = _fq.recvpipe (&_prefetched_msg, &pipe);
  168. if (rc != 0)
  169. return -1;
  170. zmq_assert (pipe != NULL);
  171. zmq_assert ((_prefetched_msg.flags () & msg_t::more) == 0);
  172. // We have received a frame with TCP data.
  173. // Rather than sending this frame, we keep it in prefetched
  174. // buffer and send a frame with peer's ID.
  175. const blob_t &routing_id = pipe->get_routing_id ();
  176. rc = msg_->close ();
  177. errno_assert (rc == 0);
  178. rc = msg_->init_size (routing_id.size ());
  179. errno_assert (rc == 0);
  180. // forward metadata (if any)
  181. metadata_t *metadata = _prefetched_msg.metadata ();
  182. if (metadata)
  183. msg_->set_metadata (metadata);
  184. memcpy (msg_->data (), routing_id.data (), routing_id.size ());
  185. msg_->set_flags (msg_t::more);
  186. _prefetched = true;
  187. _routing_id_sent = true;
  188. return 0;
  189. }
  190. bool zmq::stream_t::xhas_in ()
  191. {
  192. // We may already have a message pre-fetched.
  193. if (_prefetched)
  194. return true;
  195. // Try to read the next message.
  196. // The message, if read, is kept in the pre-fetch buffer.
  197. pipe_t *pipe = NULL;
  198. int rc = _fq.recvpipe (&_prefetched_msg, &pipe);
  199. if (rc != 0)
  200. return false;
  201. zmq_assert (pipe != NULL);
  202. zmq_assert ((_prefetched_msg.flags () & msg_t::more) == 0);
  203. const blob_t &routing_id = pipe->get_routing_id ();
  204. rc = _prefetched_routing_id.init_size (routing_id.size ());
  205. errno_assert (rc == 0);
  206. // forward metadata (if any)
  207. metadata_t *metadata = _prefetched_msg.metadata ();
  208. if (metadata)
  209. _prefetched_routing_id.set_metadata (metadata);
  210. memcpy (_prefetched_routing_id.data (), routing_id.data (),
  211. routing_id.size ());
  212. _prefetched_routing_id.set_flags (msg_t::more);
  213. _prefetched = true;
  214. _routing_id_sent = false;
  215. return true;
  216. }
  217. bool zmq::stream_t::xhas_out ()
  218. {
  219. // In theory, STREAM socket is always ready for writing. Whether actual
  220. // attempt to write succeeds depends on which pipe the message is going
  221. // to be routed to.
  222. return true;
  223. }
  224. void zmq::stream_t::identify_peer (pipe_t *pipe_, bool locally_initiated_)
  225. {
  226. // Always assign routing id for raw-socket
  227. unsigned char buffer[5];
  228. buffer[0] = 0;
  229. blob_t routing_id;
  230. if (locally_initiated_ && connect_routing_id_is_set ()) {
  231. const std::string connect_routing_id = extract_connect_routing_id ();
  232. routing_id.set (
  233. reinterpret_cast<const unsigned char *> (connect_routing_id.c_str ()),
  234. connect_routing_id.length ());
  235. // Not allowed to duplicate an existing rid
  236. zmq_assert (!has_out_pipe (routing_id));
  237. } else {
  238. put_uint32 (buffer + 1, _next_integral_routing_id++);
  239. routing_id.set (buffer, sizeof buffer);
  240. memcpy (options.routing_id, routing_id.data (), routing_id.size ());
  241. options.routing_id_size =
  242. static_cast<unsigned char> (routing_id.size ());
  243. }
  244. pipe_->set_router_socket_routing_id (routing_id);
  245. add_out_pipe (ZMQ_MOVE (routing_id), pipe_);
  246. }