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.
void connect (TCPV6Socket &server)
void disconnect (void)
 Disconnect the current session and prepare for a new one.
size_t getBufferSize (void)
 Return the size of the current stream buffering used.
int getSegmentSize (void)
 Get protocol segment size.
bool isPending (Pending pend, timeout_t timeout=ucommon::Timer::inf)
 Get the status of pending stream data.
ssize_t peek (void *buf, size_t len)
 Examine contents of next waiting packet.
size_t printf (char *format,...)
 Print content into a socket.
void setTimeout (timeout_t timer)
 Set the I/O operation timeout for socket I/O operations.
int sync (void)
 Flushes the stream input and output buffers, writes pending output.
 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.
 TCPStream (TCPV6Socket &server, bool throwflag=true, timeout_t timeout=0)
 TCPStream (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).
 TCPStream (IPV6Host &host, tpport_t port, unsigned mss=536, bool throwflag=true, timeout_t timeout=0)
 TCPStream (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.
virtual ~TCPStream ()
 Flush and empty all buffers, and then remove the allocated buffers.
- Public Member Functions inherited from ost::Socket
struct in_addr getaddress (IPV4Address &ia)
struct in6_addr getaddress (IPV6Address &ia)
Error getErrorNumber (void)
 Often used by a "catch" to fetch the last error of a thrown socket.
char * getErrorString (void)
 Often used by a "catch" to fetch the user set error string of a thrown socket, but only if EXTENDED error codes are used.
IPV4Host getIPV4Local (tpport_t *port=((void *) 0))
 Get the local address and port number this socket is currently bound to.
IPV4Host getIPV4Peer (tpport_t *port=((void *) 0))
 Get the host address and port of the socket this socket is connected to.
virtual IPV4Host getIPV4Sender (tpport_t *port=((void *) 0))
 May be used to examine the origin of data waiting in the socket receive queue.
IPV6Host getIPV6Local (tpport_t *port=((void *) 0))
IPV6Host getIPV6Peer (tpport_t *port=((void *) 0))
virtual IPV6Host getIPV6Sender (tpport_t *port=((void *) 0))
IPV4Host getLocal (tpport_t *port=((void *) 0))
IPV4Host getPeer (tpport_t *port=((void *) 0))
IPV4Host getSender (tpport_t *port=((void *) 0))
long getSystemError (void)
char * getSystemErrorString (void)
bool isActive (void)
 Test to see if the socket is at least operating or if it is mearly initialized.
bool isBroadcast (void)
 Return if broadcast has been enabled for the specified socket.
bool isConnected (void)
 Can test to see if this socket is "connected", and hence whether a "catch" can safely call getPeer().
virtual bool isPending (Pending pend, timeout_t timeout=ucommon::Timer::inf)
 Get the status of pending operations.
bool isRouted (void)
 Return if socket routing is enabled.
 operator bool ()
bool operator! ()
 Operator based testing to see if a socket is currently active.
Socket & operator= (Socket &from)
 Sockets may also be duplicated by the assignment operator.
void setCompletion (bool immediate)
 Used to specify blocking mode for the socket.
Error setKeepAlive (bool enable)
 Set the keep-alive status of this socket and if keep-alive messages will be sent.
Error setLinger (bool linger)
 Enable lingering sockets on close.
Error setTypeOfService (Tos service)
 Set packet scheduling on platforms which support ip quality of service conventions.

Protected Member Functions

void allocate (size_t size)
 Used to allocate the buffer space needed for iostream operations.
void connect (IPV4Host &host, tpport_t port, unsigned mss=536)
 Create a TCP stream by connecting to a TCP socket (on a remote machine).
void connect (IPV6Host &host, tpport_t port, unsigned mss=536)
void connect (char *name, unsigned mss=536)
 Connect a TCP stream to a named destination host and port number, using getaddrinfo interface if available.
void endStream (void)
 Used to terminate the buffer space and cleanup the socket connection.
int overflow (int ch)
 This streambuf method is used to write the output buffer through the established tcp connection.
std::iostream * tcp (void)
 Used in derived classes to refer to the current object via it's iostream.
int uflow ()
 This streambuf method is used for doing unbuffered reads through the establish tcp socket connection when in interactive mode.
int underflow ()
 This streambuf method is used to load the input buffer through the established tcp socket connection.
- Protected Member Functions inherited from ost::Socket
Error bufferSize (unsigned size)
 Set the total protocol stack network kernel buffer size for both send and receive together.
Error connectError (void)
 Used as a common handler for connection failure processing.
Error drop (IPV4Multicast &ia)
 Drop membership from a multicast group.
Error drop (IPV6Multicast &ia)
void endSocket (void)
 Used as the default destructor for ending a socket.
Error error (Error error, char *err=((void *) 0), long systemError=0)
 This service is used to throw all socket errors which usually occur during the socket constructor.
void error (char *err)
 This service is used to throw application defined socket errors where the application specific error code is a string.
Error join (IPV4Multicast &ia)
 Join a multicast group.
Error join (IPV6Multicast &ia)
virtual ssize_t readData (void *buf, size_t len, char separator=0, timeout_t t=0)
 Read in a block of len bytes with specific separator.
ssize_t readLine (char *buf, size_t len, timeout_t timeout=0)
 Process a logical input line from a socket descriptor directly.
Error receiveBuffer (unsigned size)
 Set the protocol stack network kernel receive buffer size associated with the socket.
Error receiveLimit (int limit=1)
 Set thr receive limit.
Error receiveTimeout (timeout_t timer)
 Receive timeout for receiving raw network data.
Error sendBuffer (unsigned size)
 Set the protocol stack network kernel send buffer size associated with the socket.
Error sendLimit (int limit=2048)
 Set the send limit.
Error sendTimeout (timeout_t timer)
 Set the send timeout for sending raw network data.
Error setBroadcast (bool enable)
 Set the subnet broadcast flag for the socket.
void setError (bool enable)
 This service is used to turn the error handler on or off for "throwing" exceptions by manipulating the thrown flag.
Error setLoopbackByFamily (bool enable, Family family=IPV4)
 Set the multicast loopback flag for the socket.
Error setMulticastByFamily (bool enable, Family family=IPV4)
 Setting multicast binds the multicast interface used for the socket to the interface the socket itself has been implicitly bound to.
Error setNoDelay (bool enable)
 Enable/disable delaying packets (Nagle algorithm)
Error setRouting (bool enable)
 Set the socket routing to indicate if outgoing messages should bypass normal routing (set false).
Error setTimeToLiveByFamily (unsigned char ttl, Family fam=IPV4)
 Set the multicast time to live for a multicast socket.
 Socket (int domain, int type, int protocol=0)
 An unconnected socket may be created directly on the local machine.
 Socket (socket_t fd)
 A socket object may be created from a file descriptor when that descriptor was created either through a socket() or accept() call.
 Socket ()
 Create an inactive socket object for base constructors.
 Socket (Socket &source)
 A socket can also be constructed from an already existing Socket object.
virtual ssize_t writeData (void *buf, size_t len, timeout_t t=0)
 Write a block of len bytes to socket.
- Protected Member Functions inherited from ucommon::Socket
int blocking (bool enable)
 Set socket blocking I/O mode.
int broadcast (bool enable)
 Set socket for unicast mode broadcasts.
void cancel (void)
 Cancel pending i/o by shutting down the socket.
int connectto (struct addrinfo *list)
 Connect our socket to a remote host from an address list.
int drop (struct addrinfo *list)
 Drop socket from multicast group.
int err (void)
 Get error code.
int getError (void)
 Get socket error code.
unsigned getPending (void)
 Get the number of bytes of data in the socket receive buffer.
int gettype (void)
 Get the type of a socket.
bool isConnected (void)
 Test if socket is connected.
bool isPending (unsigned value)
 See the number of bytes in the receive queue.
int join (struct addrinfo *list)
 Join socket to multicast group.
int keepalive (bool enable)
 Set socket for keepalive packets.
int loopback (bool enable)
 Set loopback to read multicast packets we broadcast.
int multicast (unsigned ttl=1)
 Set multicast mode and multicast broadcast range.
int nodelay (void)
 Set nodelay option for tcp socket.
 operator bool ()
 Test if socket is valid.
 operator socket_t ()
 Get the socket descriptor by casting.
bool operator! ()
 Test if socket is invalid.
socket_t operator* ()
 Get the socket descriptor by pointer reference.
Socketoperator= (socket_t socket)
 Assign socket from a socket descriptor.
int priority (int scheduling)
 Set packet priority, 0 to 6 unless privileged.
size_t readfrom (void *data, size_t number, struct sockaddr_storage *address=((void *) 0))
 Read data from the socket receive buffer.
size_t readline (char *data, size_t size)
 Read a newline of text data from the socket and save in NULL terminated string.
size_t readline (string &buffer)
 Read a string of input from the socket and strip trailing newline.
int recvsize (unsigned size)
 Set the size of the socket receive buffer.
void release (void)
 Shutdown and close the socket.
unsigned segsize (unsigned size)
 Set segment size and get mtu of a socket.
int sendsize (unsigned size)
 Set the size of the socket send buffer.
int sendwait (unsigned size)
 Set the size to wait before sending.
bool setccid (uint8_t ccid)
 Set ccid of dccp socket.
void shutdown (void)
 Shutdown the socket communication channel.
 Socket ()
 Create a socket object for use.
 Socket (Socket &existing)
 Create socket as duped handle of existing socket.
 Socket (socket_t socket)
 Create socket from existing socket descriptor.
 Socket (struct addrinfo *address)
 Create and connect a socket to an address from an address list.
 Socket (int family, int type, int protocol=0)
 Create an unbound socket of a specific type.
 Socket (char *address, char *port, int family=0, int type=0, int protocol=0)
 Create a bound socket.
int tos (int type)
 Set the type of service field of outgoing packets.
int ttl (unsigned char time)
 Set the time to live before packets expire.
int wait (timeout_t timeout=Timer::inf)
 Socket i/o timer setting.
bool waitPending (timeout_t timeout=0)
 Test for pending input data.
bool waitSending (timeout_t timeout=0)
 Test for output data sent.
size_t writes (char *string)
 Write a null terminated string to the socket.
size_t writeto (void *data, size_t number, struct sockaddr *address=((void *) 0))
 Write data to the socket send buffer.
virtual ~Socket ()
 Shutdown, close, and destroy socket.

Protected Attributes

size_t bufsize
Family family
char * gbuf
char * pbuf
timeout_t timeout
- Protected Attributes inherited from ost::Socket
struct {
   bool   broadcast: 1
   bool   completion: 1
   bool   keepalive: 1
   bool   linger: 1
   bool   loopback: 1
   bool   multicast: 1
   bool   route: 1
   bool   thrown: 1
   unsigned   ttl: 8
flags
State volatile state

Friends

TCPStreamcrlf (TCPStream &)
TCPStreamlfcr (TCPStream &)

Additional Inherited Members

- Public Types inherited from ost::Socket
enum  Error {
  errSuccess = 0, errCreateFailed, errCopyFailed, errInput,
  errInputInterrupt, errResourceFailure, errOutput, errOutputInterrupt,
  errNotConnected, errConnectRefused, errConnectRejected, errConnectTimeout,
  errConnectFailed, errConnectInvalid, errConnectBusy, errConnectNoRoute,
  errBindingFailed, errBroadcastDenied, errRoutingDenied, errKeepaliveDenied,
  errServiceDenied, errServiceUnavailable, errMulticastDisabled, errTimeout,
  errNoDelay, errExtended, errLookupFail, errSearchErr,
  errInvalidValue
}
typedef enum Error Error
enum  Family { IPV6 = 1, IPV4 = 2 }
typedef enum Family Family
enum  Pending { pendingInput, pendingOutput, pendingError }
typedef enum Pending Pending
enum  State {
  INITIAL, AVAILABLE, BOUND, CONNECTED,
  CONNECTING, STREAM
}
typedef enum State State
enum  Tos {
  tosLowDelay = 0, tosThroughput, tosReliability, tosMinCost,
  tosInvalid
}
typedef enum Tos Tos
- Static Public Member Functions inherited from ost::Socket
static bool check (Family fam)
 See if a specific protocol family is available in the current runtime environment.
- Protected Types inherited from ucommon::Socket
typedef void * set_t
 External definition of fd_set type.
- Static Protected Member Functions inherited from ost::Socket
static socket_t dupSocket (socket_t s, Socket::State state)
- Static Protected Attributes inherited from ost::Socket
static Mutex mutex

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 315 of file tcp.h.

Constructor & Destructor Documentation

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.
ost::TCPStream::TCPStream ( 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.
ost::TCPStream::TCPStream ( 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

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.
void ost::TCPStream::connect ( 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.
void ost::TCPStream::connect ( 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
void ost::TCPStream::connect ( TCPSocket server)

Accept a connection from a TCP Server.

Parameters
serversocket listening
void ost::TCPStream::endStream ( void  )
protected

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

This fucntion is called by the destructor.

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

Return the size of the current stream buffering used.

Returns
size of stream buffers.

Definition at line 532 of file tcp.h.

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.

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.
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.

Reimplemented from ucommon::Socket.

Definition at line 524 of file tcp.h.

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

Print content into a socket.

Returns
count of bytes sent.
Parameters
formatstring

Reimplemented from ucommon::Socket.

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 482 of file tcp.h.

int ost::TCPStream::sync ( void  )

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

Returns
0 on success.
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 423 of file tcp.h.

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.
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: