59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
#pragma once
|
|||
|
|
#include <cstdint>
|
||
|
|
|
||
|
|
namespace microbu_v2x::pki {
|
||
|
|
|
||
|
|
// ETSI TS 103 601 V2.1.1 clauses 5.1.2 and 5.1.4.
|
||
|
|
// Times use a monotonic millisecond clock. N1 includes the initial attempt.
|
||
|
|
struct RetryPolicy {
|
||
|
|
std::uint64_t interval_ms = 60000;
|
||
|
|
std::uint64_t lifetime_ms = 190000;
|
||
|
|
std::uint32_t attempts = 3;
|
||
|
|
std::uint64_t minimum_response_ms = 55000;
|
||
|
|
std::uint64_t authority_context_ms = 3600000;
|
||
|
|
|
||
|
|
bool valid() const {
|
||
|
|
return attempts && interval_ms >= minimum_response_ms && interval_ms && lifetime_ms &&
|
||
|
|
(lifetime_ms - 1) / attempts >= interval_ms && authority_context_ms >= lifetime_ms;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
class RetrySchedule {
|
||
|
|
public:
|
||
|
|
bool begin(std::uint64_t now, RetryPolicy policy = {}) {
|
||
|
|
stop();
|
||
|
|
if (!policy.valid()) return false;
|
||
|
|
policy_ = policy;
|
||
|
|
start_ = last_ = now;
|
||
|
|
attempts_ = 1;
|
||
|
|
active_ = true;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool poll(std::uint64_t now) {
|
||
|
|
if (active_ && (now < last_ || now - start_ >= policy_.lifetime_ms ||
|
||
|
|
(attempts_ == policy_.attempts && now - last_ >= policy_.interval_ms))) stop();
|
||
|
|
return active_;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool repeat(std::uint64_t now) {
|
||
|
|
if (!poll(now) || now - last_ < policy_.interval_ms || attempts_ >= policy_.attempts) return false;
|
||
|
|
last_ = now;
|
||
|
|
++attempts_;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void stop() { active_ = false; }
|
||
|
|
bool active() const { return active_; }
|
||
|
|
std::uint32_t attempts() const { return attempts_; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
RetryPolicy policy_;
|
||
|
|
std::uint64_t start_ = 0;
|
||
|
|
std::uint64_t last_ = 0;
|
||
|
|
std::uint32_t attempts_ = 0;
|
||
|
|
bool active_ = false;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace microbu_v2x::pki
|