Splits incoming data into lines using a fixed-size circular buffer, enabling zero-copy writes via Acquire() and Commit(). More...
#include <sockutils.hpp>
Public Member Functions | |
TZeroCopyLineSplitter (int maxLen) | |
Constructs a zero-copy line splitter with a fixed ring buffer capacity. | |
TLine | Pop () |
Extracts and removes the next complete line from the buffer, if available. | |
std::span< char > | Acquire (size_t size) |
Reserves space in the circular buffer for writing data directly (e.g., from a socket read) without extra copying. | |
void | Commit (size_t size) |
Finalizes the amount of data written into the span returned by Acquire(). | |
void | Push (const char *p, size_t len) |
(Optional) Copies data from an external buffer into the circular buffer. | |
Splits incoming data into lines using a fixed-size circular buffer, enabling zero-copy writes via Acquire() and Commit().
This class maintains a ring buffer of maximum length (maxLen
) where new data can be placed without extra copying. To add data, a typical workflow would be:
std::span<char>
in the internal buffer.read
or ReadSome
).Lines can then be extracted using Pop(), which returns a TLine
holding up to two string-view segments if the line crosses the buffer boundary.
NNet::TZeroCopyLineSplitter::TZeroCopyLineSplitter | ( | int | maxLen | ) |
Constructs a zero-copy line splitter with a fixed ring buffer capacity.
maxLen | The maximum number of bytes the buffer can hold. |
std::span< char > NNet::TZeroCopyLineSplitter::Acquire | ( | size_t | size | ) |
Reserves space in the circular buffer for writing data directly (e.g., from a socket read) without extra copying.
This method returns a contiguous block of available space as a std::span<char>
. If the ring buffer wraps around, you might only get the block up to the end; you can call Acquire() again for any remaining space, depending on your logic.
size | The desired number of bytes to acquire. |
std::span<char>
pointing to the ring buffer region where data can be written. Its size might be less than requested if there's less contiguous space available.void NNet::TZeroCopyLineSplitter::Commit | ( | size_t | size | ) |
TLine NNet::TZeroCopyLineSplitter::Pop | ( | ) |
Extracts and removes the next complete line from the buffer, if available.
A line is typically delimited by a newline character (implementation-specific). If the line crosses the circular boundary, the returned TLine
will contain two segments (Part1
and Part2
).
TLine
object with up to two std::string_view
segments referencing the internal ring buffer. If there is no complete line available, behavior is implementation-defined (it may return an empty TLine
or throw an exception).void NNet::TZeroCopyLineSplitter::Push | ( | const char * | p, |
size_t | len ) |
(Optional) Copies data from an external buffer into the circular buffer.
While the main purpose of this class is zero-copy insertion via Acquire() and Commit(), this method offers a fallback for situations where you already have data in a separate buffer and wish to write it into the splitter in one call.
p | Pointer to the data to copy from. |
len | Number of bytes to copy. |