zmtp_engine.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright (c) 2007-2019 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_ZMTP_ENGINE_HPP_INCLUDED__
  25. #define __ZMQ_ZMTP_ENGINE_HPP_INCLUDED__
  26. #include <stddef.h>
  27. #include "fd.hpp"
  28. #include "i_engine.hpp"
  29. #include "io_object.hpp"
  30. #include "i_encoder.hpp"
  31. #include "i_decoder.hpp"
  32. #include "options.hpp"
  33. #include "socket_base.hpp"
  34. #include "metadata.hpp"
  35. #include "msg.hpp"
  36. #include "stream_engine_base.hpp"
  37. namespace zmq
  38. {
  39. // Protocol revisions
  40. enum
  41. {
  42. ZMTP_1_0 = 0,
  43. ZMTP_2_0 = 1,
  44. ZMTP_3_x = 3
  45. };
  46. class io_thread_t;
  47. class session_base_t;
  48. class mechanism_t;
  49. // This engine handles any socket with SOCK_STREAM semantics,
  50. // e.g. TCP socket or an UNIX domain socket.
  51. class zmtp_engine_t ZMQ_FINAL : public stream_engine_base_t
  52. {
  53. public:
  54. zmtp_engine_t (fd_t fd_,
  55. const options_t &options_,
  56. const endpoint_uri_pair_t &endpoint_uri_pair_);
  57. ~zmtp_engine_t ();
  58. protected:
  59. // Detects the protocol used by the peer.
  60. bool handshake ();
  61. void plug_internal ();
  62. int process_command_message (msg_t *msg_);
  63. int produce_ping_message (msg_t *msg_);
  64. int process_heartbeat_message (msg_t *msg_);
  65. int produce_pong_message (msg_t *msg_);
  66. private:
  67. // Receive the greeting from the peer.
  68. int receive_greeting ();
  69. void receive_greeting_versioned ();
  70. typedef bool (zmtp_engine_t::*handshake_fun_t) ();
  71. static handshake_fun_t select_handshake_fun (bool unversioned,
  72. unsigned char revision,
  73. unsigned char minor);
  74. bool handshake_v1_0_unversioned ();
  75. bool handshake_v1_0 ();
  76. bool handshake_v2_0 ();
  77. bool handshake_v3_x (bool downgrade_sub);
  78. bool handshake_v3_0 ();
  79. bool handshake_v3_1 ();
  80. int routing_id_msg (msg_t *msg_);
  81. int process_routing_id_msg (msg_t *msg_);
  82. msg_t _routing_id_msg;
  83. // Need to store PING payload for PONG
  84. msg_t _pong_msg;
  85. static const size_t signature_size = 10;
  86. // Size of ZMTP/1.0 and ZMTP/2.0 greeting message
  87. static const size_t v2_greeting_size = 12;
  88. // Size of ZMTP/3.0 greeting message
  89. static const size_t v3_greeting_size = 64;
  90. // Expected greeting size.
  91. size_t _greeting_size;
  92. // Greeting received from, and sent to peer
  93. unsigned char _greeting_recv[v3_greeting_size];
  94. unsigned char _greeting_send[v3_greeting_size];
  95. // Size of greeting received so far
  96. unsigned int _greeting_bytes_read;
  97. // Indicates whether the engine is to inject a phantom
  98. // subscription message into the incoming stream.
  99. // Needed to support old peers.
  100. bool _subscription_required;
  101. int _heartbeat_timeout;
  102. ZMQ_NON_COPYABLE_NOR_MOVABLE (zmtp_engine_t)
  103. };
  104. }
  105. #endif