RTP Audio System  2.0.0
Socket Class Reference

Socket. More...

#include <tdsocket.h>

List of all members.

Public Types

enum  SocketFamily {
  UndefinedSocketFamily = -1, IP = 255, IPv4 = AF_INET, IPv6 = AF_INET6,
  Unix = AF_UNIX
}
enum  SocketType {
  UndefinedSocketType = -1, UDP = SOCK_DGRAM, Datagram = SOCK_DGRAM, TCP = SOCK_STREAM,
  Stream = SOCK_STREAM, Raw = SOCK_RAW, RDM = SOCK_RDM, SeqPacket = SOCK_SEQPACKET
}
enum  SocketProtocol {
  UndefinedSocketProtocol = -1, Default = 0, ICMPv4 = IPPROTO_ICMP, ICMPv6 = IPPROTO_ICMPV6,
  SCTP = IPPROTO_SCTP
}
enum  GetSocketAddressFlags {
  GLAF_HideLoopback = (1 << 0), GLAF_HideLinkLocal = (1 << 1), GLAF_HideSiteLocal = (1 << 2), GLAF_HideLocal = GLAF_HideLoopback|GLAF_HideLinkLocal|GLAF_HideSiteLocal,
  GLAF_HideAnycast = (1 << 3), GLAF_HideMulticast = (1 << 4), GLAF_HideBroadcast = (1 << 5), GLAF_HideReserved = (1 << 6),
  GLAF_Default = GLAF_HideLoopback|GLAF_HideLinkLocal|Socket::GLAF_HideSiteLocal|GLAF_HideBroadcast|GLAF_HideMulticast|GLAF_HideAnycast
}

Public Member Functions

 Socket ()
 Socket (const integer family, const integer socketType, const integer socketProtocol=Default)
 ~Socket ()
bool create (const integer socketFamily=IP, const integer socketType=TCP, const integer socketProtocol=Default)
void close ()
void shutdown (const cardinal shutdownLevel)
integer getFamily () const
integer getType () const
integer getProtocol () const
bool ready () const
bool bind (const SocketAddress &address=InternetAddress())
bool bindx (const SocketAddress **addressArray=NULL, const cardinal addresses=0, const integer flags=0)
bool listen (const cardinal backlog=5)
Socketaccept (SocketAddress **address=NULL)
bool connect (const SocketAddress &address, const card8 trafficClass=0)
bool connectx (const SocketAddress **addressArray, const size_t addresses)
integer getLastError ()
integer getSocketOption (const cardinal level, const cardinal optionNumber, void *optionValue, socklen_t *optionLength)
cardinal getSoLinger ()
bool getSoReuseAddress ()
bool getSoBroadcast ()
bool getTCPNoDelay ()
bool getBlockingMode ()
integer setSocketOption (const cardinal level, const cardinal optionNumber, const void *optionValue, const socklen_t optionLength)
bool setSoLinger (const bool on, const cardinal linger)
bool setSoReuseAddress (const bool on)
bool setSoBroadcast (const bool on)
bool setTCPNoDelay (const bool on)
bool setBlockingMode (const bool on)
card32 getSendFlowLabel () const
card8 getSendTrafficClass () const
card32 getReceivedFlowLabel () const
card8 getReceivedTrafficClass () const
ssize_t send (const void *buffer, const size_t length, const integer flags=0, const card8 trafficClass=0x00)
ssize_t sendTo (const void *buffer, const size_t length, const integer flags, const SocketAddress &receiver, const card8 trafficClass=0x00)
ssize_t sendMsg (const struct msghdr *msg, const integer flags, const card8 trafficClass=0x00)
ssize_t write (const void *buffer, const size_t length)
ssize_t receive (void *buffer, const size_t length, integer &flags)
ssize_t receiveFrom (void *buffer, const size_t length, SocketAddress &sender, integer &flags)
ssize_t receiveMsg (struct msghdr *msg, const integer flags, const bool internalCall=false)
ssize_t read (void *buffer, const size_t length)
integer fcntl (const integer cmd, long arg)
integer fcntl (const integer cmd, struct flock *lock)
integer ioctl (const integer request, const void *argp)
bool getSocketAddress (SocketAddress &address) const
bool getPeerAddress (SocketAddress &address) const
bool addMulticastMembership (const SocketAddress &address, const char *interface=NULL)
bool dropMulticastMembership (const SocketAddress &address, const char *interface=NULL)
bool getMulticastLoop ()
bool setMulticastLoop (const bool on)
card8 getMulticastTTL ()
bool setMulticastTTL (const card8 ttl)
InternetFlow allocFlow (const InternetAddress &address, const card32 flowLabel=0, const card8 shareLevel=2)
void freeFlow (InternetFlow &flow)
bool renewFlow (InternetFlow &flow, const cardinal expires, const cardinal linger=6)
bool renewFlow (const cardinal expires, const cardinal linger=6)
int getSystemSocketDescriptor () const

