blob: e8d12df5925d0edf4bea06c98cc29128aaeff408 [file] [log] [blame]
Victor Boiviede88b082021-05-03 13:52:531/*
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_TIMER_TASK_QUEUE_TIMEOUT_H_
11#define NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_
12
13#include <memory>
14#include <utility>
15
16#include "api/task_queue/task_queue_base.h"
17#include "net/dcsctp/public/timeout.h"
18#include "rtc_base/task_utils/pending_task_safety_flag.h"
19
20namespace dcsctp {
21
22// The TaskQueueTimeoutFactory creates `Timeout` instances, which schedules
23// itself to be triggered on the provided `task_queue`, which may be a thread,
24// an actual TaskQueue or something else which supports posting a delayed task.
25//
26// Note that each `DcSctpSocket` must have its own `TaskQueueTimeoutFactory`,
27// as the `TimeoutID` are not unique among sockets.
28//
29// This class must outlive any created Timeout that it has created. Note that
30// the `DcSctpSocket` will ensure that all Timeouts are deleted when the socket
31// is destructed, so this means that this class must outlive the `DcSctpSocket`.
32//
33// This class, and the timeouts created it, are not thread safe.
34class TaskQueueTimeoutFactory {
35 public:
36 // The `get_time` function must return the current time, relative to any
37 // epoch. Whenever a timeout expires, the `on_expired` callback will be
38 // triggered, and then the client should provided `timeout_id` to
39 // `DcSctpSocketInterface::HandleTimeout`.
40 TaskQueueTimeoutFactory(webrtc::TaskQueueBase& task_queue,
41 std::function<TimeMs()> get_time,
42 std::function<void(TimeoutID timeout_id)> on_expired)
43 : task_queue_(task_queue),
44 get_time_(std::move(get_time)),
45 on_expired_(std::move(on_expired)) {}
46
47 // Creates an implementation of `Timeout`.
48 std::unique_ptr<Timeout> CreateTimeout() {
49 return std::make_unique<TaskQueueTimeout>(*this);
50 }
51
52 private:
53 class TaskQueueTimeout : public Timeout {
54 public:
55 explicit TaskQueueTimeout(TaskQueueTimeoutFactory& parent);
56 ~TaskQueueTimeout();
57
58 void Start(DurationMs duration_ms, TimeoutID timeout_id) override;
59 void Stop() override;
60
61 private:
62 TaskQueueTimeoutFactory& parent_;
63 // A safety flag to ensure that posted tasks to the task queue don't
64 // reference these object when they go out of scope. Note that this safety
65 // flag will be re-created if the scheduled-but-not-yet-expired task is not
66 // to be run. This happens when there is a posted delayed task with an
67 // expiration time _further away_ than what is now the expected expiration
68 // time. In this scenario, a new delayed task has to be posted with a
69 // shorter duration and the old task has to be forgotten.
70 rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> pending_task_safety_flag_;
71 // The time when the posted delayed task is set to expire. Will be set to
72 // the infinite future if there is no such task running.
73 TimeMs posted_task_expiration_ = TimeMs::InfiniteFuture();
74 // The time when the timeout expires. It will be set to the infinite future
75 // if the timeout is not running/not started.
76 TimeMs timeout_expiration_ = TimeMs::InfiniteFuture();
77 // The current timeout ID that will be reported when expired.
78 TimeoutID timeout_id_ = TimeoutID(0);
79 };
80
81 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker thread_checker_;
82 webrtc::TaskQueueBase& task_queue_;
83 const std::function<TimeMs()> get_time_;
84 const std::function<void(TimeoutID)> on_expired_;
85};
86} // namespace dcsctp
87
88#endif // NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_