forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.hpp
111 lines (96 loc) · 2.73 KB
/
timer.hpp
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
111
// solid/frame/timer.hpp
//
// Copyright (c) 2015 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// 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.
//
#ifndef SOLID_FRAME_TIMER_HPP
#define SOLID_FRAME_TIMER_HPP
#include "solid/system/common.hpp"
#include "solid/system/socketdevice.hpp"
#include "completion.hpp"
#include "reactorcontext.hpp"
namespace solid{
namespace frame{
struct ObjectProxy;
struct ReactorContext;
class Timer: public CompletionHandler{
typedef Timer ThisT;
static void on_init_completion(CompletionHandler& _rch, ReactorContext &_rctx){
ThisT &rthis = static_cast<ThisT&>(_rch);
rthis.completionCallback(Timer::on_completion);
}
static void on_completion(CompletionHandler& _rch, ReactorContext &_rctx){
ThisT &rthis = static_cast<ThisT&>(_rch);
switch(rthis.reactorEvent(_rctx)){
case ReactorEventTimer:
rthis.doExec(_rctx);
break;
case ReactorEventClear:
rthis.doClear(_rctx);
rthis.f = &on_dummy;
break;
default:
SOLID_ASSERT(false);
}
}
static void on_dummy(ReactorContext &_rctx){
}
public:
Timer(
ObjectProxy const &_robj
):CompletionHandler(_robj, Timer::on_init_completion), storeidx(InvalidIndex())
{
}
~Timer(){
//MUST call here and not in the ~CompletionHandler
this->deactivate();
}
//Returns false when the operation is scheduled for completion. On completion _f(...) will be called.
//Returns true when operation could not be scheduled for completion - e.g. operation already in progress.
template <typename F>
bool waitFor(ReactorContext &_rctx, NanoTime const& _tm, F _f){
NanoTime t = _rctx.time();
t += _tm;
return waitUntil(_rctx, t, _f);
}
//Returns true when the operation completed. Check _rctx.error() for success or fail
//Returns false when operation is scheduled for completion. On completion _f(...) will be called.
template <typename F>
bool waitUntil(ReactorContext &_rctx, NanoTime const& _tm, F _f){
if(FUNCTION_EMPTY(f)){
f = _f;
this->addTimer(_rctx, _tm, storeidx);
return false;
}else{
//TODO: set proper error
error(_rctx, ErrorConditionT(-1, _rctx.error().category()));
return true;
}
}
void cancel(ReactorContext &_rctx){
doClear(_rctx);
}
private:
friend class Reactor;
void doExec(ReactorContext &_rctx){
FunctionT tmpf(std::move(f));
storeidx = InvalidIndex();
tmpf(_rctx);
}
void doClear(ReactorContext &_rctx){
FUNCTION_CLEAR(f);
remTimer(_rctx, storeidx);
storeidx = InvalidIndex();
}
private:
typedef FUNCTION<void(ReactorContext&)> FunctionT;
FunctionT f;
size_t storeidx;
};
}//namespace frame
}//namespace solid
#endif