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#pragma once
6
7namespace hi::inline v1 {
8
11class packet {
12 std::byte *data;
13 std::byte *data_end;
14 std::byte *first;
15 std::byte *last;
16 bool _pushed = false;
17
18public:
21 packet(ssize_t nrBytes) noexcept
22 {
23 data = new std::byte[nrBytes];
24 data_end = data + nrBytes;
25 first = data;
26 last = data;
27 }
28
29 ~packet() noexcept
30 {
31 delete[] data;
32 }
33
34 packet(packet const &rhs) noexcept = delete;
35 packet operator=(packet const &rhs) noexcept = delete;
36
37 packet(packet &&rhs) noexcept : data(rhs.data), data_end(rhs.data_end), first(rhs.first), last(rhs.last)
38 {
39 rhs.data = nullptr;
40 }
41
42 [[nodiscard]] std::byte *begin() noexcept
43 {
44 return first;
45 }
46
47 [[nodiscard]] std::byte *end() noexcept
48 {
49 return last;
50 }
51
54 [[nodiscard]] ssize_t readSize() const noexcept
55 {
56 return last - first;
57 }
58
61 [[nodiscard]] ssize_t writeSize() const noexcept
62 {
63 return data_end - last;
64 }
65
68 [[nodiscard]] bool pushed() const noexcept
69 {
70 return _pushed;
71 }
72
75 void push() noexcept
76 {
77 _pushed = true;
78 }
79
83 void write(ssize_t nrBytes) noexcept
84 {
85 last += nrBytes;
86 hi_assert(last <= data_end);
87 }
88
92 void read(ssize_t nrBytes) noexcept
93 {
94 first += nrBytes;
95 hi_assert(first <= last);
96 }
97};
98
99} // namespace hi::inline v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
DOXYGEN BUG.
Definition algorithm.hpp:13
A network message or stream buffer.
Definition packet.hpp:11
packet(ssize_t nrBytes) noexcept
Allocate an empty packet of a certain size.
Definition packet.hpp:21
void read(ssize_t nrBytes) noexcept
Consume a read.
Definition packet.hpp:92
ssize_t readSize() const noexcept
How many bytes can be read from this buffer.
Definition packet.hpp:54
void write(ssize_t nrBytes) noexcept
Commit a write.
Definition packet.hpp:83
bool pushed() const noexcept
Should this packet be pushed onto the network.
Definition packet.hpp:68
void push() noexcept
Mark this packet to be pushed to the network.
Definition packet.hpp:75
ssize_t writeSize() const noexcept
How many bytes can still be written to this buffer.
Definition packet.hpp:61
T begin(T... args)
T end(T... args)