simple-socket library 1.9.0
Loading...
Searching...
No Matches
NET::SCTPSocket Class Reference

SCTP socket class. More...

#include <simple_socket/SCTPSocket.h>

Inheritance diagram for NET::SCTPSocket:
Collaboration diagram for NET::SCTPSocket:

Public Types

enum  abortFlag { ABORT = SCTP_ABORT , SHUTDOWN = SCTP_EOF , KEEPALIVE = 0 }
enum  switchAddressFlag { KEEP_PRIMARY = 0 , OVERRIDE_PRIMARY = SCTP_ADDR_OVER }
enum  receiveFlag

Public Member Functions

 SCTPSocket (uint16_t numOutStreams=10, uint16_t maxInStreams=65535, uint16_t maxAttempts=4, uint16_t maxInitTimeout=0)
int bind (const std::vector< std::string > &localAddresses, unsigned short localPort=0)
int connect (const std::vector< std::string > &foreignAddresses, unsigned short foreignPort=0)
int state () const
int notAckedData () const
int pendingData () const
unsigned inStreams () const
unsigned outStreams () const
unsigned fragmentationPoint () const
std::string primaryAddress () const
IOResult send (const void *data, size_t length, uint16_t stream, unsigned ttl=0, unsigned context=0, unsigned ppid=0, abortFlag abort=KEEPALIVE, switchAddressFlag switchAddr=KEEP_PRIMARY)
IOResult sendUnordered (const void *data, size_t length, uint16_t stream, unsigned ttl=0, unsigned context=0, unsigned ppid=0, abortFlag abort=KEEPALIVE, switchAddressFlag switchAddr=KEEP_PRIMARY)
IOResult receive (void *data, size_t maxLen, uint16_t &stream)
IOResult receive (void *data, size_t maxLen, uint16_t &stream, receiveFlag &flag)
IOResult timedReceive (void *data, size_t maxLen, uint16_t &stream, int timeout)
IOResult timedReceive (void *data, size_t maxLen, uint16_t &stream, receiveFlag &flag, int timeout)
std::optional< SCTPSocket > accept () const
std::optional< SCTPSocket > timedAccept (int timeout) const
IOResult send (const void *buffer, size_t len)
 send data through a connected socket
void listen (int backlog=128)
 listen for incoming connections
ConnectResult finishConnect (int timeout=0)
 check if a previously started connected attempt is finished
void setReuseAddress (bool enable)
 Enable or disable local address reuse.
ConnectResult connect (std::string_view foreignAddress, unsigned short foreignPort)
 establish a connection with the given foreign address and port
ConnectResult timedConnect (std::string_view foreignAddress, unsigned short foreignPort, int timeout)
 establish a connection with the given foreign address and port
void bind (unsigned short localPort=0)
void bind (std::string_view localAddress, unsigned short localPort=0)
 bind socket to address / port
std::string getLocalAddress () const
unsigned short getLocalPort () const
std::string getForeignAddress () const
unsigned short getForeignPort () const
AddressFamily addressFamily () const noexcept
 return wether this socket was created for IPv4 or IPv6
int nativeHandle () noexcept
 Return the native descriptor. Ownership remains with the object.
int duplicateNativeHandle () const
 Duplicates the underlaying native socket descriptor.
bool valid () const noexcept
 Returns true if socket is in a usable (non-moved) state and open.
IOResult receive (void *buffer, size_t len)
 receive data from a bound socket
IOResult timedReceive (void *buffer, size_t len, int timeout)
 receive data from a bound socket, return after the given timespan
void disconnect ()
 Disconnect and unset any foreign addresses.
bool isConnected () const
 check if the kernel currently has an associated peer address
void shutdown (ShutdownDirection type)
 shutdown the connection in the specified direction
bool peerDisconnected () const
 returns whether a currently associated peer disconnected
bool isBlocking () const
 returns true if socket is in blocking mode
void setBlocking (bool blocking)
 set socket to blocking or non-blocking I/O operations
void setSendBufferSize (int bytes)
 override default send buffer size for this socket
void setReceiveBufferSize (int bytes)
 override default receive buffer size for this socket

Protected Member Functions

