COROIO: NNet::TLineReader< TSocket > Class Template Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TLineReader< TSocket > Class Template Reference

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< TLineRead ()
 Reads and returns the next complete line from the socket.
 

Detailed Description

template<typename TSocket>
class NNet::TLineReader< TSocket >

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).

Template Parameters
TSocketThe socket type used for reading. TSocket must provide a method: TValueTask<ssize_t> ReadSome(void* buffer, size_t size) that reads data asynchronously.

Example Usage

// Example function that processes lines read from the socket:
TValueTask<void> processLines(TSocket& socket) {
TLineReader<TSocket> lineReader(socket);
while (true) {
TLine line = co_await lineReader.Read();
if (!line) {
// No complete line is available (or end-of-stream reached)
break;
}
// Process the line here (line.Part1 and line.Part2 if the line wraps)
}
co_return;
}
High-level asynchronous socket for network communication.
Definition socket.hpp:364
Definition sockutils.hpp:9
TLineReader(TSocket &socket, int maxLineSize=4096)
Constructs a line reader with the given socket and maximum line size.
Definition sockutils.hpp:587

Constructor & Destructor Documentation

◆ TLineReader()

template<typename TSocket>
NNet::TLineReader< TSocket >::TLineReader ( TSocket & socket,
int maxLineSize = 4096 )
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.

Parameters
socketThe socket used for reading data.
maxLineSizeMaximum allowed size for a line. Default is 4096 bytes.

Member Function Documentation

◆ Read()

template<typename TSocket>
TFuture< TLine > NNet::TLineReader< TSocket >::Read ( )
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).

Returns
A TValueTask<TLine> that completes once a full line is available.

The documentation for this class was generated from the following file: