Reads a complete line from a socket using a zero-copy line splitter. More...
#include <sockutils.hpp>
Public Member Functions | |
TLineReader (TSocket &socket, int maxLineSize=4096) | |
Constructs a line reader with the given socket and maximum line size. | |
TFuture< TLine > | Read () |
Reads and returns the next complete line from the socket. | |
Reads a complete line from a socket using a zero-copy line splitter.
This class encapsulates a socket and a zero-copy line splitter to provide efficient line-based reading. Data is read from the socket in chunks and fed into a circular buffer maintained by the splitter. The splitter then extracts complete lines as soon as they become available.
The maximum line size can be specified via the constructor (default is 4096 bytes); a chunk size (default to half of the maximum line size) is used when acquiring space from the splitter for new data. Each call to Read() returns a complete line (of type TLine
). A TLine
typically contains string views Part1
and Part2
, which together represent the line (useful when a line wraps around the circular buffer).
TSocket | The socket type used for reading. TSocket must provide a method: TValueTask<ssize_t> ReadSome(void* buffer, size_t size) that reads data asynchronously. |
|
inline |
Constructs a line reader with the given socket and maximum line size.
The maximum line size (default 4096) determines the capacity of the underlying zero-copy line splitter. The chunk size is set to half the maximum line size.
socket | The socket used for reading data. |
maxLineSize | Maximum allowed size for a line. Default is 4096 bytes. |
|
inline |
Reads and returns the next complete line from the socket.
This method repeatedly attempts to pop a complete line from the splitter. If no complete line is available, it acquires a chunk of the splitter’s internal buffer and reads data from the socket into that space. After committing the newly read data, it retries extracting a line. The method returns a TLine
(which may contain two parts, if the line wraps around the circular buffer).