Twitch SDK (Internal)
timer.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
12 
13 #include <vector>
14 
15 namespace ttv
16 {
17  class WaitForEventWithTimeout;
18  class WaitForExpiry;
19  class RetryBackoffTable;
20  class RetryTimer;
21 
22  uint64_t JitterTime(uint64_t baseMs, uint64_t width);
23 }
24 
25 
27 {
28 public:
30  {
34  };
35 
36 public:
38 
39  void Reset(uint64_t timeout);
40  void Complete();
42 
43  bool IsTimedOut() { return GetState() == WFEWT_TimedOut; }
44  bool IsComplete() { return GetState() == WFEWT_Complete; }
45 
46 private:
47  uint64_t mExpiryTime;
49 };
50 
51 
53 {
54 public:
55  WaitForExpiry();
56 
57  void Set(uint64_t milliseconds);
58  void SetWithJitter(uint64_t milliseconds, uint64_t jitterWidthMs);
59  void AdjustDuration(uint64_t milliseconds);
60  bool Check(bool clearWhenExpired);
61  void Clear();
62  bool IsSet() const { return mStartTime != 0; }
63 
64  uint64_t GetStartTime() const { return mStartTime; }
65  uint64_t GetEndTime() const { return mEndTime; }
66  uint64_t GetRemainingTime() const;
67  uint64_t GetElapsedTime() const;
68 
69 private:
70  uint64_t mStartTime;
71  uint64_t mEndTime;
72 };
73 
74 
85 {
86 public:
94  RetryBackoffTable(const std::vector<uint64_t>& tableMilliseconds, uint64_t retryJitterWidthMs);
98  RetryBackoffTable(uint64_t maxInterval, uint64_t retryJitterWidthMs);
99 
103  void Advance();
107  void Reset();
111  uint64_t GetInterval() const;
112 
113 private:
114  void CreateTable(uint64_t maxInterval);
115 
116  std::vector<uint64_t> mBackOffTableMilliseconds;
119 };
120 
121 
135 {
136 public:
140  RetryTimer();
144  RetryTimer(const std::vector<uint64_t>& backOffTableMs, uint64_t retryJitterWidthMs);
148  RetryTimer(uint64_t maxInterval, uint64_t retryJitterWidthMs);
149 
150  void SetBackoffTable(const std::vector<uint64_t>& backOffTableMs, uint64_t retryJitterWidthMs);
151  void SetBackoffTable(uint64_t maxInterval, uint64_t retryJitterWidthMs);
152 
156  void ScheduleNextRetry();
160  bool CheckNextRetry();
164  void Clear();
165 
166  void StartGlobalReset(uint64_t milliseconds);
167  bool CheckGlobalReset();
168  void ClearGlobalReset();
169 
170  bool IsRetrySet() const { return mNextRetry.IsSet(); }
171  bool IsGlobalSet() const { return mGlobalResetTimer.IsSet(); }
172 
173 protected:
174  uint64_t GetNextAttempt();
175 
178  std::vector<uint64_t> mBackOffTable;
179  uint64_t mRetryJitter;
181 };
Definition: timer.h:134
uint64_t mJitterMilliseconds
Definition: timer.h:117
void Reset(uint64_t timeout)
uint32_t mNextAttemptNumber
Definition: timer.h:118
WaitForExpiry mGlobalResetTimer
The timer which will reset mNextAttemptNumber.
Definition: timer.h:177
eWaitState mState
Definition: timer.h:48
eWaitState
Definition: timer.h:29
uint64_t mStartTime
Definition: timer.h:70
bool IsGlobalSet() const
Definition: timer.h:171
bool IsTimedOut()
Definition: timer.h:43
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
uint64_t mRetryJitter
Definition: timer.h:179
uint64_t JitterTime(uint64_t baseMs, uint64_t width)
Definition: timer.h:84
Definition: timer.h:26
Definition: timer.h:52
bool IsComplete()
Definition: timer.h:44
bool IsSet() const
Definition: timer.h:62
uint64_t mEndTime
Definition: timer.h:71
uint64_t GetEndTime() const
Definition: timer.h:65
std::vector< uint64_t > mBackOffTableMilliseconds
Definition: timer.h:116
bool IsRetrySet() const
Definition: timer.h:170
WaitForExpiry mNextRetry
Definition: timer.h:176
std::vector< uint64_t > mBackOffTable
Definition: timer.h:178
uint64_t GetStartTime() const
Definition: timer.h:64
uint64_t mExpiryTime
Definition: timer.h:47
uint32_t mNextAttemptNumber
Definition: timer.h:180