PLCnext API Documentation 23.6.0.37
|
Interface to realizes ethernet based communications. More...
#include <Socket.hpp>
Public Types | |
typedef std::shared_ptr< Socket > | Ptr |
Contextual definition of pointer type. | |
Public Member Functions | |
Socket (SocketType type, SocketDomain domain, SocketBlockingMode blockingMode) | |
Constructs an Socket instance. More... | |
Socket (const Socket &arg)=delete | |
Copy contructor. | |
Socket & | operator= (const Socket &arg)=delete |
Assignment operator. | |
~Socket (void) | |
Destructs this instance and frees all resouces. More... | |
SocketType | GetSocketType (void) const |
Returns the type of the socket. More... | |
SocketDomain | GetSocketDomain (void) const |
Returns the utilized domain. More... | |
bool | IsBlocking (void) const |
Checks if the socket is in blocking mode. More... | |
bool | IsConnected (void) const |
Checks if this socket is connected with a remote peer. More... | |
IpAddress | GetRemoteIpAddress (void) const |
If this socket is connected this method returns the ip-address (v4). More... | |
int | GetRemotePort (void) const |
If this socket is connected this method returns the port of the connection. More... | |
Ptr | Accept (IpAddress &ip4address, int &port, SocketError &error) |
Accepts a pending connection request. More... | |
SocketError | Bind (const IpAddress &ip4Address, int port) |
Binds the socket to a specific address and port combination. More... | |
SocketError | Bind2 (const IpAddress &ip4Address, int &port) |
Binds the socket to a specific address and port combination. More... | |
SocketError | Connect (const IpAddress &ip4Address, int port) |
Tries to connect with a remote socket. More... | |
SocketError | Listen (size_t backlog) |
Marks this socket as a passive socket that accepts incoming connection requests. More... | |
SocketError | Shutdown (void) |
Shuts down a full-duplex connection. More... | |
SocketError | Shutdown (ShutdownMode mode) |
Shuts down a full-duplex connection. More... | |
SocketError | Close (void) |
Closes the socket. This ends all communication on the socket. More... | |
int | Send (const void *pBuffer, size_t length, SocketError &error) |
Transmit data over the socket that is in a connected state. More... | |
int | SendTo (const void *pBuffer, size_t length, IpAddress ip4Adress, int port, SocketError &error) |
Like Send but this method is to be used on sockets created with SocketType::Udp. More... | |
int | Receive (void *pBuffer, size_t length, SocketError &error) |
Reads data from connected socket. More... | |
int | ReceiveFrom (void *pBuffer, size_t length, IpAddress &ip4Adress, int &port, SocketError &error) |
Like Receive but this method is to be used on sockets created with SocketType::Udp. More... | |
bool | Select (SelectMode mode, Microseconds timeout, SocketError &error) |
Checks if an i/o operation can be performed without blocking the calling thread. More... | |
int | Poll (PollMode mode, Milliseconds timeout, SocketError &error) |
Checks if an i/o operation can be processed without blocking. More... | |
SocketError | SetSocketOption (SocketOptionName optionName, const void *optionValue, size_t optionLength) |
Sets a single option on the socket. More... | |
SocketError | GetSocketOption (SocketOptionName optionName, void *optionValue, size_t *optionLength) const |
Returns current value of queried socket option. More... | |
SocketError | SetOptionReuseAddress (bool enabled) |
Enables/Disables the reuse-address option for this socket. More... | |
SocketError | GetOptionReuseAddress (bool &enabled) const |
Checks if reuse-address is enabled. More... | |
SocketError | SetOptionKeepAlive (bool enabled) |
Enables/Disables the keep-alive option for this socket. More... | |
SocketError | GetOptionKeepAlive (bool &enabled) const |
Checks if keep-alive is enabled. More... | |
SocketError | SetOptionBroadcast (bool enabled) |
Enables/Disables broadcast for this socket. More... | |
SocketError | GetOptionBroadcast (bool &enabled) const |
Checks if broadcast is enabled. More... | |
SocketError | SetOptionNoDelay (bool enabled) |
Enables/Disables no-delay for this socket. More... | |
SocketError | GetOptionNoDelay (bool &enabled) const |
Checks if no-delay is enabled. More... | |
SocketError | SetOptionBlocking (bool enable) |
Enables/disables the blocking mode of this socket.. More... | |
SocketError | SetOptionLinger (bool enable, size_t timeout) |
Sets the amount of time a socket resides in TIME_WAIT state after active close. More... | |
SocketError | GetOptionLinger (bool &enable, size_t &timeout) |
Returns the current linger options. For more information see Arp::System::Commons::Net::Socket::SetOptionLinger More... | |
Friends | |
class | TlsSocket |
class | TlsSocket2 |
Interface to realizes ethernet based communications.
Instances of this class can be used to realize an ethernet based communication with remote peers. By now 2 different types of protocols are supported. Either UPD or TCP. A simple TCP echo server can be realized with the following code.
The server uses a single passive socket called listenigSocket that is used to wait for a single connection. Therefore the socket is created during instance creation of the TcpEchoServer-object as a blocking TCP socket that uses Ipv4 protocol. In the Process method the socket is then bound to all local network interfaces and port 45451. Note that from this time on, no other socket can be bound to any address and port 45451 since the socket has enable reusing of addresses. See Arp::System::Commons::Net::Socket::SetOptionReuseAddress for more information. Afterwards the socket is marked as passive socket listening for incoming connection request by invoking Arp::System::Commons::Net::Socket::Listen. Since the socket is a blocking socket the server will block at the accept call until a connection request reaches the socket. Afterwards it justs reads any data of up to 256 bytes and simply returns them to the sender.
A client on the same machine can then connect and communicate with the server using the following code
Arp::System::Commons::Net::Socket::Socket | ( | SocketType | type, |
SocketDomain | domain, | ||
SocketBlockingMode | blockingMode | ||
) |
Constructs an Socket instance.
type | The type of socket to be created. |
domain | The address domain for the socket. Currently only SocketDomain::Ipv4 is supported. |
blockingMode | The blocking mode of the socket. |
Arp::System::Commons::Net::Socket::~Socket | ( | void | ) |
Destructs this instance and frees all resouces.
This will also close the socket if it is not yet closed.
Ptr Arp::System::Commons::Net::Socket::Accept | ( | IpAddress & | ip4address, |
int & | port, | ||
SocketError & | error | ||
) |
Accepts a pending connection request.
Before a socket can accept connection requests, it must be Bind with an address and must be set to Listen state.
If no connection request is pending and the socket is in
Currently only ip v4 addresses are supported. The address must be encoded in a single 32 bit value. For an example how to encode the address see the example in the class description.
ip4address | holds the 32 bit encoded ip v4 address of the connected peer socket. |
port | Holds the port number of the connected peer socket. |
error | Holds error code in case the method returns nullptr. |
SocketError Arp::System::Commons::Net::Socket::Bind | ( | const IpAddress & | ip4Address, |
int | port | ||
) |
Binds the socket to a specific address and port combination.
Currently only ip v4 addresses are supported. The address must be encoded in a single 32 bit value. For an example how to encode the address see the example in the class description.
To bind this socket to all local interfaces, 0 has to be assigned to parameter ip4Address.
ip4Address | IP v4 based address to connect to. |
port | Port number to connect with. |
SocketError Arp::System::Commons::Net::Socket::Bind2 | ( | const IpAddress & | ip4Address, |
int & | port | ||
) |
Binds the socket to a specific address and port combination.
bind2 returns the actual port used by this socket in the port parameter in contrast to bind i.e. use bind(ip, 0) to let the os choose the port to use, after the call port will contain the port choosed by the os.
Currently only ip v4 addresses are supported. The address must be encoded in a single 32 bit value. For an example how to encode the address see the example in the class description.
To bind this socket to all local interfaces, 0 has to be assigned to parameter ip4Address.
ip4Address | IP v4 based address to connect to. |
port | Port number to connect with. |
SocketError Arp::System::Commons::Net::Socket::Close | ( | void | ) |
Closes the socket. This ends all communication on the socket.
After Close() no further calls (i.e. a new connect()) on this socket are possible thus the socket instance must be destroyed.
SocketError Arp::System::Commons::Net::Socket::Connect | ( | const IpAddress & | ip4Address, |
int | port | ||
) |
Tries to connect with a remote socket.
Currently only ip v4 addresses are supported. The address must be encoded in a single 32 bit value. For an example how to encode the address see the example in the class description.
ip4Address | IP v4 based address to connect to. |
port | Port number to connect with. |
SocketError Arp::System::Commons::Net::Socket::GetOptionBroadcast | ( | bool & | enabled | ) | const |
Checks if broadcast is enabled.
See SetOptionBroadcast for more information.
enabled | Container for current option state. |
SocketError Arp::System::Commons::Net::Socket::GetOptionKeepAlive | ( | bool & | enabled | ) | const |
Checks if keep-alive is enabled.
See SetOptionKeepAlive for more information.
enabled | Container for current option state. |
SocketError Arp::System::Commons::Net::Socket::GetOptionLinger | ( | bool & | enable, |
size_t & | timeout | ||
) |
Returns the current linger options. For more information see Arp::System::Commons::Net::Socket::SetOptionLinger
enable | True if linger option is active, otherwise false. |
timeout | Holds current timeout value in seconds if linger is activated on this socket. |
SocketError Arp::System::Commons::Net::Socket::GetOptionNoDelay | ( | bool & | enabled | ) | const |
Checks if no-delay is enabled.
See SetOptionNoDelay for more information.
enabled | Container for current option state. |
SocketError Arp::System::Commons::Net::Socket::GetOptionReuseAddress | ( | bool & | enabled | ) | const |
Checks if reuse-address is enabled.
See SetOptionReuseAddress for more information
enabled | Container for current option state. |
|
inline |
If this socket is connected this method returns the ip-address (v4).
This returned address is encoded in network byte order.
|
inline |
If this socket is connected this method returns the port of the connection.
The port number is returned in host byte order, not in network byte order.
|
inline |
Returns the utilized domain.
SocketError Arp::System::Commons::Net::Socket::GetSocketOption | ( | SocketOptionName | optionName, |
void * | optionValue, | ||
size_t * | optionLength | ||
) | const |
Returns current value of queried socket option.
optionName | Name of option where current value is queried. |
optionValue | Pointer to buffer where current socket option value is to be stored. |
optionLength | Value-Result argument. Must be initialized to byte size of buffer pointed to by optionValue. Holds amount of bytes set in buffer optionValue after call returned. |
|
inline |
Returns the type of the socket.
|
inline |
Checks if the socket is in blocking mode.
|
inline |
Checks if this socket is connected with a remote peer.
SocketError Arp::System::Commons::Net::Socket::Listen | ( | size_t | backlog | ) |
Marks this socket as a passive socket that accepts incoming connection requests.
A socket in listening state features an internal queue where incoming connection requests are stored until they are processed by an Accept invocation. The maximal capacity of this queue is determined by the parameter backlog. If a connection request arrives while the queue is filled, the client may receive an error telling that the connection is refused or, if the utilized protocol allows retransmissions, the request may be ignored so that a consecutive reattempt to connnect may succeed.
backlog | Defines the maximum number of pending connection requests. |
int Arp::System::Commons::Net::Socket::Poll | ( | PollMode | mode, |
Milliseconds | timeout, | ||
SocketError & | error | ||
) |
Checks if an i/o operation can be processed without blocking.
mode | Specifies the i/o operation to be checked. |
timeout | Specifies the timeout to wait for the socket to get ready for the i/o operation. If set to zero, the call will return immediately. Milliseconds(-1) determines infinite. |
error | The resulting socket error, if any error occur. |
1
if the specified i/o operation might be processed without blocking. The error is <cref name="SocketError::ConnectionReset" >, if the socket was closed local or remote. 0
if this operation timed out. -1
if any error occurs. Check the error to get more infos about the error. int Arp::System::Commons::Net::Socket::Receive | ( | void * | pBuffer, |
size_t | length, | ||
SocketError & | error | ||
) |
Reads data from connected socket.
pBuffer | Pointer to buffer where read data can be stored. |
length | Capacity in bytes of buffer pBuffer. |
error | Container variable holding error code after call returned. |
int Arp::System::Commons::Net::Socket::ReceiveFrom | ( | void * | pBuffer, |
size_t | length, | ||
IpAddress & | ip4Adress, | ||
int & | port, | ||
SocketError & | error | ||
) |
Like Receive but this method is to be used on sockets created with SocketType::Udp.
pBuffer | Pointer to buffer where read data can be stored. |
length | Capacity in bytes of buffer pBuffer. |
ip4Adress | Source address (ip v4 only) of received data. |
port | Source port number of received data. |
error | Container variable holding error code after call returned. |
bool Arp::System::Commons::Net::Socket::Select | ( | SelectMode | mode, |
Microseconds | timeout, | ||
SocketError & | error | ||
) |
Checks if an i/o operation can be performed without blocking the calling thread.
mode | Determines the i/o operation to be checked. |
timeout | Specifies the time to wait for the socket to be ready for the i/o operation. If set to zero, the call will return immediately. |
error | Container variable holding error code after call returned. |
true
if the specified i/o operation can be performed without blocking, otherwise false is returned. true
is also returned, when the socket was closed local or remote (Linux socket API behaviour!). The only way to determine if the socket is still alive, is through a subsequent call of <cref name="Socket::Receive">. This operation is a legacy operation, use <cref name="Socket::Poll"> instead.
int Arp::System::Commons::Net::Socket::Send | ( | const void * | pBuffer, |
size_t | length, | ||
SocketError & | error | ||
) |
Transmit data over the socket that is in a connected state.
pBuffer | Pointer to buffer containing serialized data to be send. |
length | Amount of bytes to be send. |
error | Container variable holding error code after call returned. |
int Arp::System::Commons::Net::Socket::SendTo | ( | const void * | pBuffer, |
size_t | length, | ||
IpAddress | ip4Adress, | ||
int | port, | ||
SocketError & | error | ||
) |
Like Send but this method is to be used on sockets created with SocketType::Udp.
pBuffer | Pointer to buffer containing serialized data to be send. |
length | Amount of bytes to be send. |
ip4Adress | Destination address (ip v4 only) for data. |
port | Destination port number for data. |
error | Container variable holding error code after call returned. |
SocketError Arp::System::Commons::Net::Socket::SetOptionBlocking | ( | bool | enable | ) |
Enables/disables the blocking mode of this socket..
enable | true if the blocking mode shall be enabled, otherwise false . |
SocketError Arp::System::Commons::Net::Socket::SetOptionBroadcast | ( | bool | enabled | ) |
Enables/Disables broadcast for this socket.
This option is only valid for datagram sockets and can be used to enable the sending of packets to broadcast addresses.
enabled | Set to tro true activate option, otherwise set to false. |
SocketError Arp::System::Commons::Net::Socket::SetOptionKeepAlive | ( | bool | enabled | ) |
Enables/Disables the keep-alive option for this socket.
Enables the sending of keep-alive probes to remote peers to check if a connection is still up if no data was transmitted for a while.
This option is only valid for connection-based sockets.
enabled | Set to true to activate option, otherwise set to false. |
SocketError Arp::System::Commons::Net::Socket::SetOptionLinger | ( | bool | enable, |
size_t | timeout | ||
) |
Sets the amount of time a socket resides in TIME_WAIT state after active close.
The port of a connection that is activly closed, i.e. first calls Arp::System::Commons::Net::Socket::Close will reside in a state called TIME_WAIT for a specific amount of time. This prevents subsequently delivered packages to be considered to belong to a potential new established connection using the same port.
The time a port is in TIME_WAIT state can be adjusted using this method. A smaller timeout can be of interest if the system has to manage a lot of connections that only are active for a small amount of time. In this case a small timeout will prevent a lot of socket residing in TIME_WAIT and are therefore unavailable for creation of new connections.
enable | Set to true to activate new linger timeout, false otherwise. |
timeout | The new linger timeout in seconds. |
SocketError Arp::System::Commons::Net::Socket::SetOptionNoDelay | ( | bool | enabled | ) |
Enables/Disables no-delay for this socket.
If this option is enabled packets are not buffered by the TCP stack stack to optimize the network usage by only sending sufficiently large amount of data. Instead even small packets are send immediately over the network.
enabled | Set to tro true activate option, otherwise set to false. |
SocketError Arp::System::Commons::Net::Socket::SetOptionReuseAddress | ( | bool | enabled | ) |
Enables/Disables the reuse-address option for this socket.
Activating this option changes the validation process for socket bindings. If the option is disabled and one port is for example bound to 0.0.0.0:21 (any local address and port 21) another port can not be bound to 192.168.0.4:21 because the wildcard IP address 0.0.0.0 conflicts with the other address. If the option is enabled, bounding the second socket will succeed.
enabled | Set to true to enable the option, otherwise set to false. |
SocketError Arp::System::Commons::Net::Socket::SetSocketOption | ( | SocketOptionName | optionName, |
const void * | optionValue, | ||
size_t | optionLength | ||
) |
Sets a single option on the socket.
optionName | Name of option ot be set. |
optionValue | Pointer to additional data for socket option. |
optionLength | Byte size of buffer pointed to by optionValue. |
SocketError Arp::System::Commons::Net::Socket::Shutdown | ( | ShutdownMode | mode | ) |
Shuts down a full-duplex connection.
After the shutdown has been called, further reception and transmission on the socket is disallowed.
mode | Specifies if read or write operations should be shutdown. |
SocketError Arp::System::Commons::Net::Socket::Shutdown | ( | void | ) |
Shuts down a full-duplex connection.
After the shutdown has been called, further reception and transmission on the socket is disallowed.