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.
5 #ifndef COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_
6 #define COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "base/timer/timer.h"
12 #include "components/cast_channel/cast_message_util.h"
13 #include "components/cast_channel/cast_transport.h"
14 #include "components/cast_channel/proto/cast_channel.pb.h"
16 namespace cast_channel {
21 // Decorator delegate which provides keep-alive functionality.
22 // Keep-alive messages are handled by this object; all other messages and
23 // errors are passed to |inner_delegate_|.
24 class KeepAliveDelegate : public CastTransport::Delegate {
26 // |socket|: The socket to be kept alive.
27 // |logger|: The logging object which collects protocol events and error
29 // |inner_delegate|: The delegate which processes all non-keep-alive
30 // messages. This object assumes ownership of
32 // |ping_interval|: The amount of idle time to wait before sending a PING to
34 // |liveness_timeout|: The amount of idle time to wait before terminating the
36 KeepAliveDelegate(CastSocket* socket,
37 scoped_refptr<Logger> logger,
38 std::unique_ptr<CastTransport::Delegate> inner_delegate,
39 base::TimeDelta ping_interval,
40 base::TimeDelta liveness_timeout);
42 ~KeepAliveDelegate() override;
44 void SetTimersForTest(
45 std::unique_ptr<base::RetainingOneShotTimer> injected_ping_timer,
46 std::unique_ptr<base::RetainingOneShotTimer> injected_liveness_timer);
48 // CastTransport::Delegate implementation.
49 void Start() override;
50 void OnError(ChannelError error_state) override;
51 void OnMessage(const CastMessage& message) override;
54 // Restarts the ping/liveness timeout timers. Called when a message
55 // is received from the remote end.
58 // Sends a formatted PING or PONG message to the remote side.
59 void SendKeepAliveMessage(const CastMessage& message,
60 CastMessageType message_type);
62 // Callback for SendKeepAliveMessage.
63 void SendKeepAliveMessageComplete(CastMessageType message_type, int rv);
65 // Called when the liveness timer expires, indicating that the remote
66 // end has not responded within the |liveness_timeout_| interval.
67 void LivenessTimeout();
69 // Stops the ping and liveness timers if they are started.
70 // To be called after an error.
73 // Indicates that Start() was called.
76 // Socket that is managed by the keep-alive object.
80 scoped_refptr<Logger> logger_;
82 // Delegate object which receives all non-keep alive messages.
83 std::unique_ptr<CastTransport::Delegate> inner_delegate_;
85 // Amount of idle time to wait before disconnecting.
86 base::TimeDelta liveness_timeout_;
88 // Amount of idle time to wait before pinging the receiver.
89 base::TimeDelta ping_interval_;
91 // Fired when |ping_interval_| is exceeded or when triggered by test code.
92 std::unique_ptr<base::RetainingOneShotTimer> ping_timer_;
94 // Fired when |liveness_timer_| is exceeded.
95 std::unique_ptr<base::RetainingOneShotTimer> liveness_timer_;
97 // The PING message to send over the wire.
98 const CastMessage ping_message_;
100 // The PONG message to send over the wire.
101 const CastMessage pong_message_;
103 THREAD_CHECKER(thread_checker_);
105 base::WeakPtrFactory<KeepAliveDelegate> weak_factory_;
107 DISALLOW_COPY_AND_ASSIGN(KeepAliveDelegate);
110 } // namespace cast_channel
112 #endif // COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_