ftl_message_reception_channel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2019 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 REMOTING_SIGNALING_FTL_MESSAGE_RECEPTION_CHANNEL_H_
  5. #define REMOTING_SIGNALING_FTL_MESSAGE_RECEPTION_CHANNEL_H_
  6. #include <list>
  7. #include <memory>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. #include "net/base/backoff_entry.h"
  14. #include "remoting/signaling/message_reception_channel.h"
  15. #include "remoting/signaling/signaling_tracker.h"
  16. namespace remoting {
  17. // Handles the lifetime and validity of the messaging stream used for FTL.
  18. class FtlMessageReceptionChannel final : public MessageReceptionChannel {
  19. public:
  20. static constexpr base::TimeDelta kPongTimeout = base::Seconds(15);
  21. // |signaling_tracker| is nullable.
  22. explicit FtlMessageReceptionChannel(SignalingTracker* signaling_tracker);
  23. FtlMessageReceptionChannel(const FtlMessageReceptionChannel&) = delete;
  24. FtlMessageReceptionChannel& operator=(const FtlMessageReceptionChannel&) =
  25. delete;
  26. ~FtlMessageReceptionChannel() override;
  27. // MessageReceptionChannel implementations.
  28. void Initialize(const StreamOpener& stream_opener,
  29. const MessageCallback& on_incoming_msg) override;
  30. void StartReceivingMessages(base::OnceClosure on_ready,
  31. DoneCallback on_closed) override;
  32. void StopReceivingMessages() override;
  33. bool IsReceivingMessages() const override;
  34. const net::BackoffEntry& GetReconnectRetryBackoffEntryForTesting() const;
  35. private:
  36. enum class State {
  37. // TODO(yuweih): Evaluate if this class needs to be reusable.
  38. STOPPED,
  39. // StartReceivingMessages() is called but the channel hasn't received a
  40. // signal from the server yet.
  41. STARTING,
  42. // Stream is started, or is dropped but being retried.
  43. STARTED,
  44. };
  45. void OnReceiveMessagesStreamReady();
  46. void OnReceiveMessagesStreamClosed(const ProtobufHttpStatus& status);
  47. void OnMessageReceived(
  48. std::unique_ptr<ftl::ReceiveMessagesResponse> response);
  49. void RunStreamReadyCallbacks();
  50. void RunStreamClosedCallbacks(const ProtobufHttpStatus& status);
  51. void RetryStartReceivingMessagesWithBackoff();
  52. void RetryStartReceivingMessages();
  53. void StartReceivingMessagesInternal();
  54. void StopReceivingMessagesInternal();
  55. void BeginStreamTimers();
  56. void OnPongTimeout();
  57. StreamOpener stream_opener_;
  58. MessageCallback on_incoming_msg_;
  59. std::unique_ptr<ScopedProtobufHttpRequest> receive_messages_stream_;
  60. std::list<base::OnceClosure> stream_ready_callbacks_;
  61. std::list<DoneCallback> stream_closed_callbacks_;
  62. State state_ = State::STOPPED;
  63. net::BackoffEntry reconnect_retry_backoff_;
  64. base::OneShotTimer reconnect_retry_timer_;
  65. std::unique_ptr<base::DelayTimer> stream_pong_timer_;
  66. raw_ptr<SignalingTracker> signaling_tracker_; // nullable.
  67. base::WeakPtrFactory<FtlMessageReceptionChannel> weak_factory_{this};
  68. };
  69. } // namespace remoting
  70. #endif // REMOTING_SIGNALING_FTL_MESSAGE_RECEPTION_CHANNEL_H_