我想创建一个可以手动设置并与WDS.vars.get(password')
我尝试了以下方法:
boost::asio::basic_waitable_timer
然后以以下方式使用它:
class my_clock {
public:
using duration = std::chrono::nanoseconds;
using time_point = std::chrono::time_point<std::chrono::system_clock>;
static time_point now() {
return n;
}
static void set_now(time_point now) {
n = now;
}
private:
static time_point n;
};
my_clock::time_point my_clock::n = my_clock::time_point();
关于如何以这种方式向boost::asio::io_context ctx;
auto now = std::chrono::system_clock::now();
my_clock::set_now(now);
auto next_expire = now + std::chrono::milliseconds(10);
boost::asio::basic_waitable_timer<my_clock> ctimer(ctx);
ctimer.expires_at(next_expire);
int count = 0;
ctimer.async_wait([&](const boost::asio::error_code& ec) {
count++;
});
ctx.run_one();
// count == 0
my_clock::set_now(next_expire + std::chrono::seconds(1));
ctx.run();
// count == 1, except it's NOT
注入自定义时钟的任何建议?
答案 0 :(得分:1)
只要您的时钟满足TrivialClock的要求,您就可以将其与basic_waitiable_timer一起使用。 几个例子:
答案 1 :(得分:0)
借助@sergiopm的回复,以下是可行的解决方案:
class my_clock {
public:
using duration = std::chrono::nanoseconds;
using time_point = std::chrono::time_point<my_clock>;
using rep = duration::rep;
using period = duration::period;
static constexpr bool is_steady = false;
static time_point now() noexcept {
return n;
}
static void set_now(time_point now) {
n = now;
}
private:
static time_point n;
};
然后,您需要覆盖waitable_timer的等待特性,以使它在轮询io上下文时不会等待挂钟:
struct wait_traits
{
static typename my_clock::duration to_wait_duration(
const typename my_clock::duration& d)
{
return std::chrono::nanoseconds(0);
}
};
然后进行如下练习:
asio::io_context ctx;
auto now = my_clock::time_point(my_clock::duration(10));
my_clock::set_now(now);
asio::basic_waitable_timer<my_clock, wait_traits> ctimer(ctx);
auto next_expire = now + std::chrono::milliseconds(10);
ctimer.expires_at(next_expire);
ctimer.async_wait([&](const asio::error_code& ec) {
future_period_count++;
std::cout << "THIS HAPPENED" << std::endl;
});
ctx.poll_one();
REQUIRE(future_period_count == 0);
my_clock::set_now(next_expire + std::chrono::seconds(1));
ctx.run_one();
REQUIRE(future_period_count == 1);