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