ucommon
Public Member Functions | Protected Member Functions | Protected Attributes
ost::TCPSocket Class Reference

TCP sockets are used for stream based connected sessions between two sockets. More...

#include <tcp.h>

Inherits ost::Socket.

Public Member Functions

IPV4Host getLocal (tpport_t *port=((void *) 0))
 Used to get local bound address.
IPV4Host getRequest (tpport_t *port=((void *) 0))
 Return address and port of next connection request.
int getSegmentSize (void)
 Get the buffer size for servers.
SOCKET getSocket (void)
 Fetch out the socket.
bool isPendingConnection (timeout_t timeout=ucommon::Timer::inf)
 Used to wait for pending connection requests.
virtual bool onAccept (IPV4Host &ia, tpport_t port)
 A method to call in a derived TCPSocket class that is acting as a server when a connection request is being accepted.
void reject (void)
 Used to reject the next incoming connection request.
 TCPSocket (IPV4Address &bind, tpport_t port, unsigned backlog=5, unsigned mss=536)
 A TCP "server" is created as a TCP socket that is bound to a hardware address and port number on the local machine and that has a backlog queue to listen for remote connection requests.
 TCPSocket (char *name, unsigned backlog=5, unsigned mss=536)
 Create a named tcp socket by service and/or interface id.
virtual ~TCPSocket ()
 Use base socket handler for ending this socket.

Protected Member Functions

void setSegmentSize (unsigned mss)
- Protected 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.
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 Attributes

int segsize
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

Additional Inherited Members

- Protected 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 Protected Member Functions inherited from ost::Socket
static bool check (Family fam)
 See if a specific protocol family is available in the current runtime environment.
static socket_t dupSocket (socket_t s, Socket::State state)

Detailed Description

TCP sockets are used for stream based connected sessions between two sockets.

Both error recovery and flow control operate transparently for a TCP socket connection. The TCP socket base class is primary used to bind a TCP "server" for accepting TCP streams.

An implicit and unique TCPSocket object exists in Common C++ to represent a bound TCP socket acting as a "server" for receiving connection requests. This class is not part of TCPStream because such objects normally perform no physical I/O (read or write operations) other than to specify a listen backlog queue and perform "accept" operations for pending connections. The Common C++ TCPSocket offers a Peek method to examine where the next pending connection is coming from, and a Reject method to flush the next request from the queue without having to create a session.

The TCPSocket also supports a "OnAccept" method which can be called when a TCPStream related object is created from a TCPSocket. By creating a TCPStream from a TCPSocket, an accept operation automatically occurs, and the TCPSocket can then still reject the client connection through the return status of it's OnAccept method.

Author
David Sugar dyfet.nosp@m.@tyc.nosp@m.ho.co.nosp@m.m bound server for TCP streams and sessions.

Definition at line 91 of file tcp.h.

Constructor & Destructor Documentation

ost::TCPSocket::TCPSocket ( IPV4Address bind,
tpport_t  port,
unsigned  backlog = 5,
unsigned  mss = 536 
)

A TCP "server" is created as a TCP socket that is bound to a hardware address and port number on the local machine and that has a backlog queue to listen for remote connection requests.

If the server cannot be created, an exception is thrown.

Parameters
bindlocal ip address or interface to use.
portnumber to bind socket under.
backlogsize of connection request queue.
mssmaximum segment size for accepted streams.
ost::TCPSocket::TCPSocket ( char *  name,
unsigned  backlog = 5,
unsigned  mss = 536 
)

Create a named tcp socket by service and/or interface id.

For IPV4 we use [host:]svc or [host/]svc for the string. If we have getaddrinfo, we use that to obtain the addr to bind for.

Parameters
nameof host interface and service port to bind.
backlogsize of connection request queue.
mssmaximum segment size for streaming buffers.

Member Function Documentation

IPV4Host ost::TCPSocket::getRequest ( tpport_t *  port = ((void*)0))
inline

Return address and port of next connection request.

This can be used instead of OnAccept() to pre-evaluate connection requests.

Returns
host requesting a connection.
Parameters
portnumber of requestor.

Definition at line 157 of file tcp.h.

bool ost::TCPSocket::isPendingConnection ( timeout_t  timeout = ucommon::Timer::inf)
inline

Used to wait for pending connection requests.

Returns
true if data packets available.
Parameters
timeoutin milliseconds. TIMEOUT_INF if not specified.

Definition at line 176 of file tcp.h.

virtual bool ost::TCPSocket::onAccept ( IPV4Host ia,
tpport_t  port 
)
virtual

A method to call in a derived TCPSocket class that is acting as a server when a connection request is being accepted.

The server can implement protocol specific rules to exclude the remote socket from being accepted by returning false. The Peek method can also be used for this purpose.

Returns
true if client should be accepted.
Parameters
iainternet host address of the client.
portnumber of the client.

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