UCommon
Public Member Functions | Protected Member Functions | Protected Attributes | Friends
ost::TCPStream Class Reference

TCP streams are used to represent TCP client connections to a server by TCP protocol servers for accepting client connections. More...

#include <tcp.h>

Inheritance diagram for ost::TCPStream:
Inheritance graph
[legend]

Public Member Functions

void connect (TCPSocket &server)
 Accept a connection from a TCP Server. More...
 
void connect (TCPV6Socket &server)
 
void disconnect (void)
 Disconnect the current session and prepare for a new one.
 
size_t getBufferSize (void) const
 Return the size of the current stream buffering used. More...
 
int getSegmentSize (void)
 Get protocol segment size.
 
bool isPending (Pending pend, timeout_t timeout=ucommon::Timer::inf)
 Get the status of pending stream data. More...
 
ssize_t peek (void *buf, size_t len)
 Examine contents of next waiting packet. More...
 
size_t printf (const char *format,...)
 Print content into a socket. More...
 
void setTimeout (timeout_t timer)
 Set the I/O operation timeout for socket I/O operations. More...
 
int sync (void)
 Flushes the stream input and output buffers, writes pending output. More...
 
 TCPStream (Family family=IPV4, bool throwflag=true, timeout_t to=0)
 The constructor required for building other classes or to start an unconnected TCPStream for connect.
 
 TCPStream (TCPSocket &server, bool throwflag=true, timeout_t timeout=0)
 Create a TCP stream by accepting a connection from a bound TCP socket acting as a server. More...
 
 TCPStream (TCPV6Socket &server, bool throwflag=true, timeout_t timeout=0)
 
 TCPStream (const IPV4Host &host, tpport_t port, unsigned mss=536, bool throwflag=true, timeout_t timeout=0)
 Create a TCP stream by connecting to a TCP socket (on a remote machine). More...
 
 TCPStream (const IPV6Host &host, tpport_t port, unsigned mss=536, bool throwflag=true, timeout_t timeout=0)
 
 TCPStream (const char *name, Family family=IPV4, unsigned mss=536, bool throwflag=false, timeout_t timer=0)
 Construct a named TCP Socket connected to a remote machine. More...
 
virtual ~TCPStream ()
 Flush and empty all buffers, and then remove the allocated buffers.
 

Protected Member Functions

void allocate (size_t size)
 Used to allocate the buffer space needed for iostream operations. More...
 
void connect (const IPV4Host &host, tpport_t port, unsigned mss=536)
 Create a TCP stream by connecting to a TCP socket (on a remote machine). More...
 
void connect (const IPV6Host &host, tpport_t port, unsigned mss=536)
 
void connect (const char *name, unsigned mss=536)
 Connect a TCP stream to a named destination host and port number, using getaddrinfo interface if available. More...
 
void endStream (void)
 Used to terminate the buffer space and cleanup the socket connection. More...
 
int overflow (int ch)
 This streambuf method is used to write the output buffer through the established tcp connection. More...
 
std::iostream * tcp (void)
 Used in derived classes to refer to the current object via it's iostream. More...
 
int uflow ()
 This streambuf method is used for doing unbuffered reads through the establish tcp socket connection when in interactive mode. More...
 
int underflow ()
 This streambuf method is used to load the input buffer through the established tcp socket connection. More...
 

Protected Attributes

size_t bufsize
 
Family family
 
char * gbuf
 
char * pbuf
 
timeout_t timeout
 

Friends

TCPStreamcrlf (TCPStream &)
 
TCPStreamlfcr (TCPStream &)
 

Detailed Description

TCP streams are used to represent TCP client connections to a server by TCP protocol servers for accepting client connections.

The TCP stream is a C++ "stream" class, and can accept streaming of data to and from other C++ objects using the << and >> operators.

TCPStream itself can be formed either by connecting to a bound network address of a TCP server, or can be created when "accepting" a network connection from a TCP server.

Author
David Sugar dyfet.nosp@m.@ost.nosp@m.el.co.nosp@m.m streamable TCP socket connection.

Definition at line 329 of file tcp.h.

Constructor & Destructor Documentation

◆ TCPStream() [1/3]

ost::TCPStream::TCPStream ( TCPSocket server,
bool  throwflag = true,
timeout_t  timeout = 0 
)

Create a TCP stream by accepting a connection from a bound TCP socket acting as a server.

This performs an "accept" call.

Parameters
serversocket listening
throwflagflag to throw errors.
timeoutfor all operations.

