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

A utility for reading data from a socket-like object, either a fixed number of bytes or until a specified delimiter. More...

#include <sockutils.hpp>

Public Member Functions

 TByteReader (TSocket &socket)
 Constructs a reader for the given socket.
 
TFuture< void > Read (void *data, size_t size)
 Reads exactly size bytes and stores them into data.
 
TFuture< std::string > ReadUntil (const std::string &delimiter)
 Reads data until the given delimiter is encountered.
 

Detailed Description

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

A utility for reading data from a socket-like object, either a fixed number of bytes or until a specified delimiter.

This class manages an internal buffer so that any data read beyond what is immediately requested can be stored and used in subsequent reads.

Template Parameters
TSocketThe socket type used for reading bytes. It must provide a method TFuture<ssize_t> ReadSome(void* buffer, size_t size) which returns:
  • 0 on connection closure,
  • a positive number for the count of bytes successfully read,
  • a negative number to indicate a recoverable read error (in which case a retry may be attempted).

Example Usage

TFuture<void> ExampleFunction(TSocket& socket) {
TByteReader<TSocket> reader(socket);
// Read a fixed number of bytes
char data[128];
co_await reader.Read(data, sizeof(data));
// 'data' now contains 128 bytes read from the socket (or an exception is thrown on closure).
// Read until a specific delimiter, e.g. "\r\n"
std::string line = co_await reader.ReadUntil("\r\n");
// 'line' includes the "\r\n" delimiter at the end.
co_return;
}
High-level asynchronous socket for network communication.
Definition socket.hpp:364
TByteReader(TSocket &socket)
Constructs a reader for the given socket.
Definition sockutils.hpp:60
Future type for coroutines returning a value of type T.
Definition corochain.hpp:177

Constructor & Destructor Documentation

◆ TByteReader()

template<typename TSocket>
NNet::TByteReader< TSocket >::TByteReader ( TSocket & socket)
inline

Constructs a reader for the given socket.

Parameters
socketReference to a socket-like object used for reading.

Member Function Documentation

◆ Read()

template<typename TSocket>
TFuture< void > NNet::TByteReader< TSocket >::Read ( void * data,
size_t size )
inline

Reads exactly size bytes and stores them into data.

  • First uses any leftover bytes in the internal buffer.
  • If more bytes are needed, repeatedly calls the socket's ReadSome() until the requested amount is fulfilled.
  • Throws a std::runtime_error if the socket is closed (ReadSome returns 0) before all requested bytes are read.
  • If ReadSome returns a negative value, the read is retried.
Parameters
dataPointer to the buffer where bytes will be stored.
sizeThe exact number of bytes to read.
Returns
A TFuture<void> that completes when all bytes are read.
Exceptions
std::runtime_errorIf the connection is closed before size bytes are fully read.

◆ ReadUntil()

template<typename TSocket>
TFuture< std::string > NNet::TByteReader< TSocket >::ReadUntil ( const std::string & delimiter)
inline

Reads data until the given delimiter is encountered.

  • Searches the internal buffer for the delimiter.
  • If found, returns all data up to and including the delimiter.
  • If not found, appends the buffered data to the result and tries reading more data from the socket, repeating until the delimiter appears.
  • Throws a std::runtime_error if the socket is closed (ReadSome returns 0) before the delimiter is found.
  • If ReadSome returns a negative value, the read is retried.
Parameters
delimiterThe string to look for in the incoming data.
Returns
A TFuture<std::string> that completes once the delimiter is found, returning everything up to and including that delimiter.
Exceptions
std::runtime_errorIf the connection is closed before the delimiter is found.

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