AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::network::TcpStream Class Reference

TCP connection as a std::iostream. More...

#include <tcpStream.hpp>

Inheritance diagram for anch::network::TcpStream:
Collaboration diagram for anch::network::TcpStream:

Public Member Functions

 TcpStream (const std::string &host, uint16_t port, std::size_t bufSize=DEFAULT_BUFFER_SIZE)
 TcpStream (anch::network::Socket &&socket, std::size_t bufSize=DEFAULT_BUFFER_SIZE)
 TcpStream (const TcpStream &)=delete
 TcpStream (TcpStream &&)=delete
virtual ~TcpStream () noexcept
void shutdown (anch::network::Direction how=anch::network::Direction::TRANSMISSION)
anch::network::Socket & getSocket () noexcept
Public Member Functions inherited from anch::cutils::CIOStream
 CIOStream (const anch::cutils::cbuffer &cbuffer)
virtual ~CIOStream ()

Static Public Attributes

static constexpr std::size_t DEFAULT_BUFFER_SIZE = 8192

Additional Inherited Members

Protected Member Functions inherited from anch::cutils::CIOStream
 CIOStream ()
void setBuffer (const anch::cutils::cbuffer &cbuffer)

Detailed Description

TCP connection as a std::iostream.

Written data are buffered: they are sent when the buffer is full, on flush (or std::endl ), before reading (request / response protocols do not need an explicit flush) and on destruction.
End of stream (eof ) is reached when the peer has closed the connection (or shut down its transmission). Network errors set badbit ; with exceptions(std::ios::badbit) , the IOException is raised.
TcpStream can not be copied nor moved: it is built from a host and a port (client) or from a connected Socket (TcpServer::accept ).

anch::network::TcpServer server("", 40099);
anch::network::TcpStream client("localhost", 40099);
anch::network::TcpStream peer = server.accept();
client << "hello" << std::endl;
std::string line;
std::getline(peer, line);
Author
Vincent Lachenal

Constructor & Destructor Documentation

◆ TcpStream() [1/4]

anch::network::TcpStream::TcpStream ( const std::string & host,
uint16_t port,
std::size_t bufSize = DEFAULT_BUFFER_SIZE )

TcpStream constructor: connect to remote host

Parameters
hostthe remote host name or numeric address (empty for loopback)
portthe remote port
bufSizethe buffer size
Exceptions
anch::network::IOExceptionconnection error

◆ TcpStream() [2/4]

anch::network::TcpStream::TcpStream ( anch::network::Socket && socket,
std::size_t bufSize = DEFAULT_BUFFER_SIZE )
explicit

TcpStream constructor: use a connected socket

Parameters
socketthe connected socket
bufSizethe buffer size

◆ TcpStream() [3/4]

anch::network::TcpStream::TcpStream ( const TcpStream & )
delete

Forbids TcpStream copy constructor

Here is the call graph for this function:

◆ TcpStream() [4/4]

anch::network::TcpStream::TcpStream ( TcpStream && )
delete

Forbids TcpStream move constructor (the stream buffer refers to the socket)

Here is the call graph for this function:

◆ ~TcpStream()

virtual anch::network::TcpStream::~TcpStream ( )
virtualnoexcept

TcpStream destructor: send pending data and close the connection

Member Function Documentation

◆ getSocket()

anch::network::Socket & anch::network::TcpStream::getSocket ( )
noexcept

Socket getter (for options, addresses, ...)

Returns
the socket
Here is the call graph for this function:

◆ shutdown()

void anch::network::TcpStream::shutdown ( anch::network::Direction how = anch::network::Direction::TRANSMISSION)

Shut down part of the connection. Pending data are sent before shutting down transmission: the peer reaches the end of stream once it has read them, and this stream can still read the response.

Parameters
howthe direction to shut down
Exceptions
anch::network::IOExceptionerror on shutdown
Here is the call graph for this function:

Member Data Documentation

◆ DEFAULT_BUFFER_SIZE

std::size_t anch::network::TcpStream::DEFAULT_BUFFER_SIZE = 8192
staticconstexpr

Default buffer size (for reading and for writing)


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