Static Public Member Functions

static bool bindSocketPair (Socket &socket1, Socket &socket2, const SocketAddress &address=InternetAddress())
static bool bindxSocketPair (Socket &socket1, Socket &socket2, const SocketAddress **addressArray=NULL, const cardinal addresses=0, const integer flags=0)
static bool getLocalAddressList (SocketAddress **&addressList, cardinal &numberOfNets, const cardinal flags=GLAF_Default)

Static Public Attributes

static const cardinal MinAutoSelectPort = 16384
static const cardinal MaxAutoSelectPort = 61000

Private Member Functions

void init ()
bool setTypeOfService (const card8 trafficClass)
ssize_t recvFrom (int fd, void *buf, const size_t len, integer &flags, struct sockaddr *addr, socklen_t *addrlen)
bool multicastMembership (const SocketAddress &address, const char *interface, const bool add)
void packSocketAddressArray (const sockaddr_storage *addrArray, const size_t addrs, sockaddr *packedArray)

Private Attributes

int SocketDescriptor
integer Family
integer Type
integer Protocol
card32 SendFlow
card32 ReceivedFlow
integer LastError
cardinal Backlog
sockaddr * Destination

Friends

class TrafficShaper

Detailed Description

Socket.

This class manages a socket. IPv6 support is automatically available, when supported by the system.

Author:
Thomas Dreibholz
Version:
1.0

Member Enumeration Documentation

Enumerator:
GLAF_HideLoopback 
GLAF_HideLinkLocal 
GLAF_HideSiteLocal 
GLAF_HideLocal 
GLAF_HideAnycast 
GLAF_HideMulticast 
GLAF_HideBroadcast 
GLAF_HideReserved 
GLAF_Default 
Enumerator:
UndefinedSocketFamily 
IP 
IPv4 
IPv6 
Unix 
Enumerator:
UndefinedSocketProtocol 
Default 
ICMPv4 
ICMPv6 
SCTP 
Enumerator:
UndefinedSocketType 
UDP 
Datagram 
TCP 
Stream 
Raw 
RDM 
SeqPacket 

Constructor & Destructor Documentation

Constructor.

Socket::Socket ( const integer  family,
const integer  socketType,
const integer  socketProtocol = Default 
)

Constructor for a new socket. For automatic usage of IPv6 when available, set communication family to IP. Use IPv4/IPv6 only if a special protocol version is necessary! The creation success can be checked using ready() method.

Parameters:
familyCommunication family (e.g. IP).
socketTypeSocket type (e.g. TCP, UDP).
socketProtocolSocket protocol (e.g. Default).
See also:
ready

Destructor.


Member Function Documentation

Socket * Socket::accept ( SocketAddress **  address = NULL)

Accept a connection.

Parameters:
addressReference to store SocketAddress object to with peer's address to (NULL to skip).
Returns:
New socket.
bool Socket::addMulticastMembership ( const SocketAddress address,
const char *  interface = NULL 
) [inline]

Add multicast membership.

Parameters:
addressMulticast address.
interfaceInterface name.
Returns:
true for success; false otherwise.
InternetFlow Socket::allocFlow ( const InternetAddress address,
const card32  flowLabel = 0,
const card8  shareLevel = 2 
)

Allocate a new flow to a given destination. A InternetFlow object is returned, the value flow.getFlowLabel() will not be 0, if the allocFlow() call was successful.

Parameters:
addressAddress of the destination.
flowLabelFlowlabel; 0 for random value.
shareLevelShare level for flow label.
Returns:
InternetFlow.
bool Socket::bind ( const SocketAddress address = InternetAddress())

Bind socket to given address. If address is null address, then INADDR_ANY and an automatically selected port will be used.

Parameters:
addressSocket address.
Returns:
true on success; false otherwise.
bool Socket::bindSocketPair ( Socket socket1,
Socket socket2,
const SocketAddress address = InternetAddress() 
) [static]

Bind a pair of sockets to a given address and port number x and x + 1. x will be a random number, if given port number is 0.

