Rise
The Vieneo Province
Loading...
Searching...
No Matches
CityLogoAssignments.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <cstring>
4#include <limits>
5#include <map>
6#include <memory>
7#include <string>
8
9namespace CityLogos
10{
11 constexpr unsigned char AssignmentType = 31;
12 // Proposed Server.NET extension; type 15 remains the vehicle PNG request.
13 constexpr unsigned char RequestType = 32;
14 constexpr size_t MaxFileBytes = 32 * 1024 * 1024;
15 // Low-priority GIF transfers may wait behind terrain and other traffic.
16 constexpr uint64_t InitialRetryDelayMs = 30000;
17
18 inline bool Filename(const char* text, size_t capacity, std::string& result)
19 {
20 if (!text) return false;
21 size_t length = 0;
22 while (length < capacity && text[length]) ++length;
23 if (!length || length > 8 || length == capacity) return false;
24 for (size_t i = 0; i < length; ++i)
25 {
26 const unsigned char c = text[i];
27 if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
28 (c >= '0' && c <= '9') || c == '_' || c == '-')) return false;
29 }
30 result.assign(text, length);
31 for (auto& c : result) if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
32 return true;
33 }
34
35 // Filename cache entries survive geometry removal. Groups are signed SQL
36 // smallints, never vehicle slots. Tokens occupy a separate negative domain.
38 {
39 std::string filename;
40 int32_t token = 0;
41 uint64_t retryAt = 0;
42 unsigned failures = 0;
43 bool ready = false;
44 };
45
47 {
48 public:
49 bool Assign(int32_t group, const char* filename, size_t capacity)
50 {
51 if (group < -32768 || group > 32767) return false;
52 std::string name;
53 if (!Filename(filename, capacity, name)) return false;
54 auto& asset = assets[name];
55 if (!asset) { asset = std::make_shared<AssignmentAsset>(); asset->filename = name; }
56 groups[group] = asset;
57 // Remove obsolete assets/tokens when their last group changes. A
58 // late response cannot install artwork into a replacement group.
59 for (auto it = assets.begin(); it != assets.end();)
60 if (it->second.use_count() == 1) it = assets.erase(it); else ++it;
61 return true;
62 }
63
64 const AssignmentAsset* Find(int32_t group) const
65 {
66 auto it = groups.find(group);
67 return it == groups.end() ? nullptr : it->second.get();
68 }
69
70 template<class Send> void RequestDue(uint64_t now, Send send)
71 {
72 for (auto& item : assets)
73 {
74 auto& asset = *item.second;
75 if (asset.ready || now < asset.retryAt) continue;
76 if (asset.token) ++asset.failures; // lost response / timeout
77 if (nextToken == (std::numeric_limits<int32_t>::min)()) continue;
78 asset.token = --nextToken; // never recycle, including retries
79 const unsigned shift = asset.failures < 4 ? asset.failures : 4;
80 asset.retryAt = now + (InitialRetryDelayMs << shift);
81 send(asset);
82 }
83 }
84
85 AssignmentAsset* Pending(int32_t token)
86 {
87 if (token >= 0) return nullptr;
88 for (auto& item : assets)
89 if (item.second->token == token && !item.second->ready) return item.second.get();
90 return nullptr;
91 }
92
93 void Complete(AssignmentAsset& asset, bool success)
94 {
95 asset.ready = success;
96 asset.token = 0;
97 if (!success) ++asset.failures; // retryAt retains the cooldown
98 }
99
100 bool Contains(const std::string& name) const { return assets.find(name) != assets.end(); }
101 void Invalidate(const std::string& name, uint64_t now)
102 {
103 auto it = assets.find(name);
104 if (it == assets.end()) return;
105 it->second->ready = false;
106 it->second->token = 0;
107 it->second->retryAt = now + InitialRetryDelayMs;
108 ++it->second->failures;
109 }
110 void Clear() { groups.clear(); assets.clear(); } // don't reuse old tokens
111 private:
112 int32_t nextToken = 0;
113 std::map<int32_t, std::shared_ptr<AssignmentAsset>> groups;
114 std::map<std::string, std::shared_ptr<AssignmentAsset>> assets;
115 };
116}
void Invalidate(const std::string &name, uint64_t now)
void Complete(AssignmentAsset &asset, bool success)
AssignmentAsset * Pending(int32_t token)
void RequestDue(uint64_t now, Send send)
const AssignmentAsset * Find(int32_t group) const
std::map< std::string, std::shared_ptr< AssignmentAsset > > assets
bool Assign(int32_t group, const char *filename, size_t capacity)
std::map< int32_t, std::shared_ptr< AssignmentAsset > > groups
bool Contains(const std::string &name) const
constexpr size_t MaxFileBytes
constexpr unsigned char AssignmentType
constexpr unsigned char RequestType
constexpr uint64_t InitialRetryDelayMs
bool Filename(const char *text, size_t capacity, std::string &result)