diff options
author | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
---|---|---|
committer | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
commit | 06e5c21a835c0e923506c4ff27929f34e00761c2 (patch) | |
tree | 75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/neh/asio/deadline_timer_impl.h | |
parent | 03f024c4412e3aa613bb543cf1660176320ba8f4 (diff) | |
download | ydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz |
fix ya.make
Diffstat (limited to 'library/cpp/neh/asio/deadline_timer_impl.h')
-rw-r--r-- | library/cpp/neh/asio/deadline_timer_impl.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/library/cpp/neh/asio/deadline_timer_impl.h b/library/cpp/neh/asio/deadline_timer_impl.h new file mode 100644 index 0000000000..d9db625c94 --- /dev/null +++ b/library/cpp/neh/asio/deadline_timer_impl.h @@ -0,0 +1,110 @@ +#pragma once + +#include "io_service_impl.h" + +namespace NAsio { + class TTimerOperation: public TOperation { + public: + TTimerOperation(TIOService::TImpl::TTimer* t, TInstant deadline) + : TOperation(deadline) + , T_(t) + { + } + + void AddOp(TIOService::TImpl&) override { + Y_ASSERT(0); + } + + void Finalize() override { + DBGOUT("TTimerDeadlineOperation::Finalize()"); + T_->DelOp(this); + } + + protected: + TIOService::TImpl::TTimer* T_; + }; + + class TRegisterTimerOperation: public TTimerOperation { + public: + TRegisterTimerOperation(TIOService::TImpl::TTimer* t, TInstant deadline = TInstant::Max()) + : TTimerOperation(t, deadline) + { + Speculative_ = true; + } + + bool Execute(int errorCode) override { + Y_UNUSED(errorCode); + T_->GetIOServiceImpl().SyncRegisterTimer(T_); + return true; + } + }; + + class TTimerDeadlineOperation: public TTimerOperation { + public: + TTimerDeadlineOperation(TIOService::TImpl::TTimer* t, TDeadlineTimer::THandler h, TInstant deadline) + : TTimerOperation(t, deadline) + , H_(h) + { + } + + void AddOp(TIOService::TImpl&) override { + T_->AddOp(this); + } + + bool Execute(int errorCode) override { + DBGOUT("TTimerDeadlineOperation::Execute(" << errorCode << ")"); + H_(errorCode == ETIMEDOUT ? 0 : errorCode, *this); + return true; + } + + private: + TDeadlineTimer::THandler H_; + }; + + class TCancelTimerOperation: public TTimerOperation { + public: + TCancelTimerOperation(TIOService::TImpl::TTimer* t) + : TTimerOperation(t, TInstant::Max()) + { + Speculative_ = true; + } + + bool Execute(int errorCode) override { + Y_UNUSED(errorCode); + T_->FailOperations(ECANCELED); + return true; + } + }; + + class TUnregisterTimerOperation: public TTimerOperation { + public: + TUnregisterTimerOperation(TIOService::TImpl::TTimer* t, TInstant deadline = TInstant::Max()) + : TTimerOperation(t, deadline) + { + Speculative_ = true; + } + + bool Execute(int errorCode) override { + Y_UNUSED(errorCode); + DBGOUT("TUnregisterTimerOperation::Execute(" << errorCode << ")"); + T_->GetIOServiceImpl().SyncUnregisterAndDestroyTimer(T_); + return true; + } + }; + + class TDeadlineTimer::TImpl: public TIOService::TImpl::TTimer { + public: + TImpl(TIOService::TImpl& srv) + : TIOService::TImpl::TTimer(srv) + { + } + + void AsyncWaitExpireAt(TDeadline d, TDeadlineTimer::THandler h) { + Srv_.ScheduleOp(new TTimerDeadlineOperation(this, h, d)); + } + + void Cancel() { + Srv_.ScheduleOp(new TCancelTimerOperation(this)); + } + }; +} |