Parameters:
socket1First socket.
socket2Second socket.
addressAddress (e.g ipv6-localhost:0) or NULL for Any address.
See also:
bind
bool Socket::bindx ( const SocketAddress **  addressArray = NULL,
const cardinal  addresses = 0,
const integer  flags = 0 
)

O * Bind socket to one or more given addresses. If no addresses are given, INADDR_ANY and an automatically selected port will be used.

Parameters:
addressArrayArray of socket addresses.
addressesNumber of addresses.
flagsFlags.
Returns:
true on success; false otherwise.
bool Socket::bindxSocketPair ( Socket socket1,
Socket socket2,
const SocketAddress **  addressArray = NULL,
const cardinal  addresses = 0,
const integer  flags = 0 
) [static]

Bind a pair of sockets to a given address and port number x and x + 1. x will be a random number, if given port number is 0. If no addresses are given, INADDR_ANY and an automatically selected port will be used.

Parameters:
socket1First socket.
socket2Second socket.
addressArrayArray of socket addresses.
addressesNumber of addresses.
flagsbindx() flags.
See also:
bindx
void Socket::close ( )

Close socket.

bool Socket::connect ( const SocketAddress address,
const card8  trafficClass = 0 
)

Connect socket to given address. A value for traffic class is supported if the connection is an IPv6 connection; otherwise it is ignored.

Parameters:
addressAddress.
trafficClassTraffic class of the connection (IPv6 only!)
Returns:
true on success; false otherwise.
bool Socket::connectx ( const SocketAddress **  addressArray,
const size_t  addresses 
)

Connect socket to destination given by list of addresses. A value for traffic class is supported if the connection is an IPv6 connection; otherwise it is ignored.

Parameters:
addressAddress.
trafficClassTraffic class of the connection (IPv6 only!)
Returns:
true on success; false otherwise.
bool Socket::create ( const integer  socketFamily = IP,
const integer  socketType = TCP,
const integer  socketProtocol = Default 
)

Close existing socket and create new socket. For automatic usage of IPv6 when available, set communication family to IP. Use IPv4/IPv6 only if a special protocol version is necessary!

Parameters:
socketFamilyCommunication family (e.g. IP).
socketTypeSocket type (e.g. TCP, UDP).
socketProtocolSocket protocol (e.g. Default).
Returns:
true, if creation was sucessful; false otherwise.
bool Socket::dropMulticastMembership ( const SocketAddress address,
const char *  interface = NULL 
) [inline]

Drop multicast membership.

Parameters:
addressMulticast address.
interfaceInterface name.
Returns:
true for success; false otherwise.
integer Socket::fcntl ( const integer  cmd,
long  arg 
) [inline]

Wrapper for fcntl().

Parameters:
cmdCommand.
argArgument.
Returns:
Result of fcntl() call.
integer Socket::fcntl ( const integer  cmd,
struct flock *  lock 
) [inline]

Wrapper for fcntl().

Parameters:
cmdCommand.
lockLock.
Returns:
Result of fcntl() call.
void Socket::freeFlow ( InternetFlow flow)

Free a flow.

Parameters:
flowFlow to be freed.

Check, if blocking mode is on.

Returns:
true, if blocking mode is on; false otherwise.
integer Socket::getFamily ( ) const [inline]

Get socket's family.

Returns:
Socket family.

Get last error code. It will be reset to 0 after copying.

Returns:
Last error code.
bool Socket::getLocalAddressList ( SocketAddress **&  addressList,
cardinal numberOfNets,
const cardinal  flags = GLAF_Default 
) [static]

Get list of all local addresses (IPv4 and IPv6 are currently supported). The resulting list has to be deallocated using SocketAddress::deleteAddressList().

Parameters:
addressListReference to store address list to.
numberOfNetsReference to store number of addresses to.
flagsFlags.
Returns:
true for success; false otherwise.
See also:
SocketAddress::deleteAddressList

Get multicast loop mode.

Returns:
true if multicast loop is enabled, false otherwise.

Get multicast TTL.

Returns:
Multicast TTL.
bool Socket::getPeerAddress ( SocketAddress address) const

Get the peer's address. Note: A socket has to be connected to a peer first to get a peer address!

Parameters:
addressReference to SocketAddress to write address to.
Returns:
true, if call was successful; false otherwise.
See also:
bind
connect
getSocketAddress
integer Socket::getProtocol ( ) const [inline]

Get socket's protocol.

Returns:
Socket protocol.

Get last received flow label.

Returns:
Last received flow label or 0, if there is no flow label.

Get last received traffic class.

Returns:
Last received traffic class or 0, if there is no traffic class.
card32 Socket::getSendFlowLabel ( ) const [inline]