void setInitValues (uint16_t ostr, uint16_t istr, uint16_t att, uint16_t time)
 InternetSocket (AddressFamily family, int type, int protocol)
 allows a subclass to create new socket
 InternetSocket (AdoptSocketTag, int sockfd, int type)
 allows a subclass to take ownership of an existing socket
 InternetSocket (InternetSocket &&other) noexcept=default
IOResult sendAll (const void *buffer, size_t len)
 send data through a connected socket
IOResult receiveAll (void *buffer, size_t len)
 receive exactly the requested amount unless the peer disconnects

Protected Attributes

AddressFamily m_family
int m_socket
bool m_connectionOriented

Detailed Description

SCTP socket class.

Definition at line 14 of file SCTPSocket.h.

Member Enumeration Documentation

◆ abortFlag

Enumerator
ABORT 
SHUTDOWN 
KEEPALIVE 

Definition at line 17 of file SCTPSocket.h.

◆ receiveFlag

Definition at line 30 of file SCTPSocket.h.

◆ switchAddressFlag

Enumerator
KEEP_PRIMARY 
OVERRIDE_PRIMARY 

Definition at line 24 of file SCTPSocket.h.

Constructor & Destructor Documentation

◆ SCTPSocket()

NET::SCTPSocket::SCTPSocket ( uint16_t numOutStreams = 10,
uint16_t maxInStreams = 65535,
uint16_t maxAttempts = 4,
uint16_t maxInitTimeout = 0 )

Member Function Documentation

◆ accept()

std::optional< SCTPSocket > NET::SCTPSocket::accept ( ) const

◆ addressFamily()

AddressFamily NET::InternetSocket::addressFamily ( ) const
inlinenoexceptinherited

return wether this socket was created for IPv4 or IPv6

Definition at line 117 of file InternetSocket.h.

◆ bind() [1/3]

void NET::InternetSocket::bind ( std::string_view localAddress,
unsigned short localPort = 0 )
inherited

bind socket to address / port

Set the local port to the specified port and the local address to an interface.

bind() attaches the local endpoint of the socket to the specified interface and port. Typically all servers use bind() to attach to the local port specified by the used protocol. You just need to call bind if you want to restrict the connection to a specific local interface, or if you want to listen on a specific port. If you are just a client using connect() to connect to a server, calling bind is most likely unnecessary.

To bind to a port < 1023 you need eighter superuser access, or the SUID bit set on your program.

Parameters
localAddresslocal address
localPortlocal port
Exceptions
SocketExceptionthrown if setting local port fails

◆ bind() [2/3]

void NET::InternetSocket::bind ( unsigned short localPort = 0)
inherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Instead of the richer function with more arguments, this bind() binds to any available interface / address

Examples
TCP_Server.cpp, UDP_Commands.cpp, and UDP_Multicast.cpp.

◆ bind() [3/3]

int NET::SCTPSocket::bind ( const std::vector< std::string > & localAddresses,
unsigned short localPort = 0 )

◆ connect() [1/2]

ConnectResult NET::InternetSocket::connect ( std::string_view foreignAddress,
unsigned short foreignPort )
inherited

establish a connection with the given foreign address and port

If you are using a connection oriented socket (like TCPSocket), you have to call connect() on that socket in order to send data. However, if you are using a non-connection oriented socket (like UDPSocket), calling connect on that socket will allow you to use the send() function like on a connection oriented socket. If not connected, you would have to use sendTo.

Parameters
foreignAddressforeign address
foreignPortforeign
Returns
ConnectResult
Exceptions
SocketExceptionthrown if unable to establish connection
Examples
TCP_Client.cpp.

◆ connect() [2/2]

int NET::SCTPSocket::connect ( const std::vector< std::string > & foreignAddresses,
unsigned short foreignPort = 0 )

◆ disconnect()

void NET::SimpleSocket::disconnect ( )
inherited

Disconnect and unset any foreign addresses.

This will ungracefully abort the connection by sending an RST frame. disconnect() should not be used under normal circumstances, as it will terminate the connection OOB, preventing the other end of the communication from retrieving data that would still be available in the OS buffer.

For normal termination of a connection use shutdown().

Exceptions
SocketExceptionthrown if unable to disconnect

◆ duplicateNativeHandle()

