HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
packet_buffer.hpp
1// Copyright Take Vos 2020-2022.
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
10 std::list<packet> packets;
11 ssize_t _totalNrBytes;
12 bool _closed;
13
14public:
18 bool closed() const noexcept
19 {
20 return _closed;
21 }
22
25 ssize_t nrBytes() const noexcept
26 {
27 return _totalNrBytes;
28 }
29
35 ssize_t nrpackets() const noexcept
36 {
37 return packets.size();
38 }
39
42 void close() noexcept
43 {
44 _closed = true;
45 }
46
50 std::span<std::byte> getNewpacket(ssize_t nrBytes) noexcept
51 {
52 hi_assert(!closed());
53 packets.emplace_back(nrBytes);
54 return {packets.back().end(), nrBytes};
55 }
56
60 std::span<std::byte> getpacket(ssize_t nrBytes) noexcept
61 {
62 hi_assert(!closed());
63 if (packets.empty() || (packets.back().writeSize() < nrBytes)) {
64 packets.emplace_back(nrBytes);
65 }
66 return {packets.back().end(), nrBytes};
67 }
68
76 void write(ssize_t nrBytes, bool push = true) noexcept
77 {
78 hi_assert(!closed());
79 packets.back().write(nrBytes);
80 if (push) {
81 packets.back().push();
82 }
83 _totalNrBytes += nrBytes;
84 }
85
92 std::span<std::byte const> peek(ssize_t nrBytes)
93 {
94 if (packets.empty() || size() < nrBytes) {
95 return {};
96 }
97
98 while (true) {
99 if (packets.front().readSize() >= nrBytes) {
100 return {packets.front().readSize(), ssize(packets)};
101 }
102
103 // Check if we can merge packets.
104 hi_assert(packets.front().size() >= nrBytes);
105
106 // Merge data from next packet.
107 }
108 }
109
117 std::string_view peekLine(ssize_t nrBytes = 1024)
118 {
119 ssize_t packetNr = 0;
120 ssize_t byteNr = 0;
121 ssize_t i = 0;
122 while (packetNr < nrpackets()) {
123 hi_check(byteNr < nrBytes, "New-line not found within {} bytes", nrBytes);
124
125 if (i == ssize(packets[packetNr])) {
126 // Advance to next packet.
127 ++packetNr;
128 i = 0;
129 }
130
131 hilet c = packets[packetNr][i] if (c == '\n' || c == '\0')
132 {
133 // Found end-of-line
134 hilet bspan = peek(byteNr + 1);
135 return {reinterpret_cast<char *>(bspan.data()), byteNr + 1};
136 }
137 ++i;
138 ++byteNr;
139 }
140
141 // Not enough bytes read yet.
142 return {}
143 }
144
151 void read(ssize_t nrBytes) noexcept
152 {
153 peekBuffer.clear();
154
155 while (nrBytes) {
156 hilet packet_size = ssize(packets.front());
157 if (nrBytes >= packet_size) {
158 packets.pop_front();
159 } else {
160 packets.front().read(nrBytes);
161 hi_assert(ssize(packets.front()) > 0);
162 }
163 nrBytes -= ssize(packets_size);
164 }
165 }
166};
167
168} // namespace hi::inline v1
#define hi_check(expression, message,...)
Check if the expression is valid, or throw a parse_error.
Definition assert.hpp:110
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
Definition packet_buffer.hpp:9
void read(ssize_t nrBytes) noexcept
Consume the data from the buffer.
Definition packet_buffer.hpp:151
std::span< std::byte > getNewpacket(ssize_t nrBytes) noexcept
Get a new packet to write a message into.
Definition packet_buffer.hpp:50
std::string_view peekLine(ssize_t nrBytes=1024)
Peek into the data a single text-line without consuming.
Definition packet_buffer.hpp:117
std::span< std::byte const > peek(ssize_t nrBytes)
Peek into the data without consuming.
Definition packet_buffer.hpp:92
bool closed() const noexcept
Connection is closed.
Definition packet_buffer.hpp:18
std::span< std::byte > getpacket(ssize_t nrBytes) noexcept
Get a packet to write a stream of bytes into.
Definition packet_buffer.hpp:60
void close() noexcept
Close the connection on this side.
Definition packet_buffer.hpp:42
void write(ssize_t nrBytes, bool push=true) noexcept
Write the data added to the packet.
Definition packet_buffer.hpp:76
ssize_t nrBytes() const noexcept
Total number of bytes in the buffer.
Definition packet_buffer.hpp:25
ssize_t nrpackets() const noexcept
Total number of packets in the buffer.
Definition packet_buffer.hpp:35
T back(T... args)
T emplace_back(T... args)
T empty(T... args)
T front(T... args)
T pop_front(T... args)
T size(T... args)