SCTP socket class. More...
#include <simple_socket/SCTPSocket.h>
Public Types | |
| enum | abortFlag { ABORT = SCTP_ABORT , SHUTDOWN = SCTP_EOF , KEEPALIVE = 0 } |
| enum | switchAddressFlag { KEEP_PRIMARY = 0 , OVERRIDE_PRIMARY = SCTP_ADDR_OVER } |
| enum | receiveFlag |
| typedef SocketHandle< SCTPSocket > | Handle |
| Handle for a new socket returned by accept(). | |
| enum | ShutdownDirection { STOP_SEND = SHUT_WR , STOP_RECEIVE = SHUT_RD , STOP_BOTH = SHUT_RDWR } |
Public Member Functions | |
| SCTPSocket (uint16_t numOutStreams=10, uint16_t maxInStreams=65535, uint16_t maxAttempts=4, uint16_t maxInitTimeout=0) | |
| SCTPSocket (Handle &&handle) | |
| 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 |
| int | 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) |
| int | 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) |
| int | receive (void *data, size_t maxLen, uint16_t &stream) |
| int | receive (void *data, size_t maxLen, uint16_t &stream, receiveFlag &flag) |
| int | timedReceive (void *data, size_t maxLen, uint16_t &stream, int timeout) |
| int | timedReceive (void *data, size_t maxLen, uint16_t &stream, receiveFlag &flag, int timeout) |
| Handle | accept () const |
| Handle | timedAccept (int timeout) const |
| void | bind (unsigned short localPort=0) |
| void | bind (std::string_view localAddress, unsigned short localPort=0) |
| bind socket to address / port | |
| void | connect (std::string_view foreignAddress, unsigned short foreignPort) |
| establish a connection with the given foreign address and port | |
| int | send (const void *buffer, size_t len) |
| send data through a connected socket | |
| int | receive (void *buffer, size_t len) |
| receive data from a bound socket | |
| int | timedReceive (void *buffer, size_t len, int timeout) |
| receive data from a bound socket, return after the given timespan | |
| void | listen (int backlog=5) |
| Listen for incoming connections. | |
| void | setReuseAddress (bool enable) |
| Enable or disable local address reuse. | |
| int | timedConnect (std::string_view foreignAddress, unsigned short foreignPort, int timeout) |
| establish a connection with the given foreign address and 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 handle of the open socket | |
| void | disconnect () |
| Disconnect and unset any foreign addresses. | |
| void | shutdown (ShutdownDirection type) |
| shutdown the connection in the specified direction | |
| bool | peerDisconnected () const noexcept |
| returns whether a currently associated peer disconnected | |
Protected Member Functions | |
| void | setInitValues (uint16_t ostr, uint16_t istr, uint16_t att, uint16_t time) |
| int | sendAll (const void *buffer, size_t len) |
| send data through a connected socket | |
| int | 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_peerDisconnected |
| bool | m_connectionOriented |
SCTP socket class.
Definition at line 14 of file SCTPSocket.h.
| typedef SocketHandle<SCTPSocket> NET::SCTPSocket::Handle |
Handle for a new socket returned by accept().
Definition at line 18 of file SCTPSocket.h.
| enum NET::SCTPSocket::abortFlag |
Definition at line 20 of file SCTPSocket.h.
| enum NET::SCTPSocket::receiveFlag |
Definition at line 33 of file SCTPSocket.h.
|
inherited |
| enum NET::SCTPSocket::switchAddressFlag |
Definition at line 27 of file SCTPSocket.h.
| SCTPSocket::SCTPSocket | ( | uint16_t | numOutStreams = 10, |
| uint16_t | maxInStreams = 65535, | ||
| uint16_t | maxAttempts = 4, | ||
| uint16_t | maxInitTimeout = 0 ) |
Definition at line 22 of file SCTPSocket.cpp.
|
explicit |
Construct a Socket from a Handle returned by accept()
| SocketException | thrown if handle is invalid |
Definition at line 32 of file SCTPSocket.cpp.
| SCTPSocket::Handle SCTPSocket::accept | ( | ) | const |
Definition at line 203 of file SCTPSocket.cpp.
|
inlinenoexceptinherited |
return wether this socket was created for IPv4 or IPv6
Definition at line 117 of file InternetSocket.h.
| int SCTPSocket::bind | ( | const std::vector< std::string > & | localAddresses, |
| unsigned short | localPort = 0 ) |
Definition at line 37 of file SCTPSocket.cpp.
| void InternetSocket::bind | ( | std::string_view | localAddress, |
| unsigned short | localPort = 0 ) |
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.
| localAddress | local address |
| localPort | local port |
| SocketException | thrown if setting local port fails |
Definition at line 86 of file InternetSocket.cpp.
| void InternetSocket::bind | ( | unsigned short | localPort = 0 | ) |
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
Definition at line 64 of file InternetSocket.cpp.
| int SCTPSocket::connect | ( | const std::vector< std::string > & | foreignAddresses, |
| unsigned short | foreignPort = 0 ) |
Definition at line 56 of file SCTPSocket.cpp.
| void InternetSocket::connect | ( | std::string_view | foreignAddress, |
| unsigned short | foreignPort ) |
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
| foreignAddress | foreign address |
| foreignPort | foreign port |
| SocketException | thrown if unable to establish connection |
Definition at line 43 of file InternetSocket.cpp.
|
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().
| SocketException | thrown if unable to disconnect |
Definition at line 112 of file SimpleSocket.cpp.
| unsigned SCTPSocket::fragmentationPoint | ( | ) | const |
Definition at line 103 of file SCTPSocket.cpp.
|
inherited |
Get the foreign address. Call connect() before using this function.
| SocketException | thrown if unable to fetch foreign address |
Definition at line 168 of file InternetSocket.cpp.
|
inherited |
Get the foreign port. Call connect() before using this function.
| SocketException | thrown if unable to fetch foreign port |
Definition at line 178 of file InternetSocket.cpp.
|
inherited |
Get the local address (after binding the socket).
| SocketException | thrown if fetch fails |
Definition at line 148 of file InternetSocket.cpp.
|
inherited |
Get the local port (after binding the socket).
| SocketException | thrown if fetch fails |
Definition at line 158 of file InternetSocket.cpp.
| unsigned SCTPSocket::inStreams | ( | ) | const |
Definition at line 93 of file SCTPSocket.cpp.
| void SimpleSocket::listen | ( | int | backlog = 5 | ) |
Listen for incoming connections.
Definition at line 211 of file SimpleSocket.cpp.
|
inlinenoexceptinherited |
return the native handle of the open socket
Definition at line 68 of file SimpleSocket.h.
| int SCTPSocket::notAckedData | ( | ) | const |
Definition at line 83 of file SCTPSocket.cpp.
| unsigned SCTPSocket::outStreams | ( | ) | const |
Definition at line 98 of file SCTPSocket.cpp.
|
noexceptinherited |
returns whether a currently associated peer disconnected
For stream sockets this is set when the peer closes or resets the connection. Use this function after a call to receive, returned 0 received bytes.
For connected datagram sockets it may also be set when the network reports that the configured peer is unreachable.
The flag is cleared by a successful connect().
Definition at line 132 of file SimpleSocket.cpp.
| int SCTPSocket::pendingData | ( | ) | const |
Definition at line 88 of file SCTPSocket.cpp.
| std::string SCTPSocket::primaryAddress | ( | ) | const |
Definition at line 108 of file SCTPSocket.cpp.
| int SimpleSocket::receive | ( | void * | buffer, |
| size_t | len ) |
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 or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
| SocketException | in case an error occured |
Definition at line 117 of file SimpleSocket.cpp.
| int SCTPSocket::receive | ( | void * | data, |
| size_t | maxLen, | ||
| uint16_t & | stream ) |
Definition at line 136 of file SCTPSocket.cpp.
| int SCTPSocket::receive | ( | void * | data, |
| size_t | maxLen, | ||
| uint16_t & | stream, | ||
| receiveFlag & | flag ) |
Definition at line 146 of file SCTPSocket.cpp.
|
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 the number of bytes received so far. In that case peerDisconnected() is set by the underlying receive() call.
| buffer | buffer the received data will be written to |
| len | number of bytes to receive |
| SocketException | on receive errors other than peer loss |
Definition at line 188 of file SimpleSocket.cpp.
| int 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.
| buffer | data to be send |
| len | length of the data to be sent |
| SocketException | if sending went wrong |
Definition at line 91 of file SimpleSocket.cpp.
| int 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 ) |
Definition at line 113 of file SCTPSocket.cpp.
|
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 bit 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.
| buffer | data to be send |
| len | length of the data to be sent |
| SocketException |
Definition at line 175 of file SimpleSocket.cpp.
| int 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 ) |
Definition at line 124 of file SCTPSocket.cpp.
|
protected |
Definition at line 215 of file SCTPSocket.cpp.
|
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().
| enable | true to enable address reuse |
| SocketException | if changing the option fails |
Definition at line 67 of file InternetSocket.cpp.
|
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.
| type | the ShutdownDirection that be used |
| SocketException | in case an error occured |
Definition at line 126 of file SimpleSocket.cpp.
| int SCTPSocket::state | ( | ) | const |
Definition at line 78 of file SCTPSocket.cpp.
| SCTPSocket::Handle SCTPSocket::timedAccept | ( | int | timeout | ) | const |
Definition at line 209 of file SCTPSocket.cpp.
|
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.
| foreignAddress | foreign address |
| foreignPort | foreign port |
| timeout | the timeout in ms after which connect will give up and return |
| SocketException | thrown if unable to establish connection |
Definition at line 84 of file InternetSocket.cpp.
| int SimpleSocket::timedReceive | ( | void * | buffer, |
| size_t | len, | ||
| int | timeout ) |
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 or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
| timeout | the timeout in ms after which receive will give up and return |
| SocketException | in case an error occured |
Definition at line 144 of file SimpleSocket.cpp.
| int SCTPSocket::timedReceive | ( | void * | data, |
| size_t | maxLen, | ||
| uint16_t & | stream, | ||
| int | timeout ) |
Definition at line 157 of file SCTPSocket.cpp.
| int SCTPSocket::timedReceive | ( | void * | data, |
| size_t | maxLen, | ||
| uint16_t & | stream, | ||
| receiveFlag & | flag, | ||
| int | timeout ) |
Definition at line 180 of file SCTPSocket.cpp.
|
protectedinherited |
Definition at line 268 of file SimpleSocket.h.
|
protected |
Definition at line 85 of file SCTPSocket.h.
|
protectedinherited |
Definition at line 267 of file SimpleSocket.h.
|
protectedinherited |
Definition at line 266 of file SimpleSocket.h.