int NET::SimpleSocket::duplicateNativeHandle ( ) const
nodiscardinherited

Duplicates the underlaying native socket descriptor.

◆ finishConnect()

check if a previously started connected attempt is finished

Use this to check if a non-blocking connect() call has completed.

Parameters
timeoutthe timeout in ms after which we stop waiting, connection is still active
Exceptions
SocketException

◆ fragmentationPoint()

unsigned NET::SCTPSocket::fragmentationPoint ( ) const

◆ getForeignAddress()

std::string NET::InternetSocket::getForeignAddress ( ) const
inherited

Get the foreign address. Call connect() before using this function.

Returns
foreign address or empty string when not connected
Exceptions
SocketExceptionthrown if unable to fetch foreign address
Examples
TCP_Client_Reconnect.cpp, and TCP_Server.cpp.

◆ getForeignPort()

unsigned short NET::InternetSocket::getForeignPort ( ) const
inherited

Get the foreign port. Call connect() before using this function.

Returns
foreign port or 0 when not connected
Exceptions
SocketExceptionthrown if unable to fetch foreign port
Examples
TCP_Client_Reconnect.cpp, and TCP_Server.cpp.

◆ getLocalAddress()

std::string NET::InternetSocket::getLocalAddress ( ) const
inherited

Get the local address (after binding the socket).

Returns
local address of socket
Exceptions
SocketExceptionthrown if fetch fails

◆ getLocalPort()

unsigned short NET::InternetSocket::getLocalPort ( ) const
inherited

Get the local port (after binding the socket).

Returns
local port of socket
Exceptions
SocketExceptionthrown if fetch fails

◆ inStreams()

unsigned NET::SCTPSocket::inStreams ( ) const

◆ InternetSocket() [1/3]

NET::InternetSocket::InternetSocket ( AddressFamily family,
int type,
int protocol )
protected

allows a subclass to create new socket

◆ InternetSocket() [2/3]

NET::InternetSocket::InternetSocket ( AdoptSocketTag ,
int sockfd,
int type )
protected

allows a subclass to take ownership of an existing socket

◆ InternetSocket() [3/3]

NET::InternetSocket::InternetSocket ( InternetSocket && other)
protecteddefaultnoexcept

◆ isBlocking()

bool NET::SimpleSocket::isBlocking ( ) const
nodiscardinherited

returns true if socket is in blocking mode

◆ isConnected()

bool NET::SimpleSocket::isConnected ( ) const
nodiscardinherited

check if the kernel currently has an associated peer address

For stream sockets, true means a peer association exists; it does not guarantee that the remote endpoint is alive or that I/O can succeed. A peer that has sent FIN may still be associated with the socket.

For datagram sockets, true only indicates that connect() configured a default peer address; it does not establish a network connection.

Returns
false if getpeername() reports ENOTCONN
Exceptions
SocketExceptionfor other getpeername() errors

◆ listen()

void NET::SimpleSocket::listen ( int backlog = 128)

listen for incoming connections

listen() can be called on a bound socket. The socket will then go into a passive state and accept incoming connections. To answer to an incoming connection call accept() or timedAccept() on the socket.

The backlog specifies the maximum requested number of pending connections waiting to be accepted.

Parameters
backlogrequested pending connection queue size
Exceptions
SocketException

◆ nativeHandle()

int NET::SimpleSocket::nativeHandle ( )
inlinenoexceptinherited

Return the native descriptor. Ownership remains with the object.

Definition at line 128 of file SimpleSocket.h.

◆ notAckedData()

int NET::SCTPSocket::notAckedData ( ) const

◆ outStreams()

unsigned NET::SCTPSocket::outStreams ( ) const

◆ peerDisconnected()

bool NET::SimpleSocket::peerDisconnected ( ) const
nodiscardinherited

returns whether a currently associated peer disconnected

Returns whether the OS currently reports hangup or error for the stream peer. A false result does not guarantee that the remote peer is reachable.

Returns
true if a peer disconnected or is no longer connected

◆ pendingData()

int NET::SCTPSocket::pendingData ( ) const

◆ primaryAddress()

std::string NET::SCTPSocket::primaryAddress ( ) const

◆ receive() [1/3]

