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
7#include "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <cstddef>
10
11hi_export_module(hikogui.net.packet);
12
13hi_export namespace hi::inline v1 {
14
17class packet {
18 std::byte *data;
19 std::byte *data_end;
20 std::byte *first;
21 std::byte *last;
22 bool _pushed = false;
23
24public:
27 packet(ptrdiff_t nrBytes) noexcept
28 {
29 data = new std::byte[nrBytes];
30 data_end = data + nrBytes;
31 first = data;
32 last = data;
33 }
34
35 ~packet() noexcept
36 {
37 delete[] data;
38 }
39
40 packet(packet const &rhs) noexcept = delete;
41 packet operator=(packet const &rhs) noexcept = delete;
42
43 packet(packet &&rhs) noexcept : data(rhs.data), data_end(rhs.data_end), first(rhs.first), last(rhs.last)
44 {
45 rhs.data = nullptr;
46 }
47
48 [[nodiscard]] std::byte *begin() noexcept
49 {
50 return first;
51 }
52
53 [[nodiscard]] std::byte *end() noexcept
54 {
55 return last;
56 }
57
60 [[nodiscard]] ptrdiff_t readSize() const noexcept
61 {
62 return last - first;
63 }
64
67 [[nodiscard]] ptrdiff_t writeSize() const noexcept
68 {
69 return data_end - last;
70 }
71
74 [[nodiscard]] bool pushed() const noexcept
75 {
76 return _pushed;
77 }
78
81 void push() noexcept
82 {
83 _pushed = true;
84 }
85
89 void write(ptrdiff_t nrBytes) noexcept
90 {
91 last += nrBytes;
92 hi_assert(last <= data_end);
93 }
94
98 void read(ptrdiff_t nrBytes) noexcept
99 {
100 first += nrBytes;
101 hi_assert(first <= last);
102 }
103};
104
105} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A network message or stream buffer.
Definition packet.hpp:17
packet(ptrdiff_t nrBytes) noexcept
Allocate an empty packet of a certain size.
Definition packet.hpp:27
ptrdiff_t writeSize() const noexcept
How many bytes can still be written to this buffer.
Definition packet.hpp:67
ptrdiff_t readSize() const noexcept
How many bytes can be read from this buffer.
Definition packet.hpp:60
bool pushed() const noexcept
Should this packet be pushed onto the network.
Definition packet.hpp:74
void push() noexcept
Mark this packet to be pushed to the network.
Definition packet.hpp:81
void write(ptrdiff_t nrBytes) noexcept
Commit a write.
Definition packet.hpp:89
void read(ptrdiff_t nrBytes) noexcept
Consume a read.
Definition packet.hpp:98
T begin(T... args)
T end(T... args)