stream_connecter_base.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 __STREAM_CONNECTER_BASE_HPP_INCLUDED__
  25. #define __STREAM_CONNECTER_BASE_HPP_INCLUDED__
  26. #include "fd.hpp"
  27. #include "own.hpp"
  28. #include "io_object.hpp"
  29. namespace zmq
  30. {
  31. class io_thread_t;
  32. class session_base_t;
  33. struct address_t;
  34. class stream_connecter_base_t : public own_t, public io_object_t
  35. {
  36. public:
  37. // If 'delayed_start' is true connecter first waits for a while,
  38. // then starts connection process.
  39. stream_connecter_base_t (zmq::io_thread_t *io_thread_,
  40. zmq::session_base_t *session_,
  41. const options_t &options_,
  42. address_t *addr_,
  43. bool delayed_start_);
  44. ~stream_connecter_base_t () ZMQ_OVERRIDE;
  45. protected:
  46. // Handlers for incoming commands.
  47. void process_plug () ZMQ_FINAL;
  48. void process_term (int linger_) ZMQ_OVERRIDE;
  49. // Handlers for I/O events.
  50. void in_event () ZMQ_OVERRIDE;
  51. void timer_event (int id_) ZMQ_OVERRIDE;
  52. // Internal function to create the engine after connection was established.
  53. virtual void create_engine (fd_t fd, const std::string &local_address_);
  54. // Internal function to add a reconnect timer
  55. void add_reconnect_timer ();
  56. // Removes the handle from the poller.
  57. void rm_handle ();
  58. // Close the connecting socket.
  59. void close ();
  60. // Address to connect to. Owned by session_base_t.
  61. // It is non-const since some parts may change during opening.
  62. address_t *const _addr;
  63. // Underlying socket.
  64. fd_t _s;
  65. // Handle corresponding to the listening socket, if file descriptor is
  66. // registered with the poller, or NULL.
  67. handle_t _handle;
  68. // String representation of endpoint to connect to
  69. std::string _endpoint;
  70. // Socket
  71. zmq::socket_base_t *const _socket;
  72. private:
  73. // ID of the timer used to delay the reconnection.
  74. enum
  75. {
  76. reconnect_timer_id = 1
  77. };
  78. // Internal function to return a reconnect backoff delay.
  79. // Will modify the current_reconnect_ivl used for next call
  80. // Returns the currently used interval
  81. int get_new_reconnect_ivl ();
  82. virtual void start_connecting () = 0;
  83. // If true, connecter is waiting a while before trying to connect.
  84. const bool _delayed_start;
  85. // True iff a timer has been started.
  86. bool _reconnect_timer_started;
  87. // Current reconnect ivl, updated for backoff strategy
  88. int _current_reconnect_ivl;
  89. ZMQ_NON_COPYABLE_NOR_MOVABLE (stream_connecter_base_t)
  90. protected:
  91. // Reference to the session we belong to.
  92. zmq::session_base_t *const _session;
  93. };
  94. }
  95. #endif