session_base.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #ifndef __ZMQ_SESSION_BASE_HPP_INCLUDED__
  25. #define __ZMQ_SESSION_BASE_HPP_INCLUDED__
  26. #include <stdarg.h>
  27. #include "own.hpp"
  28. #include "io_object.hpp"
  29. #include "pipe.hpp"
  30. #include "socket_base.hpp"
  31. #include "i_engine.hpp"
  32. #include "msg.hpp"
  33. namespace zmq
  34. {
  35. class io_thread_t;
  36. struct i_engine;
  37. struct address_t;
  38. class session_base_t : public own_t, public io_object_t, public i_pipe_events
  39. {
  40. public:
  41. // Create a session of the particular type.
  42. static session_base_t *create (zmq::io_thread_t *io_thread_,
  43. bool active_,
  44. zmq::socket_base_t *socket_,
  45. const options_t &options_,
  46. address_t *addr_);
  47. // To be used once only, when creating the session.
  48. void attach_pipe (zmq::pipe_t *pipe_);
  49. // Following functions are the interface exposed towards the engine.
  50. virtual void reset ();
  51. void flush ();
  52. void rollback ();
  53. void engine_error (bool handshaked_, zmq::i_engine::error_reason_t reason_);
  54. void engine_ready ();
  55. // i_pipe_events interface implementation.
  56. void read_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  57. void write_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  58. void hiccuped (zmq::pipe_t *pipe_) ZMQ_FINAL;
  59. void pipe_terminated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  60. // Delivers a message. Returns 0 if successful; -1 otherwise.
  61. // The function takes ownership of the message.
  62. virtual int push_msg (msg_t *msg_);
  63. int zap_connect ();
  64. bool zap_enabled () const;
  65. // Fetches a message. Returns 0 if successful; -1 otherwise.
  66. // The caller is responsible for freeing the message when no
  67. // longer used.
  68. virtual int pull_msg (msg_t *msg_);
  69. // Receives message from ZAP socket.
  70. // Returns 0 on success; -1 otherwise.
  71. // The caller is responsible for freeing the message.
  72. int read_zap_msg (msg_t *msg_);
  73. // Sends message to ZAP socket.
  74. // Returns 0 on success; -1 otherwise.
  75. // The function takes ownership of the message.
  76. int write_zap_msg (msg_t *msg_);
  77. socket_base_t *get_socket () const;
  78. const endpoint_uri_pair_t &get_endpoint () const;
  79. protected:
  80. session_base_t (zmq::io_thread_t *io_thread_,
  81. bool active_,
  82. zmq::socket_base_t *socket_,
  83. const options_t &options_,
  84. address_t *addr_);
  85. ~session_base_t () ZMQ_OVERRIDE;
  86. private:
  87. void start_connecting (bool wait_);
  88. void reconnect ();
  89. // Handlers for incoming commands.
  90. void process_plug () ZMQ_FINAL;
  91. void process_attach (zmq::i_engine *engine_) ZMQ_FINAL;
  92. void process_term (int linger_) ZMQ_FINAL;
  93. void process_conn_failed () ZMQ_OVERRIDE;
  94. // i_poll_events handlers.
  95. void timer_event (int id_) ZMQ_FINAL;
  96. // Remove any half processed messages. Flush unflushed messages.
  97. // Call this function when engine disconnect to get rid of leftovers.
  98. void clean_pipes ();
  99. // If true, this session (re)connects to the peer. Otherwise, it's
  100. // a transient session created by the listener.
  101. const bool _active;
  102. // Pipe connecting the session to its socket.
  103. zmq::pipe_t *_pipe;
  104. // Pipe used to exchange messages with ZAP socket.
  105. zmq::pipe_t *_zap_pipe;
  106. // This set is added to with pipes we are disconnecting, but haven't yet completed
  107. std::set<pipe_t *> _terminating_pipes;
  108. // This flag is true if the remainder of the message being processed
  109. // is still in the in pipe.
  110. bool _incomplete_in;
  111. // True if termination have been suspended to push the pending
  112. // messages to the network.
  113. bool _pending;
  114. // The protocol I/O engine connected to the session.
  115. zmq::i_engine *_engine;
  116. // The socket the session belongs to.
  117. zmq::socket_base_t *_socket;
  118. // I/O thread the session is living in. It will be used to plug in
  119. // the engines into the same thread.
  120. zmq::io_thread_t *_io_thread;
  121. // ID of the linger timer
  122. enum
  123. {
  124. linger_timer_id = 0x20
  125. };
  126. // True is linger timer is running.
  127. bool _has_linger_timer;
  128. // Protocol and address to use when connecting.
  129. address_t *_addr;
  130. #ifdef ZMQ_HAVE_WSS
  131. // TLS handshake, we need to take a copy when the session is created,
  132. // in order to maintain the value at the creation time
  133. const std::string _wss_hostname;
  134. #endif
  135. ZMQ_NON_COPYABLE_NOR_MOVABLE (session_base_t)
  136. };
  137. class hello_msg_session_t ZMQ_FINAL : public session_base_t
  138. {
  139. public:
  140. hello_msg_session_t (zmq::io_thread_t *io_thread_,
  141. bool connect_,
  142. zmq::socket_base_t *socket_,
  143. const options_t &options_,
  144. address_t *addr_);
  145. ~hello_msg_session_t ();
  146. // Overrides of the functions from session_base_t.
  147. int pull_msg (msg_t *msg_);
  148. void reset ();
  149. private:
  150. bool _new_pipe;
  151. ZMQ_NON_COPYABLE_NOR_MOVABLE (hello_msg_session_t)
  152. };
  153. }
  154. #endif