Get flow label of the connection.

Returns:
Flow label of the connection or 0, if there is no flow label.
See also:
connect
card8 Socket::getSendTrafficClass ( ) const [inline]

Get traffic class of the connection.

Returns:
Traffic class of the connection or 0, if there is no traffic class.
See also:
connect

Get SO_BROADCAST option of socket.

Returns:
SO_BROADCAST value.
bool Socket::getSocketAddress ( SocketAddress address) const

Get the socket's address. Note: A socket has to be bound to an address and port or connected to a peer first to let the socket have an address!

Parameters:
addressReference to SocketAddress to write address to.
Returns:
true, if call was successful; false otherwise.
See also:
bind
connect
getPeerAddress
integer Socket::getSocketOption ( const cardinal  level,
const cardinal  optionNumber,
void *  optionValue,
socklen_t *  optionLength 
) [inline]

Get socket option (wrapper for getsockopt());

Parameters:
levelLevel (e.g. SOL_SOCKET).
optionNumberOption (e.g. SO_REUSEADDR).
optionValueMemory to store option got from getsockopt().
optionLengthMemory with size of option memory.
Returns:
Result from getsockopt().

Get SO_LINGER option of socket.

Returns:
SO_LINGER value.

Get SO_REUSEADDR option of socket.

Returns:
SO_REUSEADDR value.
int Socket::getSystemSocketDescriptor ( ) const [inline]

Get system's socket descriptor. Warning: It is not recommended to manipulate the socket directly. Use Socket's methods instead.

Returns:
Socket descriptor.

Get TCP_NODELAY option of socket.

Returns:
TCP_NODELAY value.
integer Socket::getType ( ) const [inline]

Get socket's type.

Returns:
Socket type.
void Socket::init ( ) [private]
integer Socket::ioctl ( const integer  request,
const void *  argp 
) [inline]

Wrapper for ioctl().

Parameters:
requestRequest.
argpArgument.
Returns:
Result of ioctl() call.
bool Socket::listen ( const cardinal  backlog = 5)

Set socket to listen mode with given backlog (queue length for sockets waiting for acception).

Parameters:
backlogBacklog.
Returns:
true on success; false otherwise.
bool Socket::multicastMembership ( const SocketAddress address,
const char *  interface,
const bool  add 
) [private]
void Socket::packSocketAddressArray ( const sockaddr_storage *  addrArray,
const size_t  addrs,
sockaddr *  packedArray 
) [private]
ssize_t Socket::read ( void *  buffer,
const size_t  length 
) [inline]

Wrapper for read().

Parameters:
bufferBuffer to read data to.
lengthMaximum length of data to be received.
Returns:
Bytes read or error code < 0.
bool Socket::ready ( ) const [inline]

Check, if socket is ready.

Returns:
true, if socket is ready; false otherwise.
ssize_t Socket::receive ( void *  buffer,
const size_t  length,
integer flags 
) [inline]

Wrapper for recv().

Parameters:
bufferBuffer to read data to.
lengthMaximum length of data to be received.
flagsFlags for recvmsg().
Returns:
Bytes read or error code < 0.
ssize_t Socket::receiveFrom ( void *  buffer,
const size_t  length,
SocketAddress sender,
integer flags 
)

Wrapper for recvfrom().

Parameters:
bufferBuffer to receive data to.
lengthMaximum length of data to be received.
senderAddress to store sender's address.
flagsFlags for recvmsg().
Returns:
Bytes received or error code < 0.
ssize_t Socket::receiveMsg ( struct msghdr *  msg,
const integer  flags,
const bool  internalCall = false 
)

Wrapper for recvmsg().

Parameters:
msgMessage.
flagsFlags for recvmsg().
internalCallInternal usage only; set to false.
Returns:
Result of recvmsg() call.
ssize_t Socket::recvFrom ( int  fd,
void *  buf,
const size_t  len,
integer flags,
struct sockaddr *  addr,
socklen_t *  addrlen 
) [private]
bool Socket::renewFlow ( InternetFlow flow,
const cardinal  expires,
const cardinal  linger = 6 
)

Renew a flow label allocation with given expires and linger (default 6) values. The expires value gives the seconds to go until the flow label expires, the linger value gives the timeout in seconds the freed flow label cannot be allocated again.

Parameters:
flowFlow to be renewed.
expiresSeconds until the flow label expires.
lingerLinger (default 6).
Returns:
true on success; false otherwise.
bool Socket::renewFlow ( const cardinal  expires,
const cardinal  linger = 6 
)

