5#include "../macros.hpp"
12 ssize_t _totalNrBytes;
38 return packets.
size();
55 return {packets.
back().end(), nrBytes};
61 std::span<std::byte>
getpacket(ssize_t nrBytes)
noexcept
64 if (packets.
empty() || (packets.
back().writeSize() < nrBytes)) {
67 return {packets.
back().end(), nrBytes};
77 void write(ssize_t nrBytes,
bool push =
true) noexcept
80 packets.
back().write(nrBytes);
82 packets.
back().push();
84 _totalNrBytes += nrBytes;
93 std::span<std::byte const>
peek(ssize_t nrBytes)
95 if (packets.
empty() || size() < nrBytes) {
100 if (packets.
front().readSize() >= nrBytes) {
101 return {packets.
front().readSize(), ssize(packets)};
105 hi_assert(packets.
front().size() >= nrBytes);
120 ssize_t packetNr = 0;
123 while (packetNr < nrpackets()) {
124 hi_check(byteNr < nrBytes,
"New-line not found within {} bytes", nrBytes);
126 if (i == ssize(packets[packetNr])) {
132 hilet c = packets[packetNr][i]
if (c ==
'\n' || c ==
'\0')
135 hilet bspan = peek(byteNr + 1);
136 return {
reinterpret_cast<char *
>(bspan.data()), byteNr + 1};
152 void read(ssize_t nrBytes)
noexcept
157 hilet packet_size = ssize(packets.
front());
158 if (nrBytes >= packet_size) {
161 packets.
front().read(nrBytes);
162 hi_assert(ssize(packets.
front()) > 0);
164 nrBytes -= ssize(packets_size);
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 emplace_back(T... args)