xref: /aosp_15_r20/external/webrtc/net/dcsctp/socket/heartbeat_handler.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker #ifndef NET_DCSCTP_SOCKET_HEARTBEAT_HANDLER_H_
11*d9f75844SAndroid Build Coastguard Worker #define NET_DCSCTP_SOCKET_HEARTBEAT_HANDLER_H_
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include <memory>
16*d9f75844SAndroid Build Coastguard Worker #include <string>
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
19*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/packet/chunk/heartbeat_ack_chunk.h"
20*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/packet/chunk/heartbeat_request_chunk.h"
21*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/packet/sctp_packet.h"
22*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/dcsctp_options.h"
23*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/socket/context.h"
24*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/timer/timer.h"
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker namespace dcsctp {
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker // HeartbeatHandler handles all logic around sending heartbeats and receiving
29*d9f75844SAndroid Build Coastguard Worker // the responses, as well as receiving incoming heartbeat requests.
30*d9f75844SAndroid Build Coastguard Worker //
31*d9f75844SAndroid Build Coastguard Worker // Heartbeats are sent on idle connections to ensure that the connection is
32*d9f75844SAndroid Build Coastguard Worker // still healthy and to measure the RTT. If a number of heartbeats time out,
33*d9f75844SAndroid Build Coastguard Worker // the connection will eventually be closed.
34*d9f75844SAndroid Build Coastguard Worker class HeartbeatHandler {
35*d9f75844SAndroid Build Coastguard Worker  public:
36*d9f75844SAndroid Build Coastguard Worker   HeartbeatHandler(absl::string_view log_prefix,
37*d9f75844SAndroid Build Coastguard Worker                    const DcSctpOptions& options,
38*d9f75844SAndroid Build Coastguard Worker                    Context* context,
39*d9f75844SAndroid Build Coastguard Worker                    TimerManager* timer_manager);
40*d9f75844SAndroid Build Coastguard Worker 
41*d9f75844SAndroid Build Coastguard Worker   // Called when the heartbeat interval timer should be restarted. This is
42*d9f75844SAndroid Build Coastguard Worker   // generally done every time data is sent, which makes the timer expire when
43*d9f75844SAndroid Build Coastguard Worker   // the connection is idle.
44*d9f75844SAndroid Build Coastguard Worker   void RestartTimer();
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker   // Called on received HeartbeatRequestChunk chunks.
47*d9f75844SAndroid Build Coastguard Worker   void HandleHeartbeatRequest(HeartbeatRequestChunk chunk);
48*d9f75844SAndroid Build Coastguard Worker 
49*d9f75844SAndroid Build Coastguard Worker   // Called on received HeartbeatRequestChunk chunks.
50*d9f75844SAndroid Build Coastguard Worker   void HandleHeartbeatAck(HeartbeatAckChunk chunk);
51*d9f75844SAndroid Build Coastguard Worker 
52*d9f75844SAndroid Build Coastguard Worker  private:
53*d9f75844SAndroid Build Coastguard Worker   absl::optional<DurationMs> OnIntervalTimerExpiry();
54*d9f75844SAndroid Build Coastguard Worker   absl::optional<DurationMs> OnTimeoutTimerExpiry();
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker   const std::string log_prefix_;
57*d9f75844SAndroid Build Coastguard Worker   Context* ctx_;
58*d9f75844SAndroid Build Coastguard Worker   TimerManager* timer_manager_;
59*d9f75844SAndroid Build Coastguard Worker   // The time for a connection to be idle before a heartbeat is sent.
60*d9f75844SAndroid Build Coastguard Worker   const DurationMs interval_duration_;
61*d9f75844SAndroid Build Coastguard Worker   // Adding RTT to the duration will add some jitter, which is good in
62*d9f75844SAndroid Build Coastguard Worker   // production, but less good in unit tests, which is why it can be disabled.
63*d9f75844SAndroid Build Coastguard Worker   const bool interval_duration_should_include_rtt_;
64*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<Timer> interval_timer_;
65*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<Timer> timeout_timer_;
66*d9f75844SAndroid Build Coastguard Worker };
67*d9f75844SAndroid Build Coastguard Worker }  // namespace dcsctp
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker #endif  // NET_DCSCTP_SOCKET_HEARTBEAT_HANDLER_H_
70