| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 | |
| 5 #ifndef COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_ | |
| 6 #define COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/threading/thread_checker.h" | |
| 10 #include "base/timer/timer.h" | |
| 11 #include "components/cast_channel/cast_transport.h" | |
| 12 #include "components/cast_channel/proto/cast_channel.pb.h" | |
| 13 | |
| 14 namespace cast_channel { | |
| 15 | |
| 16 class CastSocket; | |
| 17 class Logger; | |
| 18 | |
| 19 // Decorator delegate which provides keep-alive functionality. | |
| 20 // Keep-alive messages are handled by this object; all other messages and | |
| 21 // errors are passed to |inner_delegate_|. | |
| 22 class KeepAliveDelegate : public CastTransport::Delegate { | |
| 23 public: | |
| 24 // |socket|: The socket to be kept alive. | |
| 25 // |logger|: The logging object which collects protocol events and error | |
| 26 // details. | |
| 27 // |inner_delegate|: The delegate which processes all non-keep-alive | |
| 28 // messages. This object assumes ownership of | |
| 29 // |inner_delegate|. | |
| 30 // |ping_interval|: The amount of idle time to wait before sending a PING to | |
| 31 // the remote end. | |
| 32 // |liveness_timeout|: The amount of idle time to wait before terminating the | |
| 33 // connection. | |
| 34 KeepAliveDelegate(CastSocket* socket, | |
| 35 scoped_refptr<Logger> logger, | |
| 36 std::unique_ptr<CastTransport::Delegate> inner_delegate, | |
| 37 base::TimeDelta ping_interval, | |
| 38 base::TimeDelta liveness_timeout); | |
| 39 | |
| 40 ~KeepAliveDelegate() override; | |
| 41 | |
| 42 // Creates a keep-alive message (e.g. PING or PONG). | |
| 43 static CastMessage CreateKeepAliveMessage(const char* message_type); | |
| 44 | |
| 45 void SetTimersForTest(std::unique_ptr<base::Timer> injected_ping_timer, | |
| 46 std::unique_ptr<base::Timer> injected_liveness_timer); | |
| 47 | |
| 48 // CastTransport::Delegate implementation. | |
| 49 void Start() override; | |
| 50 void OnError(ChannelError error_state) override; | |
| 51 void OnMessage(const CastMessage& message) override; | |
| 52 | |
| 53 static const char kHeartbeatPingType[]; | |
| 54 static const char kHeartbeatPongType[]; | |
| 55 | |
| 56 private: | |
| 57 // Restarts the ping/liveness timeout timers. Called when a message | |
| 58 // is received from the remote end. | |
| 59 void ResetTimers(); | |
| 60 | |
| 61 // Sends a formatted PING or PONG message to the remote side. | |
| 62 void SendKeepAliveMessage(const CastMessage& message, | |
| 63 const char* message_type); | |
| 64 | |
| 65 // Callback for SendKeepAliveMessage. | |
| 66 void SendKeepAliveMessageComplete(const char* message_type, int rv); | |
| 67 | |
| 68 // Called when the liveness timer expires, indicating that the remote | |
| 69 // end has not responded within the |liveness_timeout_| interval. | |
| 70 void LivenessTimeout(); | |
| 71 | |
| 72 // Stops the ping and liveness timers if they are started. | |
| 73 // To be called after an error. | |
| 74 void Stop(); | |
| 75 | |
| 76 // Indicates that Start() was called. | |
| 77 bool started_; | |
| 78 | |
| 79 // Socket that is managed by the keep-alive object. | |
| 80 CastSocket* socket_; | |
| 81 | |
| 82 // Logging object. | |
| 83 scoped_refptr<Logger> logger_; | |
| 84 | |
| 85 // Delegate object which receives all non-keep alive messages. | |
| 86 std::unique_ptr<CastTransport::Delegate> inner_delegate_; | |
| 87 | |
| 88 // Amount of idle time to wait before disconnecting. | |
| 89 base::TimeDelta liveness_timeout_; | |
| 90 | |
| 91 // Amount of idle time to wait before pinging the receiver. | |
| 92 base::TimeDelta ping_interval_; | |
| 93 | |
| 94 // Fired when |ping_interval_| is exceeded or when triggered by test code. | |
| 95 std::unique_ptr<base::Timer> ping_timer_; | |
| 96 | |
| 97 // Fired when |liveness_timer_| is exceeded. | |
| 98 std::unique_ptr<base::Timer> liveness_timer_; | |
| 99 | |
| 100 // The PING message to send over the wire. | |
| 101 CastMessage ping_message_; | |
| 102 | |
| 103 // The PONG message to send over the wire. | |
| 104 CastMessage pong_message_; | |
| 105 | |
| 106 THREAD_CHECKER(thread_checker_); | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(KeepAliveDelegate); | |
| 109 }; | |
| 110 | |
| 111 } // namespace cast_channel | |
| 112 | |
| 113 #endif // COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_ | |
| OLD | NEW |