IOResult NET::SCTPSocket::receive ( void * data,
size_t maxLen,
uint16_t & stream )

◆ receive() [2/3]

IOResult NET::SCTPSocket::receive ( void * data,
size_t maxLen,
uint16_t & stream,
receiveFlag & flag )

◆ receive() [3/3]

IOResult NET::SimpleSocket::receive ( void * buffer,
size_t len )
inherited

receive data from a bound socket

receive() can only be used on a socket that called bind() or connect() before. If you try to use receive() on a not bound socket, SocketException will be thrown.

If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.

If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.

If the remote host has closed the connection (on a connection based socket like TCP) receive() will return PeerDisconnected in the result status. If you are using a connectionless protocol (like UDP) you may get DestinationUnavailable in the result status when the peer refuses via ICMP.

Parameters
bufferthe buffer the received data will be written to
lenlength of the provided buffer, receive will not read more than that
Returns
IOResult
Exceptions
SocketExceptionin case an error occured

◆ receiveAll()

IOResult NET::SimpleSocket::receiveAll ( void * buffer,
size_t len )
protectedinherited

receive exactly the requested amount unless the peer disconnects

receiveAll() repeatedly calls receive() until len bytes have been received. This is useful for fixed-size data on TCP, where one receive() call is not guaranteed to return all bytes sent by the peer.

If the peer closes or resets the connection before len bytes arrive, the function returns an error status with the number of bytes received so far.

Parameters
bufferbuffer the received data will be written to
lennumber of bytes to receive
Returns
IOResult
Exceptions
SocketExceptionon receive errors other than peer loss

◆ send() [1/2]

IOResult NET::SimpleSocket::send ( const void * buffer,
size_t len )

send data through a connected socket

send() can only be used on a socket that called connect() before. If you try to use send() on a not connected socket, SocketException will be thrown.

If you are using a stream oriented socket (like TCPSocket), the operating system is allowed to send only a part of the packet you told it to send, so send() will return the number of bytes actually sent. It is your responsibility to resend the data not sent by send()

If you are using a datagram oriented socket (like UDPSocket or SCTPSocket) the operating system will only send and receive complete datagrams, but send() will fail if you are trying to send too much data. In that case SocketException will be thrown.

Parameters
bufferdata to be send
lenlength of the data to be sent
Returns
IOResult
Exceptions
SocketExceptionif sending went wrong

◆ send() [2/2]

IOResult NET::SCTPSocket::send ( const void * data,
size_t length,
uint16_t stream,
unsigned ttl = 0,
unsigned context = 0,
unsigned ppid = 0,
abortFlag abort = KEEPALIVE,
switchAddressFlag switchAddr = KEEP_PRIMARY )

◆ sendAll()

IOResult NET::SimpleSocket::sendAll ( const void * buffer,
size_t len )
protectedinherited

send data through a connected socket

sendAll() can only be used on a socket that called connect() before. If you try to use sendAll() on a unconnected socket, a SocketException will be thrown.

When using a stream oriented socket, the operating system is allowed to send only a part of the data you requested it to send. Because of this, you would have to check the number of sent bytes when using send(), and resend unsent data manually. sendAll() takes that responsibility from you and resends as long as it needs, to completely send the data given to it.

Parameters
bufferdata to be send
lenlength of the data to be sent
Returns
IOResult
Exceptions
SocketException

◆ sendUnordered()

IOResult NET::SCTPSocket::sendUnordered ( const void * data,
size_t length,
uint16_t stream,
unsigned ttl = 0,
unsigned context = 0,
unsigned ppid = 0,
abortFlag abort = KEEPALIVE,
switchAddressFlag switchAddr = KEEP_PRIMARY )

◆ setBlocking()

void NET::SimpleSocket::setBlocking ( bool blocking)
inherited

set socket to blocking or non-blocking I/O operations

A non-blocking socket will return from all I/O operations right away. The IOStatus WouldBlock is used to signify no work can be done at the moment. An event handler can use poll(), epoll(), etc. to monitor the socket for readyness for the next operation.

All timed operations provided by socket classes are not affected. Timed calls wait up to timeout ms for even with non-blocking sockets.

◆ setInitValues()

