HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
packet.hpp
1// Copyright Take Vos 2020.
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
6
7#pragma once
8
9namespace tt {
10
13class packet {
14 std::byte *data;
15 std::byte *data_end;
16 std::byte *first;
17 std::byte *last;
18 bool _pushed = false;
19
20public:
23 packet(ssize_t nrBytes) noexcept {
24 data = new std::byte [nrBytes];
25 data_end = data + nrBytes;
26 first = data;
27 last = data;
28 }
29
30 ~packet() noexcept {
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 :
38 data(rhs.data), data_end(rhs.data_end), first(rhs.first), last(rhs.last) {
39 rhs.data = nullptr;
40 }
41
42
43 [[nodiscard]] std::byte *begin() noexcept {
44 return first;
45 }
46
47 [[nodiscard]] std::byte *end() noexcept {
48 return last;
49 }
50
53 [[nodiscard]] ssize_t readSize() const noexcept {
54 return last - first;
55 }
56
59 [[nodiscard]] ssize_t writeSize() const noexcept {
60 return data_end - last;
61 }
62
65 [[nodiscard]] bool pushed() const noexcept {
66 return _pushed;
67 }
68
71 void push() noexcept {
72 _pushed = true;
73 }
74
78 void write(ssize_t nrBytes) noexcept {
79 last += nrBytes;
80 tt_axiom(last <= data_end);
81 }
82
86 void read(ssize_t nrBytes) noexcept {
87 first += nrBytes;
88 tt_axiom(first <= last);
89 }
90};
91
92}
A network message or stream buffer.
Definition packet.hpp:13
packet(ssize_t nrBytes) noexcept
Allocate an empty packet of a certain size.
Definition packet.hpp:23
ssize_t readSize() const noexcept
How many bytes can be read from this buffer.
Definition packet.hpp:53
void write(ssize_t nrBytes) noexcept
Commit a write.
Definition packet.hpp:78
void push() noexcept
Mark this packet to be pushed to the network.
Definition packet.hpp:71
void read(ssize_t nrBytes) noexcept
Consume a read.
Definition packet.hpp:86
bool pushed() const noexcept
Should this packet be pushed onto the network.
Definition packet.hpp:65
ssize_t writeSize() const noexcept
How many bytes can still be written to this buffer.
Definition packet.hpp:59