2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2022-07-14 17:38:48 +02:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2018-07-03 19:28:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class Timeout final : public Actor {
|
|
|
|
public:
|
|
|
|
using Data = void *;
|
|
|
|
using Callback = void (*)(Data);
|
|
|
|
Timeout() {
|
|
|
|
register_actor("Timeout", this).release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_callback(Callback callback) {
|
|
|
|
callback_ = callback;
|
|
|
|
}
|
|
|
|
void set_callback_data(Data &&data) {
|
|
|
|
data_ = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_timeout() const {
|
|
|
|
return Actor::has_timeout();
|
|
|
|
}
|
2021-08-15 10:15:14 +02:00
|
|
|
double get_timeout() const {
|
|
|
|
return Actor::get_timeout();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
void set_timeout_in(double timeout) {
|
|
|
|
Actor::set_timeout_in(timeout);
|
|
|
|
}
|
2019-12-17 17:17:57 +01:00
|
|
|
void set_timeout_at(double timeout) {
|
|
|
|
Actor::set_timeout_at(timeout);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
void cancel_timeout() {
|
|
|
|
if (has_timeout()) {
|
|
|
|
Actor::cancel_timeout();
|
|
|
|
callback_ = Callback();
|
|
|
|
data_ = Data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class Scheduler;
|
|
|
|
|
2018-10-26 16:11:20 +02:00
|
|
|
Callback callback_{};
|
|
|
|
Data data_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!has_timeout());
|
|
|
|
CHECK(callback_ != Callback());
|
|
|
|
Callback callback = callback_;
|
|
|
|
Data data = data_;
|
|
|
|
callback_ = Callback();
|
|
|
|
data_ = Data();
|
|
|
|
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|