void NET::SCTPSocket::setInitValues ( uint16_t ostr,
uint16_t istr,
uint16_t att,
uint16_t time )
protected

◆ setReceiveBufferSize()

void NET::SimpleSocket::setReceiveBufferSize ( int bytes)
inherited

override default receive buffer size for this socket

◆ setReuseAddress()

void NET::InternetSocket::setReuseAddress ( bool enable)
inherited

Enable or disable local address reuse.

SO_REUSEADDR allows binding a local address that may still be associated with previous connections, e.g. TCP connections in TIME_WAIT. This option should be set before bind().

Parameters
enabletrue to enable address reuse
Exceptions
SocketExceptionif changing the option fails
Examples
TCP_Event_Server.cpp, TCP_Server.cpp, and UDP_Multicast.cpp.

◆ setSendBufferSize()

void NET::SimpleSocket::setSendBufferSize ( int bytes)
inherited

override default send buffer size for this socket

◆ shutdown()

void NET::SimpleSocket::shutdown ( ShutdownDirection type)
inherited

shutdown the connection in the specified direction

This will gracefully end a connection by sending a FIN frame. Use this to end a normal communication cycle. To abort a connection see disconnect().

Depending on the specified ShutdownDirection, calls for that direction will stop working. Use this function if you want to have more control than just destroing the socket. It allows you to cut the connection in one direction, or both.

If you use shutdown() on an unconnected socket, the corresponding calls will simply stop working.

Parameters
typethe ShutdownDirection that be used
Exceptions
SocketExceptionin case an error occured
Examples
TCP_Server.cpp.

◆ state()

int NET::SCTPSocket::state ( ) const

◆ timedAccept()

std::optional< SCTPSocket > NET::SCTPSocket::timedAccept ( int timeout) const

◆ timedConnect()

ConnectResult NET::InternetSocket::timedConnect ( std::string_view foreignAddress,
unsigned short foreignPort,
int timeout )
inherited

establish a connection with the given foreign address and port

Provides timeout for TCP socket connection establishment. When a timeout occurs, the OS may still continue the connection attempt in the background. Use disconnect() or destroy the socket to avoid this.

Parameters
foreignAddressforeign address
foreignPortforeign port
timeoutthe timeout in ms after which connect will give up and return
Exceptions
SocketExceptionthrown if unable to establish connection
Examples
TCP_Client_Reconnect.cpp.

◆ timedReceive() [1/3]

IOResult NET::SCTPSocket::timedReceive ( void * data,
size_t maxLen,
uint16_t & stream,
int timeout )

◆ timedReceive() [2/3]

IOResult NET::SCTPSocket::timedReceive ( void * data,
size_t maxLen,
uint16_t & stream,
receiveFlag & flag,
int timeout )

◆ timedReceive() [3/3]

IOResult NET::SimpleSocket::timedReceive ( void * buffer,
size_t len,
int timeout )
inherited

receive data from a bound socket, return after the given timespan

timedReceive() can only be used on a socket that called bind() or connect before. If you try to use receive() on a not bound socket, SocketException will be thrown.

If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.

If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.

If the remote host has closed the connection (on a connection based socket like TCP) receive() will return PeerDisconnected in the result status. If you are using a connectionless protocol (like UDP) you may get DestinationUnavailable in the result status when the peer refuses via ICMP.

Parameters
bufferthe buffer the received data will be written to
lenlength of the provided buffer, receive will not read more than that
timeoutthe timeout in ms after which receive will give up and return
Returns
IOResult
Exceptions
SocketExceptionin case an error occured
Examples
TCP_Client.cpp, and TCP_Client_Reconnect.cpp.

◆ valid()

bool NET::SimpleSocket::valid ( ) const
noexceptinherited

Returns true if socket is in a usable (non-moved) state and open.

Member Data Documentation

◆ m_connectionOriented

bool NET::SimpleSocket::m_connectionOriented
protectedinherited

Definition at line 395 of file SimpleSocket.h.

◆ m_family

AddressFamily NET::InternetSocket::m_family
protectedinherited

Definition at line 130 of file InternetSocket.h.

◆ m_socket

int NET::SimpleSocket::m_socket
protectedinherited

Definition at line 394 of file SimpleSocket.h.


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