forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackofftimer.h
More file actions
163 lines (127 loc) · 4.55 KB
/
backofftimer.h
File metadata and controls
163 lines (127 loc) · 4.55 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @file mega/backofftimer.h
* @brief Generic timer facility with exponential backoff
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#ifndef MEGA_BACKOFF_TIMER_H
#define MEGA_BACKOFF_TIMER_H 1
#include "types.h"
namespace mega {
// generic timer facility with exponential backoff
class MEGA_API BackoffTimer
{
dstime next;
dstime delta;
dstime base;
PrnGen &rng;
public:
// reset timer
void reset();
// trigger exponential backoff
void backoff();
// set absolute backoff
void backoff(dstime);
// set absolute trigger time
void set(dstime);
// check if timer has elapsed
bool armed() const;
// arm timer
bool arm();
// time left for event to become armed
dstime retryin() const;
// current backoff delta
dstime backoffdelta();
// time of next trigger or 0 if no trigger since last backoff
dstime nextset() const;
// update time to wait
void update(dstime*);
BackoffTimer(PrnGen &rng);
};
class MEGA_API BackoffTimerTracked;
// This class keeps track of a group of BackoffTimerTracked, which register and deregister themselves.
// Timers are in the multimap when they have non-0 non-NEVER timeouts set, giving us a much smaller group should we need to iterate it.
class MEGA_API BackoffTimerGroupTracker
{
std::multimap<dstime, BackoffTimerTracked*> timeouts;
public:
typedef std::multimap<dstime, BackoffTimerTracked*>::iterator Iter;
inline Iter add(BackoffTimerTracked* bt);
inline void remove(Iter i) { timeouts.erase(i); }
// Find out the soonest (non-0 and non-NEVER) timeout in the group.
// For transfers, it calls set(0) on any timed out timers, as the old code did.
void update(dstime* waituntil, bool transfers);
};
// Just like a backoff timer, but is part of a group where we want to know the soonest (non-0) timeout in the group immediately
// Also, the enable() function can be used to exclude timers when they are not relevant, while keeping the timer settings.
class MEGA_API BackoffTimerTracked
{
bool mIsEnabled;
BackoffTimer bt;
BackoffTimerGroupTracker& mTracker;
BackoffTimerGroupTracker::Iter mTrackerPos;
void untrack();
void track();
public:
BackoffTimerTracked(PrnGen &rng, BackoffTimerGroupTracker& tr);
~BackoffTimerTracked();
inline bool arm() { untrack(); bool result = bt.arm(); track(); return result; }
inline void backoff() { untrack(); bt.backoff(); track(); }
inline void backoff(dstime t) { untrack(); bt.backoff(t); track(); }
inline void set(dstime t) { untrack(); bt.set(t); track(); }
inline void update(dstime* t) { untrack(); bt.update(t); track(); }
inline void reset() { untrack(); bt.reset(); track(); }
inline bool armed() const { return bt.armed(); };
inline dstime nextset() const { return bt.nextset(); };
inline dstime retryin() const { return bt.retryin(); }
inline void enable(bool b) { untrack(); mIsEnabled = b; track(); }
inline bool enabled() { return mIsEnabled; }
};
inline auto BackoffTimerGroupTracker::add(BackoffTimerTracked* bt) -> Iter
{
return timeouts.emplace(bt->nextset() ? bt->nextset() : NEVER, bt);
}
inline void BackoffTimerTracked::untrack()
{
if (mIsEnabled && bt.nextset() != 0 && bt.nextset() != NEVER)
{
mTracker.remove(mTrackerPos);
mTrackerPos = BackoffTimerGroupTracker::Iter();
}
}
inline void BackoffTimerTracked::track()
{
if (mIsEnabled && bt.nextset() != 0 && bt.nextset() != NEVER)
{
mTrackerPos = mTracker.add(this);
}
}
inline BackoffTimerTracked::BackoffTimerTracked(PrnGen &rng, BackoffTimerGroupTracker& tr) : mIsEnabled(true), bt(rng), mTracker(tr)
{
track();
}
inline BackoffTimerTracked::~BackoffTimerTracked()
{
untrack();
}
class MEGA_API TimerWithBackoff: public BackoffTimer {
public:
int tag;
TimerWithBackoff(PrnGen &rng, int tag);
};
} // namespace
#endif