session_base.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 "session_base.hpp"
  27. #include "i_engine.hpp"
  28. #include "err.hpp"
  29. #include "pipe.hpp"
  30. #include "likely.hpp"
  31. #include "tcp_connecter.hpp"
  32. #include "ws_connecter.hpp"
  33. #include "ipc_connecter.hpp"
  34. #include "tipc_connecter.hpp"
  35. #include "socks_connecter.hpp"
  36. #include "vmci_connecter.hpp"
  37. #include "pgm_sender.hpp"
  38. #include "pgm_receiver.hpp"
  39. #include "address.hpp"
  40. #include "norm_engine.hpp"
  41. #include "udp_engine.hpp"
  42. #include "ctx.hpp"
  43. #include "req.hpp"
  44. #include "radio.hpp"
  45. #include "dish.hpp"
  46. zmq::session_base_t *zmq::session_base_t::create (class io_thread_t *io_thread_,
  47. bool active_,
  48. class socket_base_t *socket_,
  49. const options_t &options_,
  50. address_t *addr_)
  51. {
  52. session_base_t *s = NULL;
  53. switch (options_.type) {
  54. case ZMQ_REQ:
  55. s = new (std::nothrow)
  56. req_session_t (io_thread_, active_, socket_, options_, addr_);
  57. break;
  58. case ZMQ_RADIO:
  59. s = new (std::nothrow)
  60. radio_session_t (io_thread_, active_, socket_, options_, addr_);
  61. break;
  62. case ZMQ_DISH:
  63. s = new (std::nothrow)
  64. dish_session_t (io_thread_, active_, socket_, options_, addr_);
  65. break;
  66. case ZMQ_DEALER:
  67. case ZMQ_REP:
  68. case ZMQ_ROUTER:
  69. case ZMQ_PUB:
  70. case ZMQ_XPUB:
  71. case ZMQ_SUB:
  72. case ZMQ_XSUB:
  73. case ZMQ_PUSH:
  74. case ZMQ_PULL:
  75. case ZMQ_PAIR:
  76. case ZMQ_STREAM:
  77. case ZMQ_SERVER:
  78. case ZMQ_CLIENT:
  79. case ZMQ_GATHER:
  80. case ZMQ_SCATTER:
  81. case ZMQ_DGRAM:
  82. case ZMQ_PEER:
  83. case ZMQ_CHANNEL:
  84. #ifdef ZMQ_BUILD_DRAFT_API
  85. if (options_.can_send_hello_msg && options_.hello_msg.size () > 0)
  86. s = new (std::nothrow) hello_msg_session_t (
  87. io_thread_, active_, socket_, options_, addr_);
  88. else
  89. s = new (std::nothrow) session_base_t (
  90. io_thread_, active_, socket_, options_, addr_);
  91. break;
  92. #else
  93. s = new (std::nothrow)
  94. session_base_t (io_thread_, active_, socket_, options_, addr_);
  95. break;
  96. #endif
  97. default:
  98. errno = EINVAL;
  99. return NULL;
  100. }
  101. alloc_assert (s);
  102. return s;
  103. }
  104. zmq::session_base_t::session_base_t (class io_thread_t *io_thread_,
  105. bool active_,
  106. class socket_base_t *socket_,
  107. const options_t &options_,
  108. address_t *addr_) :
  109. own_t (io_thread_, options_),
  110. io_object_t (io_thread_),
  111. _active (active_),
  112. _pipe (NULL),
  113. _zap_pipe (NULL),
  114. _incomplete_in (false),
  115. _pending (false),
  116. _engine (NULL),
  117. _socket (socket_),
  118. _io_thread (io_thread_),
  119. _has_linger_timer (false),
  120. _addr (addr_)
  121. #ifdef ZMQ_HAVE_WSS
  122. ,
  123. _wss_hostname (options_.wss_hostname)
  124. #endif
  125. {
  126. }
  127. const zmq::endpoint_uri_pair_t &zmq::session_base_t::get_endpoint () const
  128. {
  129. return _engine->get_endpoint ();
  130. }
  131. zmq::session_base_t::~session_base_t ()
  132. {
  133. zmq_assert (!_pipe);
  134. zmq_assert (!_zap_pipe);
  135. // If there's still a pending linger timer, remove it.
  136. if (_has_linger_timer) {
  137. cancel_timer (linger_timer_id);
  138. _has_linger_timer = false;
  139. }
  140. // Close the engine.
  141. if (_engine)
  142. _engine->terminate ();
  143. LIBZMQ_DELETE (_addr);
  144. }
  145. void zmq::session_base_t::attach_pipe (pipe_t *pipe_)
  146. {
  147. zmq_assert (!is_terminating ());
  148. zmq_assert (!_pipe);
  149. zmq_assert (pipe_);
  150. _pipe = pipe_;
  151. _pipe->set_event_sink (this);
  152. }
  153. int zmq::session_base_t::pull_msg (msg_t *msg_)
  154. {
  155. if (!_pipe || !_pipe->read (msg_)) {
  156. errno = EAGAIN;
  157. return -1;
  158. }
  159. _incomplete_in = (msg_->flags () & msg_t::more) != 0;
  160. return 0;
  161. }
  162. int zmq::session_base_t::push_msg (msg_t *msg_)
  163. {
  164. // pass subscribe/cancel to the sockets
  165. if ((msg_->flags () & msg_t::command) && !msg_->is_subscribe ()
  166. && !msg_->is_cancel ())
  167. return 0;
  168. if (_pipe && _pipe->write (msg_)) {
  169. const int rc = msg_->init ();
  170. errno_assert (rc == 0);
  171. return 0;
  172. }
  173. errno = EAGAIN;
  174. return -1;
  175. }
  176. int zmq::session_base_t::read_zap_msg (msg_t *msg_)
  177. {
  178. if (_zap_pipe == NULL) {
  179. errno = ENOTCONN;
  180. return -1;
  181. }
  182. if (!_zap_pipe->read (msg_)) {
  183. errno = EAGAIN;
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. int zmq::session_base_t::write_zap_msg (msg_t *msg_)
  189. {
  190. if (_zap_pipe == NULL || !_zap_pipe->write (msg_)) {
  191. errno = ENOTCONN;
  192. return -1;
  193. }
  194. if ((msg_->flags () & msg_t::more) == 0)
  195. _zap_pipe->flush ();
  196. const int rc = msg_->init ();
  197. errno_assert (rc == 0);
  198. return 0;
  199. }
  200. void zmq::session_base_t::reset ()
  201. {
  202. }
  203. void zmq::session_base_t::flush ()
  204. {
  205. if (_pipe)
  206. _pipe->flush ();
  207. }
  208. void zmq::session_base_t::rollback ()
  209. {
  210. if (_pipe)
  211. _pipe->rollback ();
  212. }
  213. void zmq::session_base_t::clean_pipes ()
  214. {
  215. zmq_assert (_pipe != NULL);
  216. // Get rid of half-processed messages in the out pipe. Flush any
  217. // unflushed messages upstream.
  218. _pipe->rollback ();
  219. _pipe->flush ();
  220. // Remove any half-read message from the in pipe.
  221. while (_incomplete_in) {
  222. msg_t msg;
  223. int rc = msg.init ();
  224. errno_assert (rc == 0);
  225. rc = pull_msg (&msg);
  226. errno_assert (rc == 0);
  227. rc = msg.close ();
  228. errno_assert (rc == 0);
  229. }
  230. }
  231. void zmq::session_base_t::pipe_terminated (pipe_t *pipe_)
  232. {
  233. // Drop the reference to the deallocated pipe if required.
  234. zmq_assert (pipe_ == _pipe || pipe_ == _zap_pipe
  235. || _terminating_pipes.count (pipe_) == 1);
  236. if (pipe_ == _pipe) {
  237. // If this is our current pipe, remove it
  238. _pipe = NULL;
  239. if (_has_linger_timer) {
  240. cancel_timer (linger_timer_id);
  241. _has_linger_timer = false;
  242. }
  243. } else if (pipe_ == _zap_pipe)
  244. _zap_pipe = NULL;
  245. else
  246. // Remove the pipe from the detached pipes set
  247. _terminating_pipes.erase (pipe_);
  248. if (!is_terminating () && options.raw_socket) {
  249. if (_engine) {
  250. _engine->terminate ();
  251. _engine = NULL;
  252. }
  253. terminate ();
  254. }
  255. // If we are waiting for pending messages to be sent, at this point
  256. // we are sure that there will be no more messages and we can proceed
  257. // with termination safely.
  258. if (_pending && !_pipe && !_zap_pipe && _terminating_pipes.empty ()) {
  259. _pending = false;
  260. own_t::process_term (0);
  261. }
  262. }
  263. void zmq::session_base_t::read_activated (pipe_t *pipe_)
  264. {
  265. // Skip activating if we're detaching this pipe
  266. if (unlikely (pipe_ != _pipe && pipe_ != _zap_pipe)) {
  267. zmq_assert (_terminating_pipes.count (pipe_) == 1);
  268. return;
  269. }
  270. if (unlikely (_engine == NULL)) {
  271. if (_pipe)
  272. _pipe->check_read ();
  273. return;
  274. }
  275. if (likely (pipe_ == _pipe))
  276. _engine->restart_output ();
  277. else {
  278. // i.e. pipe_ == zap_pipe
  279. _engine->zap_msg_available ();
  280. }
  281. }
  282. void zmq::session_base_t::write_activated (pipe_t *pipe_)
  283. {
  284. // Skip activating if we're detaching this pipe
  285. if (_pipe != pipe_) {
  286. zmq_assert (_terminating_pipes.count (pipe_) == 1);
  287. return;
  288. }
  289. if (_engine)
  290. _engine->restart_input ();
  291. }
  292. void zmq::session_base_t::hiccuped (pipe_t *)
  293. {
  294. // Hiccups are always sent from session to socket, not the other
  295. // way round.
  296. zmq_assert (false);
  297. }
  298. zmq::socket_base_t *zmq::session_base_t::get_socket () const
  299. {
  300. return _socket;
  301. }
  302. void zmq::session_base_t::process_plug ()
  303. {
  304. if (_active)
  305. start_connecting (false);
  306. }
  307. // This functions can return 0 on success or -1 and errno=ECONNREFUSED if ZAP
  308. // is not setup (IE: inproc://zeromq.zap.01 does not exist in the same context)
  309. // or it aborts on any other error. In other words, either ZAP is not
  310. // configured or if it is configured it MUST be configured correctly and it
  311. // MUST work, otherwise authentication cannot be guaranteed and it would be a
  312. // security flaw.
  313. int zmq::session_base_t::zap_connect ()
  314. {
  315. if (_zap_pipe != NULL)
  316. return 0;
  317. endpoint_t peer = find_endpoint ("inproc://zeromq.zap.01");
  318. if (peer.socket == NULL) {
  319. errno = ECONNREFUSED;
  320. return -1;
  321. }
  322. zmq_assert (peer.options.type == ZMQ_REP || peer.options.type == ZMQ_ROUTER
  323. || peer.options.type == ZMQ_SERVER);
  324. // Create a bi-directional pipe that will connect
  325. // session with zap socket.
  326. object_t *parents[2] = {this, peer.socket};
  327. pipe_t *new_pipes[2] = {NULL, NULL};
  328. int hwms[2] = {0, 0};
  329. bool conflates[2] = {false, false};
  330. int rc = pipepair (parents, new_pipes, hwms, conflates);
  331. errno_assert (rc == 0);
  332. // Attach local end of the pipe to this socket object.
  333. _zap_pipe = new_pipes[0];
  334. _zap_pipe->set_nodelay ();
  335. _zap_pipe->set_event_sink (this);
  336. send_bind (peer.socket, new_pipes[1], false);
  337. // Send empty routing id if required by the peer.
  338. if (peer.options.recv_routing_id) {
  339. msg_t id;
  340. rc = id.init ();
  341. errno_assert (rc == 0);
  342. id.set_flags (msg_t::routing_id);
  343. bool ok = _zap_pipe->write (&id);
  344. zmq_assert (ok);
  345. _zap_pipe->flush ();
  346. }
  347. return 0;
  348. }
  349. bool zmq::session_base_t::zap_enabled () const
  350. {
  351. return (options.mechanism != ZMQ_NULL || !options.zap_domain.empty ());
  352. }
  353. void zmq::session_base_t::process_attach (i_engine *engine_)
  354. {
  355. zmq_assert (engine_ != NULL);
  356. zmq_assert (!_engine);
  357. _engine = engine_;
  358. if (!engine_->has_handshake_stage ())
  359. engine_ready ();
  360. // Plug in the engine.
  361. _engine->plug (_io_thread, this);
  362. }
  363. void zmq::session_base_t::engine_ready ()
  364. {
  365. // Create the pipe if it does not exist yet.
  366. if (!_pipe && !is_terminating ()) {
  367. object_t *parents[2] = {this, _socket};
  368. pipe_t *pipes[2] = {NULL, NULL};
  369. const bool conflate = get_effective_conflate_option (options);
  370. int hwms[2] = {conflate ? -1 : options.rcvhwm,
  371. conflate ? -1 : options.sndhwm};
  372. bool conflates[2] = {conflate, conflate};
  373. const int rc = pipepair (parents, pipes, hwms, conflates);
  374. errno_assert (rc == 0);
  375. // Plug the local end of the pipe.
  376. pipes[0]->set_event_sink (this);
  377. // Remember the local end of the pipe.
  378. zmq_assert (!_pipe);
  379. _pipe = pipes[0];
  380. // The endpoints strings are not set on bind, set them here so that
  381. // events can use them.
  382. pipes[0]->set_endpoint_pair (_engine->get_endpoint ());
  383. pipes[1]->set_endpoint_pair (_engine->get_endpoint ());
  384. // Ask socket to plug into the remote end of the pipe.
  385. send_bind (_socket, pipes[1]);
  386. }
  387. }
  388. void zmq::session_base_t::engine_error (bool handshaked_,
  389. zmq::i_engine::error_reason_t reason_)
  390. {
  391. // Engine is dead. Let's forget about it.
  392. _engine = NULL;
  393. // Remove any half-done messages from the pipes.
  394. if (_pipe) {
  395. clean_pipes ();
  396. #ifdef ZMQ_BUILD_DRAFT_API
  397. // Only send disconnect message if socket was accepted and handshake was completed
  398. if (!_active && handshaked_ && options.can_recv_disconnect_msg
  399. && !options.disconnect_msg.empty ()) {
  400. _pipe->set_disconnect_msg (options.disconnect_msg);
  401. _pipe->send_disconnect_msg ();
  402. }
  403. #endif
  404. }
  405. zmq_assert (reason_ == i_engine::connection_error
  406. || reason_ == i_engine::timeout_error
  407. || reason_ == i_engine::protocol_error);
  408. switch (reason_) {
  409. case i_engine::timeout_error:
  410. /* FALLTHROUGH */
  411. case i_engine::connection_error:
  412. if (_active) {
  413. reconnect ();
  414. break;
  415. }
  416. case i_engine::protocol_error:
  417. if (_pending) {
  418. if (_pipe)
  419. _pipe->terminate (false);
  420. if (_zap_pipe)
  421. _zap_pipe->terminate (false);
  422. } else {
  423. terminate ();
  424. }
  425. break;
  426. }
  427. // Just in case there's only a delimiter in the pipe.
  428. if (_pipe)
  429. _pipe->check_read ();
  430. if (_zap_pipe)
  431. _zap_pipe->check_read ();
  432. }
  433. void zmq::session_base_t::process_term (int linger_)
  434. {
  435. zmq_assert (!_pending);
  436. // If the termination of the pipe happens before the term command is
  437. // delivered there's nothing much to do. We can proceed with the
  438. // standard termination immediately.
  439. if (!_pipe && !_zap_pipe && _terminating_pipes.empty ()) {
  440. own_t::process_term (0);
  441. return;
  442. }
  443. _pending = true;
  444. if (_pipe != NULL) {
  445. // If there's finite linger value, delay the termination.
  446. // If linger is infinite (negative) we don't even have to set
  447. // the timer.
  448. if (linger_ > 0) {
  449. zmq_assert (!_has_linger_timer);
  450. add_timer (linger_, linger_timer_id);
  451. _has_linger_timer = true;
  452. }
  453. // Start pipe termination process. Delay the termination till all messages
  454. // are processed in case the linger time is non-zero.
  455. _pipe->terminate (linger_ != 0);
  456. // TODO: Should this go into pipe_t::terminate ?
  457. // In case there's no engine and there's only delimiter in the
  458. // pipe it wouldn't be ever read. Thus we check for it explicitly.
  459. if (!_engine)
  460. _pipe->check_read ();
  461. }
  462. if (_zap_pipe != NULL)
  463. _zap_pipe->terminate (false);
  464. }
  465. void zmq::session_base_t::timer_event (int id_)
  466. {
  467. // Linger period expired. We can proceed with termination even though
  468. // there are still pending messages to be sent.
  469. zmq_assert (id_ == linger_timer_id);
  470. _has_linger_timer = false;
  471. // Ask pipe to terminate even though there may be pending messages in it.
  472. zmq_assert (_pipe);
  473. _pipe->terminate (false);
  474. }
  475. void zmq::session_base_t::process_conn_failed ()
  476. {
  477. std::string *ep = new (std::string);
  478. _addr->to_string (*ep);
  479. send_term_endpoint (_socket, ep);
  480. }
  481. void zmq::session_base_t::reconnect ()
  482. {
  483. // For delayed connect situations, terminate the pipe
  484. // and reestablish later on
  485. if (_pipe && options.immediate == 1
  486. #ifdef ZMQ_HAVE_OPENPGM
  487. && _addr->protocol != protocol_name::pgm
  488. && _addr->protocol != protocol_name::epgm
  489. #endif
  490. #ifdef ZMQ_HAVE_NORM
  491. && _addr->protocol != protocol_name::norm
  492. #endif
  493. && _addr->protocol != protocol_name::udp) {
  494. _pipe->hiccup ();
  495. _pipe->terminate (false);
  496. _terminating_pipes.insert (_pipe);
  497. _pipe = NULL;
  498. if (_has_linger_timer) {
  499. cancel_timer (linger_timer_id);
  500. _has_linger_timer = false;
  501. }
  502. }
  503. reset ();
  504. // Reconnect.
  505. if (options.reconnect_ivl > 0)
  506. start_connecting (true);
  507. else {
  508. std::string *ep = new (std::string);
  509. _addr->to_string (*ep);
  510. send_term_endpoint (_socket, ep);
  511. }
  512. // For subscriber sockets we hiccup the inbound pipe, which will cause
  513. // the socket object to resend all the subscriptions.
  514. if (_pipe
  515. && (options.type == ZMQ_SUB || options.type == ZMQ_XSUB
  516. || options.type == ZMQ_DISH))
  517. _pipe->hiccup ();
  518. }
  519. void zmq::session_base_t::start_connecting (bool wait_)
  520. {
  521. zmq_assert (_active);
  522. // Choose I/O thread to run connecter in. Given that we are already
  523. // running in an I/O thread, there must be at least one available.
  524. io_thread_t *io_thread = choose_io_thread (options.affinity);
  525. zmq_assert (io_thread);
  526. // Create the connecter object.
  527. own_t *connecter = NULL;
  528. if (_addr->protocol == protocol_name::tcp) {
  529. if (!options.socks_proxy_address.empty ()) {
  530. address_t *proxy_address = new (std::nothrow)
  531. address_t (protocol_name::tcp, options.socks_proxy_address,
  532. this->get_ctx ());
  533. alloc_assert (proxy_address);
  534. connecter = new (std::nothrow) socks_connecter_t (
  535. io_thread, this, options, _addr, proxy_address, wait_);
  536. alloc_assert (connecter);
  537. if (!options.socks_proxy_username.empty ()) {
  538. reinterpret_cast<socks_connecter_t *> (connecter)
  539. ->set_auth_method_basic (options.socks_proxy_username,
  540. options.socks_proxy_password);
  541. }
  542. } else {
  543. connecter = new (std::nothrow)
  544. tcp_connecter_t (io_thread, this, options, _addr, wait_);
  545. }
  546. }
  547. #if defined ZMQ_HAVE_IPC
  548. else if (_addr->protocol == protocol_name::ipc) {
  549. connecter = new (std::nothrow)
  550. ipc_connecter_t (io_thread, this, options, _addr, wait_);
  551. }
  552. #endif
  553. #if defined ZMQ_HAVE_TIPC
  554. else if (_addr->protocol == protocol_name::tipc) {
  555. connecter = new (std::nothrow)
  556. tipc_connecter_t (io_thread, this, options, _addr, wait_);
  557. }
  558. #endif
  559. #if defined ZMQ_HAVE_VMCI
  560. else if (_addr->protocol == protocol_name::vmci) {
  561. connecter = new (std::nothrow)
  562. vmci_connecter_t (io_thread, this, options, _addr, wait_);
  563. }
  564. #endif
  565. #if defined ZMQ_HAVE_WS
  566. else if (_addr->protocol == protocol_name::ws) {
  567. connecter = new (std::nothrow) ws_connecter_t (
  568. io_thread, this, options, _addr, wait_, false, std::string ());
  569. }
  570. #endif
  571. #if defined ZMQ_HAVE_WSS
  572. else if (_addr->protocol == protocol_name::wss) {
  573. connecter = new (std::nothrow) ws_connecter_t (
  574. io_thread, this, options, _addr, wait_, true, _wss_hostname);
  575. }
  576. #endif
  577. if (connecter != NULL) {
  578. alloc_assert (connecter);
  579. launch_child (connecter);
  580. return;
  581. }
  582. if (_addr->protocol == protocol_name::udp) {
  583. zmq_assert (options.type == ZMQ_DISH || options.type == ZMQ_RADIO
  584. || options.type == ZMQ_DGRAM);
  585. udp_engine_t *engine = new (std::nothrow) udp_engine_t (options);
  586. alloc_assert (engine);
  587. bool recv = false;
  588. bool send = false;
  589. if (options.type == ZMQ_RADIO) {
  590. send = true;
  591. recv = false;
  592. } else if (options.type == ZMQ_DISH) {
  593. send = false;
  594. recv = true;
  595. } else if (options.type == ZMQ_DGRAM) {
  596. send = true;
  597. recv = true;
  598. }
  599. int rc = engine->init (_addr, send, recv);
  600. errno_assert (rc == 0);
  601. send_attach (this, engine);
  602. return;
  603. }
  604. #ifdef ZMQ_HAVE_OPENPGM
  605. // Both PGM and EPGM transports are using the same infrastructure.
  606. if (_addr->protocol == "pgm" || _addr->protocol == "epgm") {
  607. zmq_assert (options.type == ZMQ_PUB || options.type == ZMQ_XPUB
  608. || options.type == ZMQ_SUB || options.type == ZMQ_XSUB);
  609. // For EPGM transport with UDP encapsulation of PGM is used.
  610. bool const udp_encapsulation = _addr->protocol == "epgm";
  611. // At this point we'll create message pipes to the session straight
  612. // away. There's no point in delaying it as no concept of 'connect'
  613. // exists with PGM anyway.
  614. if (options.type == ZMQ_PUB || options.type == ZMQ_XPUB) {
  615. // PGM sender.
  616. pgm_sender_t *pgm_sender =
  617. new (std::nothrow) pgm_sender_t (io_thread, options);
  618. alloc_assert (pgm_sender);
  619. int rc =
  620. pgm_sender->init (udp_encapsulation, _addr->address.c_str ());
  621. errno_assert (rc == 0);
  622. send_attach (this, pgm_sender);
  623. } else {
  624. // PGM receiver.
  625. pgm_receiver_t *pgm_receiver =
  626. new (std::nothrow) pgm_receiver_t (io_thread, options);
  627. alloc_assert (pgm_receiver);
  628. int rc =
  629. pgm_receiver->init (udp_encapsulation, _addr->address.c_str ());
  630. errno_assert (rc == 0);
  631. send_attach (this, pgm_receiver);
  632. }
  633. return;
  634. }
  635. #endif
  636. #ifdef ZMQ_HAVE_NORM
  637. if (_addr->protocol == "norm") {
  638. // At this point we'll create message pipes to the session straight
  639. // away. There's no point in delaying it as no concept of 'connect'
  640. // exists with NORM anyway.
  641. if (options.type == ZMQ_PUB || options.type == ZMQ_XPUB) {
  642. // NORM sender.
  643. norm_engine_t *norm_sender =
  644. new (std::nothrow) norm_engine_t (io_thread, options);
  645. alloc_assert (norm_sender);
  646. int rc = norm_sender->init (_addr->address.c_str (), true, false);
  647. errno_assert (rc == 0);
  648. send_attach (this, norm_sender);
  649. } else { // ZMQ_SUB or ZMQ_XSUB
  650. // NORM receiver.
  651. norm_engine_t *norm_receiver =
  652. new (std::nothrow) norm_engine_t (io_thread, options);
  653. alloc_assert (norm_receiver);
  654. int rc = norm_receiver->init (_addr->address.c_str (), false, true);
  655. errno_assert (rc == 0);
  656. send_attach (this, norm_receiver);
  657. }
  658. return;
  659. }
  660. #endif // ZMQ_HAVE_NORM
  661. zmq_assert (false);
  662. }
  663. zmq::hello_msg_session_t::hello_msg_session_t (io_thread_t *io_thread_,
  664. bool connect_,
  665. socket_base_t *socket_,
  666. const options_t &options_,
  667. address_t *addr_) :
  668. session_base_t (io_thread_, connect_, socket_, options_, addr_),
  669. _new_pipe (true)
  670. {
  671. }
  672. zmq::hello_msg_session_t::~hello_msg_session_t ()
  673. {
  674. }
  675. int zmq::hello_msg_session_t::pull_msg (msg_t *msg_)
  676. {
  677. if (_new_pipe) {
  678. _new_pipe = false;
  679. const int rc =
  680. msg_->init_buffer (&options.hello_msg[0], options.hello_msg.size ());
  681. errno_assert (rc == 0);
  682. return 0;
  683. }
  684. return session_base_t::pull_msg (msg_);
  685. }
  686. void zmq::hello_msg_session_t::reset ()
  687. {
  688. session_base_t::reset ();
  689. _new_pipe = true;
  690. }