HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
packet.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#include "../macros.hpp"
6#pragma once
7
8namespace hi::inline v1 {
9
12class packet {
13 std::byte *data;
14 std::byte *data_end;
15 std::byte *first;
16 std::byte *last;
17 bool _pushed = false;
18
19public:
22 packet(ssize_t nrBytes) noexcept
23 {
24 data = new std::byte[nrBytes];
25 data_end = data + nrBytes;
26 first = data;
27 last = data;
28 }
29
30 ~packet() noexcept
31 {
32 delete[] data;
33 }
34
35 packet(packet const &rhs) noexcept = delete;
36 packet operator=(packet const &rhs) noexcept = delete;
37
38 packet(packet &&rhs) noexcept : data(rhs.data), data_end(rhs.data_end), first(rhs.first), last(rhs.last)
39 {
40 rhs.data = nullptr;
41 }
42
43 [[nodiscard]] std::byte *begin() noexcept
44 {
45 return first;
46 }
47
48 [[nodiscard]] std::byte *end() noexcept
49 {
50 return last;
51 }
52
55 [[nodiscard]] ssize_t readSize() const noexcept
56 {
57 return last - first;
58 }
59
62 [[nodiscard]] ssize_t writeSize() const noexcept
63 {
64 return data_end - last;
65 }
66
69 [[nodiscard]] bool pushed() const noexcept
70 {
71 return _pushed;
72 }
73
76 void push() noexcept
77 {
78 _pushed = true;
79 }
80
84 void write(ssize_t nrBytes) noexcept
85 {
86 last += nrBytes;
87 hi_assert(last <= data_end);
88 }
89
93 void read(ssize_t nrBytes) noexcept
94 {
95 first += nrBytes;
96 hi_assert(first <= last);
97 }
98};
99
100} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A network message or stream buffer.
Definition packet.hpp:12
packet(ssize_t nrBytes) noexcept
Allocate an empty packet of a certain size.
Definition packet.hpp:22
void read(ssize_t nrBytes) noexcept
Consume a read.
Definition packet.hpp:93
ssize_t readSize() const noexcept
How many bytes can be read from this buffer.
Definition packet.hpp:55
void write(ssize_t nrBytes) noexcept
Commit a write.
Definition packet.hpp:84
bool pushed() const noexcept
Should this packet be pushed onto the network.
Definition packet.hpp:69
void push() noexcept
Mark this packet to be pushed to the network.
Definition packet.hpp:76
ssize_t writeSize() const noexcept
How many bytes can still be written to this buffer.
Definition packet.hpp:62
T begin(T... args)
T end(T... args)