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