Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudWeatherProtocol.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <cstdint>
4#include <vector>
6
7namespace CloudWeather
8{
9 constexpr unsigned MessageOffset = 11, HeaderBytes = 48, ChunkBytes = 16384;
10 constexpr unsigned MaxDecodedBytes = 4096 * 2048;
11 inline uint16_t U16(const uint8_t* p) { return uint16_t(p[0]) | uint16_t(p[1]) << 8; }
12 inline uint32_t U32(const uint8_t* p) { return uint32_t(U16(p)) | uint32_t(U16(p + 2)) << 16; }
13 inline uint64_t U64(const uint8_t* p) { return uint64_t(U32(p)) | uint64_t(U32(p + 4)) << 32; }
14 inline uint32_t Crc32(const std::vector<uint8_t>& bytes)
15 {
16 static const auto table = [] {
17 std::vector<uint32_t> t(256);
18 for (unsigned i = 0; i < 256; ++i) { uint32_t c = i; for (int b = 0; b < 8; ++b) c = (c >> 1) ^ (0xedb88320u & (0u - (c & 1))); t[i] = c; }
19 return t;
20 }();
21 uint32_t c = 0xffffffffu;
22 for (auto b : bytes) c = table[(c ^ b) & 255] ^ (c >> 8);
23 return ~c;
24 }
25 struct Header
26 {
27 unsigned encoding = 0, width = 0, height = 0, decoded = 0, encoded = 0, offset = 0, count = 0, crc = 0;
28 uint64_t revision = 0, time = 0;
29 bool SameSnapshot(const Header& h) const {
30 return encoding == h.encoding && width == h.width && height == h.height && decoded == h.decoded
31 && encoded == h.encoded && crc == h.crc && revision == h.revision && time == h.time;
32 }
33 };
34 inline bool Parse(const uint8_t* p, size_t length, Header& h)
35 {
36 // Byte zero is checked by the dedicated network dispatch.
37 if (length<HeaderBytes || length>HeaderBytes + ChunkBytes || p[1] != 1 || p[2] > 1 || p[3] != 1
38 || p[38] != 1 || p[39] != 1 || U32(p + 44) != 0) return false;
39 h.encoding = p[2]; h.width = U16(p + 4); h.height = U16(p + 6);
40 h.revision = U64(p + 8); h.time = U64(p + 16); h.decoded = U32(p + 24); h.encoded = U32(p + 28);
41 h.offset = U32(p + 32); h.count = U16(p + 36); h.crc = U32(p + 40);
42 return (h.width == 2048 || h.width == 4096) && h.height == h.width / 2 && h.revision != 0
43 && h.decoded == h.width * h.height && h.encoded > 0 && h.encoded <= h.decoded
44 && (h.encoding != 0 || h.encoded == h.decoded) && h.offset < h.encoded
45 && h.offset % ChunkBytes == 0 && h.count == (std::min)(ChunkBytes, h.encoded - h.offset)
46 && length == HeaderBytes + h.count;
47 }
49 {
51 std::vector<uint8_t> encoded, received;
52 unsigned count = 0;
53 uint64_t newest = 0;
54 public:
55 void Reset() { header = {}; encoded.clear(); received.clear(); count = 0; newest = 0; }
56 MapPtr Accept(const uint8_t* packet, size_t size)
57 {
58 Header h;
59 if (!Parse(packet, size, h) || h.revision < newest) return {};
60 if (h.revision > newest)
61 {
62 newest = h.revision; header = h; count = 0;
63 encoded.assign(h.encoded, 0); received.assign((h.encoded + ChunkBytes - 1) / ChunkBytes, 0);
64 }
65 if (encoded.empty() || !header.SameSnapshot(h)) return {};
66 const unsigned index = h.offset / ChunkBytes;
67 if (received[index])
68 {
69 if (!std::equal(packet + HeaderBytes, packet + size, encoded.begin() + h.offset)) encoded.clear();
70 return {};
71 }
72 std::copy(packet + HeaderBytes, packet + size, encoded.begin() + h.offset);
73 received[index] = 1;
74 if (++count != received.size()) return {};
75 auto map = std::make_shared<Map>(); map->revision = h.revision; map->simulationTimeMs = h.time;
76 Mip base; base.width = h.width; base.height = h.height;
77 if (h.encoding == 0) base.pixels = std::move(encoded);
78 else
79 {
80 base.pixels.reserve(h.decoded);
81 for (size_t pos = 0; pos < encoded.size(); pos += 3)
82 {
83 if (encoded.size() - pos < 3) { encoded.clear(); return {}; }
84 const auto run = U16(encoded.data() + pos);
85 if (!run || run > h.decoded - base.pixels.size()) { encoded.clear(); return {}; }
86 base.pixels.insert(base.pixels.end(), run, encoded[pos + 2]);
87 }
88 encoded.clear();
89 }
90 if (base.pixels.size() != h.decoded || Crc32(base.pixels) != h.crc) return {};
91 map->mips.push_back(std::move(base)); BuildMips(*map);
92 return map;
93 }
94 };
95}
MapPtr Accept(const uint8_t *packet, size_t size)
std::vector< uint8_t > received
std::vector< uint8_t > encoded
constexpr unsigned ChunkBytes
constexpr unsigned HeaderBytes
constexpr unsigned MaxDecodedBytes
uint64_t U64(const uint8_t *p)
uint32_t U32(const uint8_t *p)
uint32_t Crc32(const std::vector< uint8_t > &bytes)
bool Parse(const uint8_t *p, size_t length, Header &h)
std::shared_ptr< const Map > MapPtr
void BuildMips(Map &map)
constexpr unsigned MessageOffset
uint16_t U16(const uint8_t *p)
bool SameSnapshot(const Header &h) const
std::vector< uint8_t > pixels