Rise
The Vieneo Province
Toggle main menu visibility
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>
5
#include "
Viewscreen/CloudWeatherMap.h
"
6
7
namespace
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
}
48
class
Assembler
49
{
50
Header
header
;
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
}
CloudWeatherMap.h
CloudWeather::Assembler
Definition
CloudWeatherProtocol.h:49
CloudWeather::Assembler::count
unsigned count
Definition
CloudWeatherProtocol.h:52
CloudWeather::Assembler::Reset
void Reset()
Definition
CloudWeatherProtocol.h:55
CloudWeather::Assembler::Accept
MapPtr Accept(const uint8_t *packet, size_t size)
Definition
CloudWeatherProtocol.h:56
CloudWeather::Assembler::received
std::vector< uint8_t > received
Definition
CloudWeatherProtocol.h:51
CloudWeather::Assembler::encoded
std::vector< uint8_t > encoded
Definition
CloudWeatherProtocol.h:51
CloudWeather::Assembler::header
Header header
Definition
CloudWeatherProtocol.h:50
CloudWeather::Assembler::newest
uint64_t newest
Definition
CloudWeatherProtocol.h:53
CloudWeather
Definition
CloudWeatherProtocol.h:8
CloudWeather::ChunkBytes
constexpr unsigned ChunkBytes
Definition
CloudWeatherProtocol.h:9
CloudWeather::HeaderBytes
constexpr unsigned HeaderBytes
Definition
CloudWeatherProtocol.h:9
CloudWeather::MaxDecodedBytes
constexpr unsigned MaxDecodedBytes
Definition
CloudWeatherProtocol.h:10
CloudWeather::U64
uint64_t U64(const uint8_t *p)
Definition
CloudWeatherProtocol.h:13
CloudWeather::U32
uint32_t U32(const uint8_t *p)
Definition
CloudWeatherProtocol.h:12
CloudWeather::Crc32
uint32_t Crc32(const std::vector< uint8_t > &bytes)
Definition
CloudWeatherProtocol.h:14
CloudWeather::Parse
bool Parse(const uint8_t *p, size_t length, Header &h)
Definition
CloudWeatherProtocol.h:34
CloudWeather::MapPtr
std::shared_ptr< const Map > MapPtr
Definition
CloudWeatherMap.h:20
CloudWeather::BuildMips
void BuildMips(Map &map)
Definition
CloudWeatherMap.h:26
CloudWeather::MessageOffset
constexpr unsigned MessageOffset
Definition
CloudWeatherProtocol.h:9
CloudWeather::U16
uint16_t U16(const uint8_t *p)
Definition
CloudWeatherProtocol.h:11
CloudWeather::Header
Definition
CloudWeatherProtocol.h:26
CloudWeather::Header::width
unsigned width
Definition
CloudWeatherProtocol.h:27
CloudWeather::Header::crc
unsigned crc
Definition
CloudWeatherProtocol.h:27
CloudWeather::Header::height
unsigned height
Definition
CloudWeatherProtocol.h:27
CloudWeather::Header::encoded
unsigned encoded
Definition
CloudWeatherProtocol.h:27
CloudWeather::Header::revision
uint64_t revision
Definition
CloudWeatherProtocol.h:28
CloudWeather::Header::SameSnapshot
bool SameSnapshot(const Header &h) const
Definition
CloudWeatherProtocol.h:29
CloudWeather::Header::count
unsigned count
Definition
CloudWeatherProtocol.h:27
CloudWeather::Header::offset
unsigned offset
Definition
CloudWeatherProtocol.h:27
CloudWeather::Header::time
uint64_t time
Definition
CloudWeatherProtocol.h:28
CloudWeather::Header::decoded
unsigned decoded
Definition
CloudWeatherProtocol.h:27
CloudWeather::Header::encoding
unsigned encoding
Definition
CloudWeatherProtocol.h:27
CloudWeather::Mip
Definition
CloudWeatherMap.h:11
CloudWeather::Mip::width
unsigned width
Definition
CloudWeatherMap.h:12
CloudWeather::Mip::pixels
std::vector< uint8_t > pixels
Definition
CloudWeatherMap.h:13
CloudWeather::Mip::height
unsigned height
Definition
CloudWeatherMap.h:12
CloudWeatherProtocol.h
Generated on
for Rise by
1.18.0