COROIO: NNet::TStructReader< T, TSocket > Class Template Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TStructReader< T, TSocket > Class Template Reference

A utility for reading a fixed-size structure of type T from a socket-like object. More...

#include <sockutils.hpp>

Public Member Functions

 TStructReader (TSocket &socket)
 Constructs a reader with a reference to the given socket.
 
TFuture< T > Read ()
 Reads a single instance of type T from the socket.
 

Detailed Description

template<typename T, typename TSocket>
class NNet::TStructReader< T, TSocket >

A utility for reading a fixed-size structure of type T from a socket-like object.

This class expects the socket to provide a method TValueTask<ssize_t> ReadSome(void* buffer, size_t size), which returns:

  • 0 if the connection is closed,
  • 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).
Template Parameters
TA trivially copyable (or otherwise byte-serializable) type that can be read directly into memory.
TSocketThe socket type used for reading, offering the required ReadSome() method.

Example Usage

// A simple struct to demonstrate reading a fixed-size block of data:
struct MyData {
int Id;
float Value;
};
TValueTask<void> ExampleFunction(TSocket& socket) {
// Create a reader for MyData structures
// Read one instance of MyData
MyData data = co_await reader.Read();
// 'data' is now populated with the bytes read from the socket
// (assuming the full size of MyData was successfully read).
co_return;
}
High-level asynchronous socket for network communication.
Definition socket.hpp:364
A utility for reading a fixed-size structure of type T from a socket-like object.
Definition sockutils.hpp:288

Constructor & Destructor Documentation

◆ TStructReader()

template<typename T, typename TSocket>
NNet::TStructReader< T, TSocket >::TStructReader ( TSocket & socket)
inline

Constructs a reader with a reference to the given socket.

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

Member Function Documentation

◆ Read()

template<typename T, typename TSocket>
TFuture< T > NNet::TStructReader< T, TSocket >::Read ( )
inline

Reads a single instance of type T from the socket.

  • Reads exactly sizeof(T) bytes.
  • Continues to call ReadSome() until all needed bytes are read.
  • Throws a std::runtime_error if the socket closes (returns 0) before the structure is fully read.
  • Retries if ReadSome() returns a negative number (indicating a recoverable read error).
Returns
A TValueTask<T> that completes once the entire structure is read.
Exceptions
std::runtime_errorIf the connection is closed before the structure is fully read.

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