VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
PendingConVarQueries.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <utility>
5
6namespace VoltMod
7{
8
10{
11 if (!IsValidSlot(slot))
12 {
13 return;
14 }
15
16 auto& queries = _slots[slot];
17 std::erase_if(queries, [&](const PendingConVarQuery& query) { return now - query.SentAtSec >= TimeoutSec; });
18}
19
21{
22 if (!IsValidSlot(slot))
23 {
24 return nullptr;
25 }
26
27 auto& queries = _slots[slot];
28 const auto found = std::ranges::find(queries, name, &PendingConVarQuery::Name);
29 return found != queries.end() ? &*found : nullptr;
30}
31
33{
34 return Count(slot) >= MaxPendingPerSlot;
35}
36
38{
39 if (!IsValidSlot(slot))
40 {
41 return -1;
42 }
43
44 const auto& queries = _slots[slot];
45 // One more attempt than the cap guarantees a free cookie exists whenever the slot has room.
46 for (size_t attempt = 0; attempt <= MaxPendingPerSlot; ++attempt)
47 {
48 if (++_cookieCounter > MaxCookie)
49 {
50 _cookieCounter = 1;
51 }
52
53 const int cookie = static_cast<int>(_cookieCounter);
54 if (std::none_of(queries.begin(), queries.end(),
55 [&](const PendingConVarQuery& query) { return query.Cookie == cookie; }))
56 {
57 return cookie;
58 }
59 }
60 return -1;
61}
62
64 double now)
65{
66 if (!IsValidSlot(slot) || cookie < 0)
67 {
68 return;
69 }
70
71 _slots[slot].push_back(
72 {.Name = std::move(name), .Callback = std::move(callback), .SentAtSec = now, .Cookie = cookie});
73}
74
75std::optional<PendingConVarQuery> PendingConVarQueries::Take(int slot, int cookie, std::string_view name)
76{
77 if (!IsValidSlot(slot))
78 {
79 return std::nullopt;
80 }
81
82 auto& queries = _slots[slot];
83 auto it = std::find_if(queries.begin(), queries.end(),
84 [&](const PendingConVarQuery& query) { return query.Cookie == cookie; });
85 if (it == queries.end() || it->Name != name)
86 {
87 return std::nullopt;
88 }
89
90 PendingConVarQuery query = std::move(*it);
91 queries.erase(it);
92 return query;
93}
94
96{
97 if (IsValidSlot(slot))
98 {
99 _slots[slot].clear();
100 }
101}
102
104{
105 for (auto& queries : _slots)
106 {
107 queries.clear();
108 }
109}
110
112{
113 return IsValidSlot(slot) ? _slots[slot].size() : 0;
114}
115
116} // namespace VoltMod
std::function< void(int slot, ClientConVarStatus status, std::string_view name, std::string_view value)> QueryCallback
void Add(int slot, int cookie, std::string name, ClientConVars::QueryCallback callback, double now)
static constexpr size_t MaxPendingPerSlot
std::optional< PendingConVarQuery > Take(int slot, int cookie, std::string_view name)
void Prune(int slot, double now)
static constexpr uint32_t MaxCookie
PendingConVarQuery * Find(int slot, std::string_view name)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
One outstanding client convar query, identified by the cookie sent to the client.
std::string Name
Convar name the query asked for.