HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
PacketBuffer.hpp
1
2
3#pragma once
4
5namespace tt {
6
8 std::list<Packet> packets;
9 ssize_t _totalNrBytes;
10 bool _closed;
11
12public:
16 bool closed() const noexcept {
17 return _closed;
18 }
19
22 ssize_t nrBytes() const noexcept {
23 return _totalNrBytes;
24 }
25
31 ssize_t nrPackets() const noexcept {
32 return packets.size();
33 }
34
37 void close() noexcept {
38 _closed = true;
39 }
40
44 nonstd::span<std::byte> getNewPacket(ssize_t nrBytes) noexcept {
45 tt_assert(!closed());
46 packets.emplace_back(nrBytes);
47 return {packets.back().end(), nrBytes};
48 }
49
53 nonstd::span<std::byte> getPacket(ssize_t nrBytes) noexcept {
54 tt_assert(!closed());
55 if (packets.empty() || (packets.back().writeSize() < nrBytes)) {
56 packets.emplace_back(nrBytes);
57 }
58 return {packets.back().end(), nrBytes};
59 }
60
68 void write(ssize_t nrBytes, bool push=true) noexcept {
69 tt_assert(!closed());
70 packets.back().write(nrBytes);
71 if (push) {
72 packets.back().push();
73 }
74 _totalNrBytes += nrBytes;
75 }
76
83 nonstd::span<std::byte const> peek(ssize_t nrBytes) {
84 if (packets.empty() || size() < nrBytes) {
85 return {};
86 }
87
88 while (true) {
89 if (packets.front().readSize() >= nrBytes) {
90 return {packets.front().readSize(), ssize(packets)};
91 }
92
93 // Check if we can merge packets.
94 tt_assert(packets.front().size() >= nrBytes);
95
96 // Merge data from next packet.
97 }
98 }
99
107 std::string_view peekLine(ssize_t nrBytes=1024) {
108 ssize_t packetNr = 0;
109 ssize_t byteNr = 0;
110 ssize_t i = 0;
111 while (packetNr < nrPackets()) {
112 parse_assert(byteNr < nrBytes, "New-line not found within {} bytes", nrBytes);
113
114 if (i == ssize(packets[packetNr])) {
115 // Advance to next packet.
116 ++packetNr;
117 i = 0;
118 }
119
120 ttlet c = packets[packetNr][i]
121 if (c == '\n' || c == '\0') {
122 // Found end-of-line
123 ttlet bspan = peek(byteNr + 1);
124 return {reinterpret_cast<char *>(bspan.data(), byteNr + 1};
125 }
126 ++i;
127 ++byteNr;
128 }
129
130 // Not enough bytes read yet.
131 return {}
132 }
133
140 void read(ssize_t nrBytes) noexcept {
141 peekBuffer.clear();
142
143 while (nrBytes) {
144 ttlet packet_size = ssize(packets.front());
145 if (nrBytes >= packet_size) {
146 packets.pop_front();
147 } else {
148 packets.front().read(nrBytes);
149 tt_assume(ssize(packets.front()) > 0);
150 }
151 nrBytes -= ssize(packets_size);
152 }
153 }
154
155
156};
157
158
159}
160
Definition PacketBuffer.hpp:7
void read(ssize_t nrBytes) noexcept
Consume the data from the buffer.
Definition PacketBuffer.hpp:140
void close() noexcept
Close the connection on this side.
Definition PacketBuffer.hpp:37
ssize_t nrPackets() const noexcept
Total number of packets in the buffer.
Definition PacketBuffer.hpp:31
std::string_view peekLine(ssize_t nrBytes=1024)
Peek into the data a single text-line without consuming.
Definition PacketBuffer.hpp:107
ssize_t nrBytes() const noexcept
Total number of bytes in the buffer.
Definition PacketBuffer.hpp:22
nonstd::span< std::byte > getNewPacket(ssize_t nrBytes) noexcept
Get a new packet to write a message into.
Definition PacketBuffer.hpp:44
nonstd::span< std::byte > getPacket(ssize_t nrBytes) noexcept
Get a packet to write a stream of bytes into.
Definition PacketBuffer.hpp:53
void write(ssize_t nrBytes, bool push=true) noexcept
Write the data added to the packet.
Definition PacketBuffer.hpp:68
nonstd::span< std::byte const > peek(ssize_t nrBytes)
Peek into the data without consuming.
Definition PacketBuffer.hpp:83
bool closed() const noexcept
Connection is closed.
Definition PacketBuffer.hpp:16
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)