aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/asio/deadline_timer_impl.h
blob: d9db625c948acc021ce0cbe96d4db4c2d2c90175 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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));
        }
    };
}