Renew current flow's flow label allocation with given expires and linger (default 6) values.

Parameters:
expiresSeconds until the flow label expires.
lingerLinger (default 6).
Returns:
true on success; false otherwise.
ssize_t Socket::send ( const void *  buffer,
const size_t  length,
const integer  flags = 0,
const card8  trafficClass = 0x00 
)

Wrapper for send(). send() will set the packet's traffic class, if trafficClass is not 0. In this case, the packet will be sent by sendto() to the destination address, the socket is connected to!

Parameters:
bufferBuffer with data to send.
lengthLength of data to send.
flagsFlags for sendto().
trafficClassTraffic class for packet.
Returns:
Bytes sent or error code < 0.
ssize_t Socket::sendMsg ( const struct msghdr *  msg,
const integer  flags,
const card8  trafficClass = 0x00 
)

Wrapper for sendmsg().

Parameters:
msgMessage.
flagsFlags.
trafficClassTraffic class for packet.
Returns:
Result of sendmsg() call.
ssize_t Socket::sendTo ( const void *  buffer,
const size_t  length,
const integer  flags,
const SocketAddress receiver,
const card8  trafficClass = 0x00 
)

Wrapper for sendto(). sendto() will set the packet's traffic class, if trafficClass is not 0.

Parameters:
bufferBuffer with data to send.
lengthLength of data to send.
flagsFlags for sendto().
receiverAddress of receiver.
trafficClassTraffic class for packet.
Returns:
Bytes sent or error code < 0.
bool Socket::setBlockingMode ( const bool  on)

Set blocking mode.

Parameters:
onTrue to set blocking mode, false to unset.
truefor success; false otherwise.
Returns:
true for success; false otherwise.
bool Socket::setMulticastLoop ( const bool  on)

Set multicast loop mode.

Parameters:
ontrue to enable, false to disable.
Returns:
true for success; false otherwise.
bool Socket::setMulticastTTL ( const card8  ttl)

Set multicast TTL.

Parameters:
ttlTTL.
Returns:
true for success; false otherwise.
bool Socket::setSoBroadcast ( const bool  on)

Set SO_BROADCAST option of socket.

Parameters:
ontrue to set SO_BROADCAST on; false otherwise.
Returns:
true for success; false otherwise.
integer Socket::setSocketOption ( const cardinal  level,
const cardinal  optionNumber,
const void *  optionValue,
const socklen_t  optionLength 
) [inline]

Get socket option (wrapper for getsockopt());

Parameters:
levelLevel (e.g. SOL_SOCKET).
optionNumberOption (e.g. SO_REUSEADDR).
optionValueMemory with option.
optionLengthLength of option memory.
Returns:
Result from setsockopt().
bool Socket::setSoLinger ( const bool  on,
const cardinal  linger 
)

Set SO_LINGER option of socket.

Parameters:
ontrue to set linger on; false otherwise.
lingerSO_LINGER in seconds.
Returns:
true for success; false otherwise.
bool Socket::setSoReuseAddress ( const bool  on)

Set SO_REUSEADDR option of socket.

Parameters:
ontrue to set SO_REUSEADDR on; false otherwise.
Returns:
true for success; false otherwise.
bool Socket::setTCPNoDelay ( const bool  on)

Set TCP_NODELAY option of socket.

Parameters:
ontrue to set TCP_NODELAY on; false otherwise.
Returns:
true for success; false otherwise.
bool Socket::setTypeOfService ( const card8  trafficClass) [private]
void Socket::shutdown ( const cardinal  shutdownLevel)

Shutdown full-duplex connection partial or completely. SHUT_RD - further receives will be disallowed. SHUT_WR - further sends will be disallowed. SHUT_RDWR - further sends and receives will be disallowed.

Parameters:
shutdownLevelSHUT_RD, SHUT_WR, SHUT_RDWR.
ssize_t Socket::write ( const void *  buffer,
const size_t  length 
) [inline]

Wrapper for write().

Parameters:
bufferBuffer with data to write
lengthLength of data to write
Returns:
Bytes sent or error code < 0.

Friends And Related Function Documentation

friend class TrafficShaper [friend]

Member Data Documentation

sockaddr* Socket::Destination [private]
const cardinal Socket::MaxAutoSelectPort = 61000 [static]

Maximum port number for bind()'s automatic port selection.

See also:
bind
const cardinal Socket::MinAutoSelectPort = 16384 [static]

Minimum port number for bind()'s automatic port selection.

See also:
bind
int Socket::SocketDescriptor [private]
integer Socket::Type [private]

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines