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

A utility for writing data to a socket-like object. More...

#include <sockutils.hpp>

Public Member Functions

 TByteWriter (TSocket &socket)
 Constructs a writer for the given socket.
 
TFuture< void > Write (const void *data, size_t size)
 Writes exactly size bytes from data to the socket.
 
TFuture< void > Write (const TLine &line)
 Writes a TLine object by sequentially writing its parts.
 

Detailed Description

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

A utility for writing data to a socket-like object.

This class provides methods to write a specified number of bytes or to write a TLine object. If the underlying socket indicates it cannot make progress, the write operation is retried; if the socket is closed (write returns 0), an exception is thrown.

Template Parameters
TSocketThe socket type used for writing bytes. It must provide a method TValueTask<ssize_t> WriteSome(const void* data, size_t size) which returns:
  • 0 if the connection is closed,
  • a positive number for the count of bytes successfully written,
  • a negative number to indicate a recoverable write error (in which case a retry may be attempted).

Example Usage

TValueTask<void> ExampleFunction(TSocket& socket) {
TByteWriter<TSocket> writer(socket);
// Write a fixed amount of data
const char* message = "Hello, World!";
co_await writer.Write(message, std::strlen(message));
// Write a TLine object
TLine line{"PartA", "PartB"};
co_await writer.Write(line);
co_return;
}
High-level asynchronous socket for network communication.
Definition socket.hpp:364
TByteWriter(TSocket &socket)
Constructs a writer for the given socket.
Definition sockutils.hpp:195
Definition sockutils.hpp:9

Constructor & Destructor Documentation

◆ TByteWriter()

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

Constructs a writer for the given socket.

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

Member Function Documentation

◆ Write() [1/2]

template<typename TSocket>
TFuture< void > NNet::TByteWriter< TSocket >::Write ( const TLine & line)
inline

Writes a TLine object by sequentially writing its parts.

This method calls Write(const void*, size_t) for line.Part1 and then line.Part2.

Parameters
lineA structure holding the data to write in two parts.
Returns
A TValueTask<void> that completes once both parts have been fully written.
Exceptions
std::runtime_errorIf the connection is closed while writing.

◆ Write() [2/2]

template<typename TSocket>
TFuture< void > NNet::TByteWriter< TSocket >::Write ( const void * data,
size_t size )
inline

Writes exactly size bytes from data to the socket.

  • Calls the socket's WriteSome() in a loop until all requested bytes are written.
  • If WriteSome() returns 0, a std::runtime_error is thrown to indicate closure.
  • If WriteSome() returns a negative value, the write is retried.
Parameters
dataPointer to the buffer containing the bytes to write.
sizeThe number of bytes to write.
Returns
A TValueTask<void> that completes when all bytes have been written or an exception is thrown if the socket is closed.
Exceptions
std::runtime_errorIf the connection is closed before size bytes are fully written.

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