◆ TCPStream() [2/3]

ost::TCPStream::TCPStream ( const IPV4Host host,
tpport_t  port,
unsigned  mss = 536,
bool  throwflag = true,
timeout_t  timeout = 0 
)

Create a TCP stream by connecting to a TCP socket (on a remote machine).

Parameters
hostaddress of remote TCP server.
portnumber to connect.
mssmaximum segment size of streaming buffers.
throwflagflag to throw errors.
timeoutfor all operations.

◆ TCPStream() [3/3]

ost::TCPStream::TCPStream ( const char *  name,
Family  family = IPV4,
unsigned  mss = 536,
bool  throwflag = false,
timeout_t  timer = 0 
)

Construct a named TCP Socket connected to a remote machine.

Parameters
nameof remote service.
familyof protocol.
mssmaximum segment size of streaming buffers.
throwflagflag to throw errors.
timerfor timeout for all operations.

Member Function Documentation

◆ allocate()

void ost::TCPStream::allocate ( size_t  size)
protected

Used to allocate the buffer space needed for iostream operations.

This function is called by the constructor.

Parameters
sizeof stream buffers from constructor.

◆ connect() [1/3]

void ost::TCPStream::connect ( const IPV4Host host,
tpport_t  port,
unsigned  mss = 536 
)
protected

Create a TCP stream by connecting to a TCP socket (on a remote machine).

Parameters
hostaddress of remote TCP server.
portnumber to connect.
mssmaximum segment size of streaming buffers.

◆ connect() [2/3]

void ost::TCPStream::connect ( const char *  name,
unsigned  mss = 536 
)
protected

Connect a TCP stream to a named destination host and port number, using getaddrinfo interface if available.

Parameters
nameof host and service to connect
mssmaximum segment size of stream buffer

◆ connect() [3/3]

void ost::TCPStream::connect ( TCPSocket server)

Accept a connection from a TCP Server.

Parameters
serversocket listening

◆ endStream()

void ost::TCPStream::endStream ( void  )
protected

Used to terminate the buffer space and cleanup the socket connection.

This fucntion is called by the destructor.

◆ getBufferSize()

size_t ost::TCPStream::getBufferSize ( void  ) const
inline

Return the size of the current stream buffering used.

Returns
size of stream buffers.

Definition at line 549 of file tcp.h.

◆ isPending()

bool ost::TCPStream::isPending ( Pending  pend,
timeout_t  timeout = ucommon::Timer::inf 
)

Get the status of pending stream data.

This can be used to examine if input or output is waiting, or if an error or disconnect has occured on the stream. If a read buffer contains data then input is ready and if write buffer contains data it is first flushed and then checked.

◆ overflow()

int ost::TCPStream::overflow ( int  ch)
protected

This streambuf method is used to write the output buffer through the established tcp connection.

Parameters
chchar to push through.
Returns
char pushed through.

◆ peek()

ssize_t ost::TCPStream::peek ( void *  buf,
size_t  len 
)
inline

Examine contents of next waiting packet.

Parameters
bufpointer to packet buffer for contents.
lenof packet buffer.
Returns
number of bytes examined.

Definition at line 540 of file tcp.h.

◆ printf()

size_t ost::TCPStream::printf ( const char *  format,
  ... 
)

Print content into a socket.

Returns
count of bytes sent.
Parameters
formatstring

◆ setTimeout()

void ost::TCPStream::setTimeout ( timeout_t  timer)
inline

Set the I/O operation timeout for socket I/O operations.

Parameters
timerto change timeout.

Definition at line 497 of file tcp.h.

◆ sync()

int ost::TCPStream::sync ( void  )

Flushes the stream input and output buffers, writes pending output.

Returns
0 on success.

◆ tcp()

std::iostream* ost::TCPStream::tcp ( void  )
inlineprotected

Used in derived classes to refer to the current object via it's iostream.

For example, to send a set of characters in a derived method, one might use *tcp() << "test".

Returns
stream pointer of this object.

Definition at line 437 of file tcp.h.

◆ uflow()

int ost::TCPStream::uflow ( )
protected

This streambuf method is used for doing unbuffered reads through the establish tcp socket connection when in interactive mode.

Also this method will handle proper use of buffers if not in interative mode.

Returns
char from tcp socket connection, EOF if not connected.

◆ underflow()

int ost::TCPStream::underflow ( )
protected

This streambuf method is used to load the input buffer through the established tcp socket connection.

Returns
char from get buffer, EOF if not connected.

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