|
|
This class manages a socket. IPv6 support is automatically available, when supported by the system.
enum SocketCommunicationDomain { UndefinedSocketCommunicationDomain = -1, IP = 255, IPv4 = AF_INET, IPv6 = AF_INET6, Unix = AF_UNIX } | SocketCommunicationDomain |
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 } | SocketType |
enum SocketProtocol { UndefinedSocketProtocol = -1, Default = 0, ICMPv4 = IPPROTO_ICMP, ICMPv6 = IPPROTO_ICMPV6, SCTP = IPPROTO_SCTP } | SocketProtocol |
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|GLAF_HideBroadcast|GLAF_HideMulticast|GLAF_HideAnycast } | GetSocketAddressFlags |
Socket ()
| Socket |
Constructor.
Socket (const integer communicationDomain,
const integer socketType,
const integer socketProtocol = Default)
| Socket |
Constructor for a new socket. For automatic usage of IPv6 when available, set communication domain to IP. Use IPv4/IPv6 only if a special protocol version is necessary! The creation success can be checked using ready() method.
Parameters:
communicationDomain | Communication domain (e.g. IP). |
socketType | Socket type (e.g. TCP, UDP). |
socketProtocol | Socket protocol (e.g. Default). |
See also: ready
~Socket ()
| ~Socket |
Destructor.
bool create (const integer communicationDomain = IP,
const integer socketType = TCP,
const integer socketProtocol = Default)
| create |
Close existing socket and create new socket. For automatic usage of IPv6 when available, set communication domain to IP. Use IPv4/IPv6 only if a special protocol version is necessary!
Parameters:
communicationDomain | Communication domain (e.g. IP). |
socketType | Socket type (e.g. TCP, UDP). |
socketProtocol | Socket protocol (e.g. Default). |
Returns: true, if creation was sucessful; false otherwise.
void close ()
| close |
Close socket.
void shutdown (const cardinal shutdownLevel)
| shutdown |
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:
shutdownLevel | SHUT_RD, SHUT_WR, SHUT_RDWR. |
inline card64 getBytesSent ()
| getBytesSent |
[const]
Get number of bytes sent.
Returns: Number of bytes sent.
inline card64 getBytesReceived ()
| getBytesReceived |
[const]
Get number of bytes received.
Returns: Number of bytes received.
inline void resetBytesSent ()
| resetBytesSent |
Reset number of bytes sent.
inline void resetBytesReceived ()
| resetBytesReceived |
Reset number of bytes received.
inline bool ready ()
| ready |
[const]
Check, if socket is ready.
Returns: true, if socket is ready; false otherwise.
bool bind (const SocketAddress& address = InternetAddress())
| bind |
Bind socket to given address. If address is null address, then INADDR_ANY and an automatically selected port will be used.
Parameters:
address | Socket address. |
Returns: true on success; false otherwise.
bool bindx (const SocketAddress** addressArray = NULL,
const cardinal addresses = 0,
const integer flags = 0)
| bindx |
Bind socket to one or more given addresses. If no addresses are given, INADDR_ANY and an automatically selected port will be used.
Parameters:
addressArray | Array of socket addresses. |
addresses | Number of addresses. |
Returns: true on success; false otherwise.
bool listen (const cardinal backlog = 5)
| listen |
Set socket to listen mode with given backlog (queue length for sockets waiting for acception).
Parameters:
backlog | Backlog. |
Returns: true on success; false otherwise.
Socket* accept (SocketAddress** address = NULL)
| accept |
Accept a connection.
Parameters:
address | Reference to store SocketAddress object to with peer's address to (NULL to skip). |
Returns: New socket.
bool connect (const SocketAddress& address, const card8 trafficClass = 0)
| connect |
Connect socket to given address. A value for traffic class is supported if the connection is an IPv6 connection; otherwise it is ignored.
Parameters:
address | Address. |
trafficClass | Traffic class of the connection (IPv6 only!) |
Returns: true on success; false otherwise.
inline integer getLastError ()
| getLastError |
Get last error code. It will be reset to 0 after copying.
Returns: Last error code.
inline integer getSocketOption (const cardinal level,
const cardinal optionNumber,
void* optionValue,
socklen_t* optionLength)
| getSocketOption |
Get socket option (wrapper for getsockopt());
Parameters:
level | Level (e.g. SOL_SOCKET). |
optionNumber | Option (e.g. SO_REUSEADDR). |
optionValue | Memory to store option got from getsockopt(). |
optionLength | Memory with size of option memory. |
Returns: Result from getsockopt().
cardinal getSoLinger ()
| getSoLinger |
Get SO_LINGER option of socket.
Returns: SO_LINGER value.
bool getSoReuseAddress ()
| getSoReuseAddress |
Get SO_REUSEADDR option of socket.
Returns: SO_REUSEADDR value.
bool getSoBroadcast ()
| getSoBroadcast |
Get SO_BROADCAST option of socket.
Returns: SO_BROADCAST value.
bool getTCPNoDelay ()
| getTCPNoDelay |
Get TCP_NODELAY option of socket.
Returns: TCP_NODELAY value.
bool getBlockingMode ()
| getBlockingMode |
Check, if blocking mode is on.
Returns: true, if blocking mode is on; false otherwise.
inline integer setSocketOption (const cardinal level,
const cardinal optionNumber,
const void* optionValue,
const socklen_t optionLength)
| setSocketOption |
Get socket option (wrapper for getsockopt());
Parameters:
level | Level (e.g. SOL_SOCKET). |
optionNumber | Option (e.g. SO_REUSEADDR). |
optionValue | Memory with option. |
optionLength | Length of option memory. |
Returns: Result from setsockopt().
bool setSoLinger (const bool on, const cardinal linger)
| setSoLinger |
Set SO_LINGER option of socket.
Parameters:
on | true to set linger on; false otherwise. |
linger | SO_LINGER in seconds. |
Returns: true for success; false otherwise.
bool setSoReuseAddress (const bool on)
| setSoReuseAddress |
Set SO_REUSEADDR option of socket.
Parameters:
on | true to set SO_REUSEADDR on; false otherwise. |
Returns: true for success; false otherwise.
bool setSoBroadcast (const bool on)
| setSoBroadcast |
Set SO_BROADCAST option of socket.
Parameters:
on | true to set SO_BROADCAST on; false otherwise. |
Returns: true for success; false otherwise.
bool setTCPNoDelay (const bool on)
| setTCPNoDelay |
Set TCP_NODELAY option of socket.
Parameters:
on | true to set TCP_NODELAY on; false otherwise. |
Returns: true for success; false otherwise.
bool setBlockingMode (const bool on)
| setBlockingMode |
Set blocking mode.
Parameters:
on | True to set blocking mode, false to unset. |
true | for success; false otherwise. |
Returns: true for success; false otherwise.
inline card32 getSendFlowLabel ()
| getSendFlowLabel |
[const]
Get flow label of the connection.
Returns: Flow label of the connection or 0, if there is no flow label.
See also: connect
inline card8 getSendTrafficClass ()
| getSendTrafficClass |
[const]
Get traffic class of the connection.
Returns: Traffic class of the connection or 0, if there is no traffic class.
See also: connect
inline card32 getReceivedFlowLabel ()
| getReceivedFlowLabel |
[const]
Get last received flow label.
Returns: Last received flow label or 0, if there is no flow label.
inline card8 getReceivedTrafficClass ()
| getReceivedTrafficClass |
[const]
Get last received traffic class.
Returns: Last received traffic class or 0, if there is no traffic class.
ssize_t send (const void* buffer,
const size_t length,
const cardinal flags = 0,
const card8 trafficClass = 0x00)
| send |
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:
buffer | Buffer with data to send. |
length | Length of data to send. |
flags | Flags for sendto(). |
trafficClass | Traffic class for packet. |
Returns: Bytes sent or error code < 0.
ssize_t sendTo (const void* buffer,
const size_t length,
const cardinal flags,
const SocketAddress& receiver,
const card8 trafficClass = 0x00)
| sendTo |
Wrapper for sendto(). sendto() will set the packet's traffic class, if trafficClass is not 0.
Parameters:
buffer | Buffer with data to send. |
length | Length of data to send. |
flags | Flags for sendto(). |
receiver | Address of receiver. |
trafficClass | Traffic class for packet. |
Returns: Bytes sent or error code < 0.
ssize_t sendMsg (const struct msghdr* msg,
const cardinal flags,
const card8 trafficClass = 0x00)
| sendMsg |
Wrapper for sendmsg().
Parameters:
msg | Message. |
flags | Flags. |
trafficClass | Traffic class for packet. |
Returns: Result of sendmsg() call.
inline ssize_t write (const void* buffer,
const size_t length)
| write |
Wrapper for write().
Parameters:
buffer | Buffer with data to write |
length | Length of data to write |
Returns: Bytes sent or error code < 0.
inline ssize_t receive (void* buffer,
const size_t length,
const cardinal flags = 0)
| receive |
Wrapper for recv().
Parameters:
buffer | Buffer to read data to. |
length | Maximum length of data to be received. |
flags | Flags for recv(). |
Returns: Bytes read or error code < 0.
ssize_t receiveFrom (void* buffer,
const size_t length,
SocketAddress& sender,
const cardinal flags = 0)
| receiveFrom |
Wrapper for recvfrom().
Parameters:
buffer | Buffer to receive data to. |
length | Maximum length of data to be received. |
sender | Address to store sender's address. |
flags | Flags for recvfrom(). |
Returns: Bytes received or error code < 0.
ssize_t receiveMsg (struct msghdr* msg,
const cardinal flags,
const bool internalCall = false)
| receiveMsg |
Wrapper for recvmsg().
Parameters:
msg | Message. |
flags | Flags. |
internalCall | Internal usage only; set to false. |
Returns: Result of recvmsg() call.
inline ssize_t read (void* buffer,
const size_t length)
| read |
Wrapper for read().
Parameters:
buffer | Buffer to read data to. |
length | Maximum length of data to be received. |
Returns: Bytes read or error code < 0.
inline integer fcntl (const integer cmd, long arg)
| fcntl |
Wrapper for fcntl().
Parameters:
cmd | Command. |
arg | Argument. |
Returns: Result of fcntl() call.
inline integer fcntl (const integer cmd, long arg, struct flock* lock)
| fcntl |
Wrapper for fcntl().
Parameters:
cmd | Command. |
arg | Argument. |
lock | Lock. |
Returns: Result of fcntl() call.
inline integer ioctl (const integer request, const void* argp)
| ioctl |
Wrapper for ioctl().
Parameters:
request | Request. |
argp | Argument. |
Returns: Result of ioctl() call.
bool getSocketAddress (SocketAddress& address)
| getSocketAddress |
[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:
address | Reference to SocketAddress to write address to. |
Returns: true, if call was successful; false otherwise.
See also: bind, connect, getPeerAddress
bool getPeerAddress (SocketAddress& address)
| getPeerAddress |
[const]
Get the peer's address. Note: A socket has to be connected to a peer first to get a peer address!
Parameters:
address | Reference to SocketAddress to write address to. |
Returns: true, if call was successful; false otherwise.
See also: bind, connect, getSocketAddress
inline bool addMulticastMembership (const SocketAddress& address,
const char* interface = NULL)
| addMulticastMembership |
Add multicast membership.
Parameters:
address | Multicast address. |
interface | Interface name. |
Returns: true for success; false otherwise.
inline bool dropMulticastMembership (const SocketAddress& address,
const char* interface = NULL)
| dropMulticastMembership |
Drop multicast membership.
Parameters:
address | Multicast address. |
interface | Interface name. |
Returns: true for success; false otherwise.
bool getMulticastLoop ()
| getMulticastLoop |
Get multicast loop mode.
Returns: true if multicast loop is enabled, false otherwise.
bool setMulticastLoop (const bool on)
| setMulticastLoop |
Set multicast loop mode.
Parameters:
on | true to enable, false to disable. |
Returns: true for success; false otherwise.
card8 getMulticastTTL ()
| getMulticastTTL |
Get multicast TTL.
Returns: Multicast TTL.
bool setMulticastTTL (const card8 ttl)
| setMulticastTTL |
Set multicast TTL.
Parameters:
ttl | TTL. |
Returns: true for success; false otherwise.
InternetFlow allocFlow (const InternetAddress& address,
const card32 flowLabel = 0,
const card8 shareLevel = 2)
| allocFlow |
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:
address | Address of the destination. |
flowLabel | Flowlabel; 0 for random value. |
shareLevel | Share level for flow label. |
Returns: InternetFlow.
void freeFlow (InternetFlow& flow)
| freeFlow |
Free a flow.
Parameters:
flow | Flow to be freed. |
bool renewFlow (InternetFlow& flow,
const cardinal expires,
const cardinal linger = 6)
| renewFlow |
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:
flow | Flow to be renewed. |
expires | Seconds until the flow label expires. |
linger | Linger (default 6). |
Returns: true on success; false otherwise.
bool renewFlow (const cardinal expires,
const cardinal linger = 6)
| renewFlow |
Renew current flow's flow label allocation with given expires and linger (default 6) values.
Parameters:
expires | Seconds until the flow label expires. |
linger | Linger (default 6). |
Returns: true on success; false otherwise.
bool bindInternetSocketPair (Socket& senderSocket,
Socket& receiverSocket,
const InternetAddress& receiver = InternetAddress())
| bindInternetSocketPair |
[static]
Bind a pair of internet 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:
senderSocket | First socket. |
receiverSocket | Second socket. |
receiver | Address (e.g ipv6-localhost:0) or NULL for Any address. |
inline int getSystemSocketDescriptor ()
| getSystemSocketDescriptor |
[const]
Get system's socket descriptor. Warning: It is not recommended to manipulate the socket directly. Use Socket's methods instead.
Returns: Socket descriptor.
bool getLocalAddressList (SocketAddress**& addressList,
cardinal& numberOfNets,
const cardinal flags = GLAF_Default)
| getLocalAddressList |
[static]
Get list of all local addresses (IPv4 and IPv6 are currently supported). The resulting list has to be deallocated using SocketAddress::deleteAddressList().
Parameters:
addressList | Reference to store address list to. |
numberOfNets | Reference to store number of addresses to. |
flags | Flags. |
Returns: true for success; false otherwise.
See also: SocketAddress#deleteAddressList
static const cardinal MinAutoSelectPort | MinAutoSelectPort |
static const cardinal MaxAutoSelectPort | MaxAutoSelectPort |
friend class TrafficShaper | TrafficShaper |
[protected]
void init ()
| init |
[protected]
Maximum port number for bind()'s automatic port selection.
See also: bind
bool setTypeOfService (const card8 trafficClass)
| setTypeOfService |
[protected]
ssize_t recvFrom (int fd,
void* buf,
const size_t len,
const integer flags,
struct sockaddr* addr,
socklen_t* addrlen)
| recvFrom |
[protected]
bool multicastMembership (const SocketAddress& address,
const char* interface,
const bool add)
| multicastMembership |
[protected]
card64 BytesSent | BytesSent |
[protected]
card64 BytesReceived | BytesReceived |
[protected]
card32 SendFlow | SendFlow |
[protected]
card32 ReceivedFlow | ReceivedFlow |
[protected]
cardinal Backlog | Backlog |
[protected]
integer LastError | LastError |
[protected]
int SocketDescriptor | SocketDescriptor |
[protected]
sockaddr* Destination | Destination |
[protected]
integer CommunicationDomain | CommunicationDomain |
[protected]
integer Type | Type |
[protected]
integer Protocol | Protocol |
[protected]