keep_alive_handler.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_HANDLER_H_
  5. #define COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_HANDLER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/threading/thread_checker.h"
  9. #include "base/time/time.h"
  10. #include "base/timer/timer.h"
  11. #include "components/cast_channel/cast_channel_enum.h"
  12. #include "components/cast_channel/cast_message_util.h"
  13. namespace cast_channel {
  14. class CastSocket;
  15. class Logger;
  16. using ::cast::channel::CastMessage;
  17. class KeepAliveHandler {
  18. public:
  19. using OnErrorCallback = base::RepeatingCallback<void(ChannelError)>;
  20. // |socket|: The socket to be kept alive.
  21. // |logger|: The logging object which collects protocol events and error
  22. // details.
  23. // |ping_interval|: The amount of idle time to wait before sending a PING to
  24. // the remote end.
  25. // |liveness_timeout|: The amount of idle time to wait before terminating the
  26. // connection.
  27. KeepAliveHandler(CastSocket* socket,
  28. scoped_refptr<Logger> logger,
  29. base::TimeDelta ping_interval,
  30. base::TimeDelta liveness_timeout,
  31. OnErrorCallback on_error_cb);
  32. KeepAliveHandler(const KeepAliveHandler&) = delete;
  33. KeepAliveHandler& operator=(const KeepAliveHandler&) = delete;
  34. ~KeepAliveHandler();
  35. // Restarts the ping/liveness timeout timers. Called when a message
  36. // is received from the remote end.
  37. void ResetTimers();
  38. void SetTimersForTest(
  39. std::unique_ptr<base::RetainingOneShotTimer> injected_ping_timer,
  40. std::unique_ptr<base::RetainingOneShotTimer> injected_liveness_timer);
  41. void Start();
  42. // Stops the ping and liveness timers if they are started.
  43. // To be called after an error.
  44. void Stop();
  45. bool HandleMessage(const CastMessage& message);
  46. private:
  47. // Sends a formatted PING or PONG message to the remote side.
  48. void SendKeepAliveMessage(const CastMessage& message,
  49. CastMessageType message_type);
  50. // Callback for SendKeepAliveMessage.
  51. void SendKeepAliveMessageComplete(CastMessageType message_type, int rv);
  52. // Called when the liveness timer expires, indicating that the remote
  53. // end has not responded within the |liveness_timeout_| interval.
  54. void LivenessTimeout();
  55. // Indicates that Start() was called.
  56. bool started_;
  57. // Socket that is managed by the keep-alive object.
  58. raw_ptr<CastSocket> socket_;
  59. // Logging object.
  60. scoped_refptr<Logger> logger_;
  61. // Amount of idle time to wait before disconnecting.
  62. base::TimeDelta liveness_timeout_;
  63. // Amount of idle time to wait before pinging the receiver.
  64. base::TimeDelta ping_interval_;
  65. // Fired when |ping_interval_| is exceeded or when triggered by test code.
  66. std::unique_ptr<base::RetainingOneShotTimer> ping_timer_;
  67. // Fired when |liveness_timer_| is exceeded.
  68. std::unique_ptr<base::RetainingOneShotTimer> liveness_timer_;
  69. // The PING message to send over the wire.
  70. const CastMessage ping_message_;
  71. // The PONG message to send over the wire.
  72. const CastMessage pong_message_;
  73. OnErrorCallback on_error_cb_;
  74. THREAD_CHECKER(thread_checker_);
  75. base::WeakPtrFactory<KeepAliveHandler> weak_factory_{this};
  76. };
  77. } // namespace cast_channel
  78. #endif // COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_HANDLER_H_