HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
packet_buffer.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#pragma once
7
8namespace tt {
9
11 std::list<packet> packets;
12 ssize_t _totalNrBytes;
13 bool _closed;
14
15public:
19 bool closed() const noexcept {
20 return _closed;
21 }
22
25 ssize_t nrBytes() const noexcept {
26 return _totalNrBytes;
27 }
28
34 ssize_t nrpackets() const noexcept {
35 return packets.size();
36 }
37
40 void close() noexcept {
41 _closed = true;
42 }
43
47 std::span<std::byte> getNewpacket(ssize_t nrBytes) noexcept {
48 tt_assert(!closed());
49 packets.emplace_back(nrBytes);
50 return {packets.back().end(), nrBytes};
51 }
52
56 std::span<std::byte> getpacket(ssize_t nrBytes) noexcept {
57 tt_assert(!closed());
58 if (packets.empty() || (packets.back().writeSize() < nrBytes)) {
59 packets.emplace_back(nrBytes);
60 }
61 return {packets.back().end(), nrBytes};
62 }
63
71 void write(ssize_t nrBytes, bool push=true) noexcept {
72 tt_assert(!closed());
73 packets.back().write(nrBytes);
74 if (push) {
75 packets.back().push();
76 }
77 _totalNrBytes += nrBytes;
78 }
79
86 std::span<std::byte const> peek(ssize_t nrBytes) {
87 if (packets.empty() || size() < nrBytes) {
88 return {};
89 }
90
91 while (true) {
92 if (packets.front().readSize() >= nrBytes) {
93 return {packets.front().readSize(), std::ssize(packets)};
94 }
95
96 // Check if we can merge packets.
97 tt_assert(packets.front().size() >= nrBytes);
98
99 // Merge data from next packet.
100 }
101 }
102
110 std::string_view peekLine(ssize_t nrBytes=1024) {
111 ssize_t packetNr = 0;
112 ssize_t byteNr = 0;
113 ssize_t i = 0;
114 while (packetNr < nrpackets()) {
115 tt_parse_check(byteNr < nrBytes, "New-line not found within {} bytes", nrBytes);
116
117 if (i == std::ssize(packets[packetNr])) {
118 // Advance to next packet.
119 ++packetNr;
120 i = 0;
121 }
122
123 ttlet c = packets[packetNr][i]
124 if (c == '\n' || c == '\0') {
125 // Found end-of-line
126 ttlet bspan = peek(byteNr + 1);
127 return {reinterpret_cast<char *>(bspan.data(), byteNr + 1};
128 }
129 ++i;
130 ++byteNr;
131 }
132
133 // Not enough bytes read yet.
134 return {}
135 }
136
143 void read(ssize_t nrBytes) noexcept {
144 peekBuffer.clear();
145
146 while (nrBytes) {
147 ttlet packet_size = std::ssize(packets.front());
148 if (nrBytes >= packet_size) {
149 packets.pop_front();
150 } else {
151 packets.front().read(nrBytes);
152 tt_axiom(std::ssize(packets.front()) > 0);
153 }
154 nrBytes -= std::ssize(packets_size);
155 }
156 }
157
158
159};
160
161
162}
163
Definition packet_buffer.hpp:10
void read(ssize_t nrBytes) noexcept
Consume the data from the buffer.
Definition packet_buffer.hpp:143
bool closed() const noexcept
Connection is closed.
Definition packet_buffer.hpp:19
ssize_t nrBytes() const noexcept
Total number of bytes in the buffer.
Definition packet_buffer.hpp:25
void close() noexcept
Close the connection on this side.
Definition packet_buffer.hpp:40
std::span< std::byte const > peek(ssize_t nrBytes)
Peek into the data without consuming.
Definition packet_buffer.hpp:86
void write(ssize_t nrBytes, bool push=true) noexcept
Write the data added to the packet.
Definition packet_buffer.hpp:71
ssize_t nrpackets() const noexcept
Total number of packets in the buffer.
Definition packet_buffer.hpp:34
std::span< std::byte > getpacket(ssize_t nrBytes) noexcept
Get a packet to write a stream of bytes into.
Definition packet_buffer.hpp:56
std::span< std::byte > getNewpacket(ssize_t nrBytes) noexcept
Get a new packet to write a message into.
Definition packet_buffer.hpp:47
std::string_view peekLine(ssize_t nrBytes=1024)
Peek into the data a single text-line without consuming.
Definition packet_buffer.hpp:110
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)