18 const auto found = std::ranges::find(_required,
id, &Requirement::Id);
19 if (found != _required.end())
25 _required.push_back({.Id = id, .Holders = 1});
32 const auto found = std::ranges::find(_required,
id, &Requirement::Id);
33 if (found != _required.end() && --found->Holders <= 0)
35 _required.erase(found);
41 std::vector<uint64_t> ids;
42 ids.reserve(_required.size());
43 for (
const Requirement& requirement : _required)
45 ids.push_back(requirement.Id);
50const DownloadQueue::Client* DownloadQueue::FindClient(int64_t steamId)
const
52 const auto found = _clients.find(steamId);
53 return found != _clients.end() ? &found->second :
nullptr;
58 const Client* client = FindClient(steamId);
59 std::vector<uint64_t> missing =
Required();
62 std::erase_if(missing, [client](uint64_t
id) {
return std::ranges::contains(client->Downloaded,
id); });
69 const Client* client = FindClient(steamId);
75 std::vector<uint64_t> ids =
Required();
77 ids, [client](uint64_t
id) {
return id != client->Sending && !std::ranges::contains(client->Downloaded,
id); });
83 const Client* client = FindClient(steamId);
86 return !_required.empty();
89 const auto missing = [client](
const Requirement& requirement) {
90 return !std::ranges::contains(client->Downloaded, requirement.Id);
92 return std::ranges::any_of(_required, missing);
97 const std::vector<uint64_t> missing =
FindMissing(steamId);
103 Client& client = _clients[steamId];
104 const uint64_t next = missing.front();
107 client.Attempts = (client.Sending == next) ? client.Attempts + 1 : 1;
108 if (client.Attempts > maxAttempts)
113 client.Sending = next;
115 return {.Action =
AddonAction::Send, .Id = next, .Remaining = missing.size() - 1};
132 if (downloaded.empty())
140 if (listed.size() == 1)
154 Client& client = _clients[steamId];
162 const auto found = _clients.find(steamId);
163 if (found == _clients.end())
168 Client& client = found->second;
170 if (client.Sending == 0)
174 if (now - client.SentAt <= timeoutSec)
176 if (!std::ranges::contains(client.Downloaded, client.Sending))
178 client.Downloaded.push_back(client.Sending);
187 if (
const auto found = _clients.find(steamId); found != _clients.end())
189 found->second.LeftAt = now;
191 std::erase_if(_clients, [now, forgetAfterSec](
const auto& entry) {
192 const double leftAt = entry.second.LeftAt;
193 return leftAt > 0.0 && now - leftAt > forgetAfterSec;
205 std::vector<std::string_view> entries;
207 while (!field.empty())
209 const size_t comma = field.find(
',');
210 entries.push_back(field.substr(0, comma));
212 if (comma == std::string_view::npos)
216 field.remove_prefix(comma + 1);
219 entries.emplace_back();
228 std::vector<uint64_t> ids;
242 std::vector<uint64_t> appended;
244 for (uint64_t
id : ids)
246 if (
id == 0 || std::ranges::contains(named,
id) || std::ranges::contains(appended,
id))
255 field += std::to_string(
id);
256 appended.push_back(
id);
265 for (uint64_t
id : ids)
268 std::ranges::find_if(entries, [
id](std::string_view entry) {
return ParseUInt64(entry) == id; });
269 if (at != entries.end())
276 kept.reserve(field.size());
277 for (
size_t i = 0; i < entries.size(); ++i)
285 field = std::move(kept);
AddonDecision DecideJoinMessage(int64_t steamId, bool reconnect, std::string_view addons, double now, int maxAttempts)
bool HasMissing(int64_t steamId) const
void ClientReconnected(int64_t steamId, double now, double timeoutSec)
std::vector< uint64_t > ClientMountList(int64_t steamId) const
void MarkSending(int64_t steamId, uint64_t id, double now)
std::vector< uint64_t > FindMissing(int64_t steamId) const
std::vector< uint64_t > Required() const
void ClientLeft(int64_t steamId, double now, double forgetAfterSec)
AddonDecision NextToSend(int64_t steamId, double now, int maxAttempts)
std::vector< uint64_t > AppendToAddonList(std::string &field, const std::vector< uint64_t > &ids)
std::optional< uint64_t > ParseUInt64(std::string_view text)
void RemoveFromAddonList(std::string &field, const std::vector< uint64_t > &ids)
std::vector< uint64_t > ParseAddonList(std::string_view field)
@ TrimToFirst
the message names several addons; keep only AddonDecision::Id
@ Kick
the client refused AddonDecision::Id too often
@ Send
point the client at AddonDecision::Id and wait for its reconnect
@ KeepMounted
a map change names no addon; name AddonDecision::Id so the client keeps it
static std::vector< std::string_view > SplitAddonList(std::string_view field)