proxy.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 <stddef.h>
  26. #include "poller.hpp"
  27. #include "proxy.hpp"
  28. #include "likely.hpp"
  29. #include "msg.hpp"
  30. #if defined ZMQ_POLL_BASED_ON_POLL && !defined ZMQ_HAVE_WINDOWS \
  31. && !defined ZMQ_HAVE_AIX
  32. #include <poll.h>
  33. #endif
  34. // These headers end up pulling in zmq.h somewhere in their include
  35. // dependency chain
  36. #include "socket_base.hpp"
  37. #include "err.hpp"
  38. #ifdef ZMQ_HAVE_POLLER
  39. #include "socket_poller.hpp"
  40. // Macros for repetitive code.
  41. // PROXY_CLEANUP() must not be used before these variables are initialized.
  42. #define PROXY_CLEANUP() \
  43. do { \
  44. delete poller_all; \
  45. delete poller_in; \
  46. delete poller_control; \
  47. delete poller_receive_blocked; \
  48. delete poller_send_blocked; \
  49. delete poller_both_blocked; \
  50. delete poller_frontend_only; \
  51. delete poller_backend_only; \
  52. } while (false)
  53. #define CHECK_RC_EXIT_ON_FAILURE() \
  54. do { \
  55. if (rc < 0) { \
  56. PROXY_CLEANUP (); \
  57. return close_and_return (&msg, -1); \
  58. } \
  59. } while (false)
  60. #endif // ZMQ_HAVE_POLLER
  61. // Control socket messages
  62. typedef struct
  63. {
  64. uint64_t msg_in;
  65. uint64_t bytes_in;
  66. uint64_t msg_out;
  67. uint64_t bytes_out;
  68. } zmq_socket_stats_t;
  69. // Utility functions
  70. static int
  71. capture (class zmq::socket_base_t *capture_, zmq::msg_t *msg_, int more_ = 0)
  72. {
  73. // Copy message to capture socket if any
  74. if (capture_) {
  75. zmq::msg_t ctrl;
  76. int rc = ctrl.init ();
  77. if (unlikely (rc < 0))
  78. return -1;
  79. rc = ctrl.copy (*msg_);
  80. if (unlikely (rc < 0))
  81. return -1;
  82. rc = capture_->send (&ctrl, more_ ? ZMQ_SNDMORE : 0);
  83. if (unlikely (rc < 0))
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. static int forward (class zmq::socket_base_t *from_,
  89. zmq_socket_stats_t *from_stats_,
  90. class zmq::socket_base_t *to_,
  91. zmq_socket_stats_t *to_stats_,
  92. class zmq::socket_base_t *capture_,
  93. zmq::msg_t *msg_)
  94. {
  95. // Forward a burst of messages
  96. for (unsigned int i = 0; i < zmq::proxy_burst_size; i++) {
  97. int more;
  98. size_t moresz;
  99. size_t complete_msg_size = 0;
  100. // Forward all the parts of one message
  101. while (true) {
  102. int rc = from_->recv (msg_, ZMQ_DONTWAIT);
  103. if (rc < 0) {
  104. if (likely (errno == EAGAIN && i > 0))
  105. return 0; // End of burst
  106. return -1;
  107. }
  108. complete_msg_size += msg_->size ();
  109. moresz = sizeof more;
  110. rc = from_->getsockopt (ZMQ_RCVMORE, &more, &moresz);
  111. if (unlikely (rc < 0))
  112. return -1;
  113. // Copy message to capture socket if any
  114. rc = capture (capture_, msg_, more);
  115. if (unlikely (rc < 0))
  116. return -1;
  117. rc = to_->send (msg_, more ? ZMQ_SNDMORE : 0);
  118. if (unlikely (rc < 0))
  119. return -1;
  120. if (more == 0)
  121. break;
  122. }
  123. // A multipart message counts as 1 packet:
  124. from_stats_->msg_in++;
  125. from_stats_->bytes_in += complete_msg_size;
  126. to_stats_->msg_out++;
  127. to_stats_->bytes_out += complete_msg_size;
  128. }
  129. return 0;
  130. }
  131. static int loop_and_send_multipart_stat (zmq::socket_base_t *control_,
  132. uint64_t stat_,
  133. bool first_,
  134. bool more_)
  135. {
  136. int rc;
  137. zmq::msg_t msg;
  138. // VSM of 8 bytes can't fail to init
  139. msg.init_size (sizeof (uint64_t));
  140. memcpy (msg.data (), &stat_, sizeof (uint64_t));
  141. // if the first message is handed to the pipe successfully then the HWM
  142. // is not full, which means failures are due to interrupts (on Windows pipes
  143. // are TCP sockets), so keep retrying
  144. do {
  145. rc = control_->send (&msg, more_ ? ZMQ_SNDMORE : 0);
  146. } while (!first_ && rc != 0 && errno == EAGAIN);
  147. return rc;
  148. }
  149. static int reply_stats (zmq::socket_base_t *control_,
  150. const zmq_socket_stats_t *frontend_stats_,
  151. const zmq_socket_stats_t *backend_stats_)
  152. {
  153. // first part: frontend stats - the first send might fail due to HWM
  154. if (loop_and_send_multipart_stat (control_, frontend_stats_->msg_in, true,
  155. true)
  156. != 0)
  157. return -1;
  158. loop_and_send_multipart_stat (control_, frontend_stats_->bytes_in, false,
  159. true);
  160. loop_and_send_multipart_stat (control_, frontend_stats_->msg_out, false,
  161. true);
  162. loop_and_send_multipart_stat (control_, frontend_stats_->bytes_out, false,
  163. true);
  164. // second part: backend stats
  165. loop_and_send_multipart_stat (control_, backend_stats_->msg_in, false,
  166. true);
  167. loop_and_send_multipart_stat (control_, backend_stats_->bytes_in, false,
  168. true);
  169. loop_and_send_multipart_stat (control_, backend_stats_->msg_out, false,
  170. true);
  171. loop_and_send_multipart_stat (control_, backend_stats_->bytes_out, false,
  172. false);
  173. return 0;
  174. }
  175. #ifdef ZMQ_HAVE_POLLER
  176. int zmq::proxy (class socket_base_t *frontend_,
  177. class socket_base_t *backend_,
  178. class socket_base_t *capture_,
  179. class socket_base_t *control_)
  180. {
  181. msg_t msg;
  182. int rc = msg.init ();
  183. if (rc != 0)
  184. return -1;
  185. // The algorithm below assumes ratio of requests and replies processed
  186. // under full load to be 1:1.
  187. int more;
  188. size_t moresz = sizeof (more);
  189. // Proxy can be in these three states
  190. enum
  191. {
  192. active,
  193. paused,
  194. terminated
  195. } state = active;
  196. bool frontend_equal_to_backend;
  197. bool frontend_in = false;
  198. bool frontend_out = false;
  199. bool backend_in = false;
  200. bool backend_out = false;
  201. bool control_in = false;
  202. zmq::socket_poller_t::event_t events[3];
  203. zmq_socket_stats_t frontend_stats;
  204. zmq_socket_stats_t backend_stats;
  205. memset (&frontend_stats, 0, sizeof (frontend_stats));
  206. memset (&backend_stats, 0, sizeof (backend_stats));
  207. // Don't allocate these pollers from stack because they will take more than 900 kB of stack!
  208. // On Windows this blows up default stack of 1 MB and aborts the program.
  209. // I wanted to use std::shared_ptr here as the best solution but that requires C++11...
  210. zmq::socket_poller_t *poller_all =
  211. new (std::nothrow) zmq::socket_poller_t; // Poll for everything.
  212. zmq::socket_poller_t *poller_in = new (std::nothrow) zmq::
  213. socket_poller_t; // Poll only 'ZMQ_POLLIN' on all sockets. Initial blocking poll in loop.
  214. zmq::socket_poller_t *poller_control = new (std::nothrow) zmq::
  215. socket_poller_t; // Poll only for 'ZMQ_POLLIN' on 'control_', when proxy is paused.
  216. zmq::socket_poller_t *poller_receive_blocked = new (std::nothrow)
  217. zmq::socket_poller_t; // All except 'ZMQ_POLLIN' on 'frontend_'.
  218. // If frontend_==backend_ 'poller_send_blocked' and 'poller_receive_blocked' are the same, 'ZMQ_POLLIN' is ignored.
  219. // In that case 'poller_send_blocked' is not used. We need only 'poller_receive_blocked'.
  220. // We also don't need 'poller_both_blocked', 'poller_backend_only' nor 'poller_frontend_only' no need to initialize it.
  221. // We save some RAM and time for initialization.
  222. zmq::socket_poller_t *poller_send_blocked =
  223. NULL; // All except 'ZMQ_POLLIN' on 'backend_'.
  224. zmq::socket_poller_t *poller_both_blocked =
  225. NULL; // All except 'ZMQ_POLLIN' on both 'frontend_' and 'backend_'.
  226. zmq::socket_poller_t *poller_frontend_only =
  227. NULL; // Only 'ZMQ_POLLIN' and 'ZMQ_POLLOUT' on 'frontend_'.
  228. zmq::socket_poller_t *poller_backend_only =
  229. NULL; // Only 'ZMQ_POLLIN' and 'ZMQ_POLLOUT' on 'backend_'.
  230. if (frontend_ != backend_) {
  231. poller_send_blocked = new (std::nothrow)
  232. zmq::socket_poller_t; // All except 'ZMQ_POLLIN' on 'backend_'.
  233. poller_both_blocked = new (std::nothrow) zmq::
  234. socket_poller_t; // All except 'ZMQ_POLLIN' on both 'frontend_' and 'backend_'.
  235. poller_frontend_only = new (std::nothrow) zmq::
  236. socket_poller_t; // Only 'ZMQ_POLLIN' and 'ZMQ_POLLOUT' on 'frontend_'.
  237. poller_backend_only = new (std::nothrow) zmq::
  238. socket_poller_t; // Only 'ZMQ_POLLIN' and 'ZMQ_POLLOUT' on 'backend_'.
  239. frontend_equal_to_backend = false;
  240. } else
  241. frontend_equal_to_backend = true;
  242. if (poller_all == NULL || poller_in == NULL || poller_control == NULL
  243. || poller_receive_blocked == NULL
  244. || ((poller_send_blocked == NULL || poller_both_blocked == NULL)
  245. && !frontend_equal_to_backend)) {
  246. PROXY_CLEANUP ();
  247. return close_and_return (&msg, -1);
  248. }
  249. zmq::socket_poller_t *poller_wait =
  250. poller_in; // Poller for blocking wait, initially all 'ZMQ_POLLIN'.
  251. // Register 'frontend_' and 'backend_' with pollers.
  252. rc = poller_all->add (frontend_, NULL,
  253. ZMQ_POLLIN | ZMQ_POLLOUT); // Everything.
  254. CHECK_RC_EXIT_ON_FAILURE ();
  255. rc = poller_in->add (frontend_, NULL, ZMQ_POLLIN); // All 'ZMQ_POLLIN's.
  256. CHECK_RC_EXIT_ON_FAILURE ();
  257. if (frontend_equal_to_backend) {
  258. // If frontend_==backend_ 'poller_send_blocked' and 'poller_receive_blocked' are the same,
  259. // so we don't need 'poller_send_blocked'. We need only 'poller_receive_blocked'.
  260. // We also don't need 'poller_both_blocked', no need to initialize it.
  261. rc = poller_receive_blocked->add (frontend_, NULL, ZMQ_POLLOUT);
  262. CHECK_RC_EXIT_ON_FAILURE ();
  263. } else {
  264. rc = poller_all->add (backend_, NULL,
  265. ZMQ_POLLIN | ZMQ_POLLOUT); // Everything.
  266. CHECK_RC_EXIT_ON_FAILURE ();
  267. rc = poller_in->add (backend_, NULL, ZMQ_POLLIN); // All 'ZMQ_POLLIN's.
  268. CHECK_RC_EXIT_ON_FAILURE ();
  269. rc = poller_both_blocked->add (
  270. frontend_, NULL, ZMQ_POLLOUT); // Waiting only for 'ZMQ_POLLOUT'.
  271. CHECK_RC_EXIT_ON_FAILURE ();
  272. rc = poller_both_blocked->add (
  273. backend_, NULL, ZMQ_POLLOUT); // Waiting only for 'ZMQ_POLLOUT'.
  274. CHECK_RC_EXIT_ON_FAILURE ();
  275. rc = poller_send_blocked->add (
  276. backend_, NULL,
  277. ZMQ_POLLOUT); // All except 'ZMQ_POLLIN' on 'backend_'.
  278. CHECK_RC_EXIT_ON_FAILURE ();
  279. rc = poller_send_blocked->add (
  280. frontend_, NULL,
  281. ZMQ_POLLIN | ZMQ_POLLOUT); // All except 'ZMQ_POLLIN' on 'backend_'.
  282. CHECK_RC_EXIT_ON_FAILURE ();
  283. rc = poller_receive_blocked->add (
  284. frontend_, NULL,
  285. ZMQ_POLLOUT); // All except 'ZMQ_POLLIN' on 'frontend_'.
  286. CHECK_RC_EXIT_ON_FAILURE ();
  287. rc = poller_receive_blocked->add (
  288. backend_, NULL,
  289. ZMQ_POLLIN | ZMQ_POLLOUT); // All except 'ZMQ_POLLIN' on 'frontend_'.
  290. CHECK_RC_EXIT_ON_FAILURE ();
  291. rc =
  292. poller_frontend_only->add (frontend_, NULL, ZMQ_POLLIN | ZMQ_POLLOUT);
  293. CHECK_RC_EXIT_ON_FAILURE ();
  294. rc =
  295. poller_backend_only->add (backend_, NULL, ZMQ_POLLIN | ZMQ_POLLOUT);
  296. CHECK_RC_EXIT_ON_FAILURE ();
  297. }
  298. // Register 'control_' with pollers.
  299. if (control_ != NULL) {
  300. rc = poller_all->add (control_, NULL, ZMQ_POLLIN);
  301. CHECK_RC_EXIT_ON_FAILURE ();
  302. rc = poller_in->add (control_, NULL, ZMQ_POLLIN);
  303. CHECK_RC_EXIT_ON_FAILURE ();
  304. rc = poller_control->add (
  305. control_, NULL,
  306. ZMQ_POLLIN); // When proxy is paused we wait only for ZMQ_POLLIN on 'control_' socket.
  307. CHECK_RC_EXIT_ON_FAILURE ();
  308. rc = poller_receive_blocked->add (control_, NULL, ZMQ_POLLIN);
  309. CHECK_RC_EXIT_ON_FAILURE ();
  310. if (!frontend_equal_to_backend) {
  311. rc = poller_send_blocked->add (control_, NULL, ZMQ_POLLIN);
  312. CHECK_RC_EXIT_ON_FAILURE ();
  313. rc = poller_both_blocked->add (control_, NULL, ZMQ_POLLIN);
  314. CHECK_RC_EXIT_ON_FAILURE ();
  315. rc = poller_frontend_only->add (control_, NULL, ZMQ_POLLIN);
  316. CHECK_RC_EXIT_ON_FAILURE ();
  317. rc = poller_backend_only->add (control_, NULL, ZMQ_POLLIN);
  318. CHECK_RC_EXIT_ON_FAILURE ();
  319. }
  320. }
  321. bool request_processed, reply_processed;
  322. while (state != terminated) {
  323. // Blocking wait initially only for 'ZMQ_POLLIN' - 'poller_wait' points to 'poller_in'.
  324. // If one of receiving end's queue is full ('ZMQ_POLLOUT' not available),
  325. // 'poller_wait' is pointed to 'poller_receive_blocked', 'poller_send_blocked' or 'poller_both_blocked'.
  326. rc = poller_wait->wait (events, 3, -1);
  327. if (rc < 0 && errno == EAGAIN)
  328. rc = 0;
  329. CHECK_RC_EXIT_ON_FAILURE ();
  330. // Some of events waited for by 'poller_wait' have arrived, now poll for everything without blocking.
  331. rc = poller_all->wait (events, 3, 0);
  332. if (rc < 0 && errno == EAGAIN)
  333. rc = 0;
  334. CHECK_RC_EXIT_ON_FAILURE ();
  335. // Process events.
  336. for (int i = 0; i < rc; i++) {
  337. if (events[i].socket == frontend_) {
  338. frontend_in = (events[i].events & ZMQ_POLLIN) != 0;
  339. frontend_out = (events[i].events & ZMQ_POLLOUT) != 0;
  340. } else
  341. // This 'if' needs to be after check for 'frontend_' in order never
  342. // to be reached in case frontend_==backend_, so we ensure backend_in=false in that case.
  343. if (events[i].socket == backend_) {
  344. backend_in = (events[i].events & ZMQ_POLLIN) != 0;
  345. backend_out = (events[i].events & ZMQ_POLLOUT) != 0;
  346. } else if (events[i].socket == control_)
  347. control_in = (events[i].events & ZMQ_POLLIN) != 0;
  348. }
  349. // Process a control command if any.
  350. if (control_in) {
  351. rc = control_->recv (&msg, 0);
  352. CHECK_RC_EXIT_ON_FAILURE ();
  353. rc = control_->getsockopt (ZMQ_RCVMORE, &more, &moresz);
  354. if (unlikely (rc < 0) || more) {
  355. PROXY_CLEANUP ();
  356. return close_and_return (&msg, -1);
  357. }
  358. // Copy message to capture socket if any.
  359. rc = capture (capture_, &msg);
  360. CHECK_RC_EXIT_ON_FAILURE ();
  361. if (msg.size () == 5 && memcmp (msg.data (), "PAUSE", 5) == 0) {
  362. state = paused;
  363. poller_wait = poller_control;
  364. } else if (msg.size () == 6
  365. && memcmp (msg.data (), "RESUME", 6) == 0) {
  366. state = active;
  367. poller_wait = poller_in;
  368. } else {
  369. if (msg.size () == 9
  370. && memcmp (msg.data (), "TERMINATE", 9) == 0)
  371. state = terminated;
  372. else {
  373. if (msg.size () == 10
  374. && memcmp (msg.data (), "STATISTICS", 10) == 0) {
  375. rc = reply_stats (control_, &frontend_stats,
  376. &backend_stats);
  377. CHECK_RC_EXIT_ON_FAILURE ();
  378. } else {
  379. // This is an API error, we assert
  380. puts ("E: invalid command sent to proxy");
  381. zmq_assert (false);
  382. }
  383. }
  384. }
  385. control_in = false;
  386. }
  387. if (state == active) {
  388. // Process a request, 'ZMQ_POLLIN' on 'frontend_' and 'ZMQ_POLLOUT' on 'backend_'.
  389. // In case of frontend_==backend_ there's no 'ZMQ_POLLOUT' event.
  390. if (frontend_in && (backend_out || frontend_equal_to_backend)) {
  391. rc = forward (frontend_, &frontend_stats, backend_,
  392. &backend_stats, capture_, &msg);
  393. CHECK_RC_EXIT_ON_FAILURE ();
  394. request_processed = true;
  395. frontend_in = backend_out = false;
  396. } else
  397. request_processed = false;
  398. // Process a reply, 'ZMQ_POLLIN' on 'backend_' and 'ZMQ_POLLOUT' on 'frontend_'.
  399. // If 'frontend_' and 'backend_' are the same this is not needed because previous processing
  400. // covers all of the cases. 'backend_in' is always false if frontend_==backend_ due to
  401. // design in 'for' event processing loop.
  402. if (backend_in && frontend_out) {
  403. rc = forward (backend_, &backend_stats, frontend_,
  404. &frontend_stats, capture_, &msg);
  405. CHECK_RC_EXIT_ON_FAILURE ();
  406. reply_processed = true;
  407. backend_in = frontend_out = false;
  408. } else
  409. reply_processed = false;
  410. if (request_processed || reply_processed) {
  411. // If request/reply is processed that means we had at least one 'ZMQ_POLLOUT' event.
  412. // Enable corresponding 'ZMQ_POLLIN' for blocking wait if any was disabled.
  413. if (poller_wait != poller_in) {
  414. if (request_processed) { // 'frontend_' -> 'backend_'
  415. if (poller_wait == poller_both_blocked)
  416. poller_wait = poller_send_blocked;
  417. else if (poller_wait == poller_receive_blocked
  418. || poller_wait == poller_frontend_only)
  419. poller_wait = poller_in;
  420. }
  421. if (reply_processed) { // 'backend_' -> 'frontend_'
  422. if (poller_wait == poller_both_blocked)
  423. poller_wait = poller_receive_blocked;
  424. else if (poller_wait == poller_send_blocked
  425. || poller_wait == poller_backend_only)
  426. poller_wait = poller_in;
  427. }
  428. }
  429. } else {
  430. // No requests have been processed, there were no 'ZMQ_POLLIN' with corresponding 'ZMQ_POLLOUT' events.
  431. // That means that out queue(s) is/are full or one out queue is full and second one has no messages to process.
  432. // Disable receiving 'ZMQ_POLLIN' for sockets for which there's no 'ZMQ_POLLOUT',
  433. // or wait only on both 'backend_''s or 'frontend_''s 'ZMQ_POLLIN' and 'ZMQ_POLLOUT'.
  434. if (frontend_in) {
  435. if (frontend_out)
  436. // If frontend_in and frontend_out are true, obviously backend_in and backend_out are both false.
  437. // In that case we need to wait for both 'ZMQ_POLLIN' and 'ZMQ_POLLOUT' only on 'backend_'.
  438. // We'll never get here in case of frontend_==backend_ because then frontend_out will always be false.
  439. poller_wait = poller_backend_only;
  440. else {
  441. if (poller_wait == poller_send_blocked)
  442. poller_wait = poller_both_blocked;
  443. else if (poller_wait == poller_in)
  444. poller_wait = poller_receive_blocked;
  445. }
  446. }
  447. if (backend_in) {
  448. // Will never be reached if frontend_==backend_, 'backend_in' will
  449. // always be false due to design in 'for' event processing loop.
  450. if (backend_out)
  451. // If backend_in and backend_out are true, obviously frontend_in and frontend_out are both false.
  452. // In that case we need to wait for both 'ZMQ_POLLIN' and 'ZMQ_POLLOUT' only on 'frontend_'.
  453. poller_wait = poller_frontend_only;
  454. else {
  455. if (poller_wait == poller_receive_blocked)
  456. poller_wait = poller_both_blocked;
  457. else if (poller_wait == poller_in)
  458. poller_wait = poller_send_blocked;
  459. }
  460. }
  461. }
  462. }
  463. }
  464. PROXY_CLEANUP ();
  465. return close_and_return (&msg, 0);
  466. }
  467. #else // ZMQ_HAVE_POLLER
  468. int zmq::proxy (class socket_base_t *frontend_,
  469. class socket_base_t *backend_,
  470. class socket_base_t *capture_,
  471. class socket_base_t *control_)
  472. {
  473. msg_t msg;
  474. int rc = msg.init ();
  475. if (rc != 0)
  476. return -1;
  477. // The algorithm below assumes ratio of requests and replies processed
  478. // under full load to be 1:1.
  479. int more;
  480. size_t moresz;
  481. zmq_pollitem_t items[] = {{frontend_, 0, ZMQ_POLLIN, 0},
  482. {backend_, 0, ZMQ_POLLIN, 0},
  483. {control_, 0, ZMQ_POLLIN, 0}};
  484. int qt_poll_items = (control_ ? 3 : 2);
  485. zmq_pollitem_t itemsout[] = {{frontend_, 0, ZMQ_POLLOUT, 0},
  486. {backend_, 0, ZMQ_POLLOUT, 0}};
  487. zmq_socket_stats_t frontend_stats;
  488. memset (&frontend_stats, 0, sizeof (frontend_stats));
  489. zmq_socket_stats_t backend_stats;
  490. memset (&backend_stats, 0, sizeof (backend_stats));
  491. // Proxy can be in these three states
  492. enum
  493. {
  494. active,
  495. paused,
  496. terminated
  497. } state = active;
  498. while (state != terminated) {
  499. // Wait while there are either requests or replies to process.
  500. rc = zmq_poll (&items[0], qt_poll_items, -1);
  501. if (unlikely (rc < 0))
  502. return close_and_return (&msg, -1);
  503. // Get the pollout separately because when combining this with pollin it maxes the CPU
  504. // because pollout shall most of the time return directly.
  505. // POLLOUT is only checked when frontend and backend sockets are not the same.
  506. if (frontend_ != backend_) {
  507. rc = zmq_poll (&itemsout[0], 2, 0);
  508. if (unlikely (rc < 0)) {
  509. return close_and_return (&msg, -1);
  510. }
  511. }
  512. // Process a control command if any
  513. if (control_ && items[2].revents & ZMQ_POLLIN) {
  514. rc = control_->recv (&msg, 0);
  515. if (unlikely (rc < 0))
  516. return close_and_return (&msg, -1);
  517. moresz = sizeof more;
  518. rc = control_->getsockopt (ZMQ_RCVMORE, &more, &moresz);
  519. if (unlikely (rc < 0) || more)
  520. return close_and_return (&msg, -1);
  521. // Copy message to capture socket if any
  522. rc = capture (capture_, &msg);
  523. if (unlikely (rc < 0))
  524. return close_and_return (&msg, -1);
  525. if (msg.size () == 5 && memcmp (msg.data (), "PAUSE", 5) == 0)
  526. state = paused;
  527. else if (msg.size () == 6 && memcmp (msg.data (), "RESUME", 6) == 0)
  528. state = active;
  529. else if (msg.size () == 9
  530. && memcmp (msg.data (), "TERMINATE", 9) == 0)
  531. state = terminated;
  532. else {
  533. if (msg.size () == 10
  534. && memcmp (msg.data (), "STATISTICS", 10) == 0) {
  535. rc =
  536. reply_stats (control_, &frontend_stats, &backend_stats);
  537. if (unlikely (rc < 0))
  538. return close_and_return (&msg, -1);
  539. } else {
  540. // This is an API error, we assert
  541. puts ("E: invalid command sent to proxy");
  542. zmq_assert (false);
  543. }
  544. }
  545. }
  546. // Process a request
  547. if (state == active && items[0].revents & ZMQ_POLLIN
  548. && (frontend_ == backend_ || itemsout[1].revents & ZMQ_POLLOUT)) {
  549. rc = forward (frontend_, &frontend_stats, backend_, &backend_stats,
  550. capture_, &msg);
  551. if (unlikely (rc < 0))
  552. return close_and_return (&msg, -1);
  553. }
  554. // Process a reply
  555. if (state == active && frontend_ != backend_
  556. && items[1].revents & ZMQ_POLLIN
  557. && itemsout[0].revents & ZMQ_POLLOUT) {
  558. rc = forward (backend_, &backend_stats, frontend_, &frontend_stats,
  559. capture_, &msg);
  560. if (unlikely (rc < 0))
  561. return close_and_return (&msg, -1);
  562. }
  563. }
  564. return close_and_return (&msg, 0);
  565. }
  566. #endif // ZMQ_HAVE_POLLER