PLCnext API Documentation 25.0.2.69
|
Socket for interprocess communication between processes on the same controller. More...
#include <IpcSocket.hpp>
Public Types | |
typedef std::shared_ptr< IpcSocket > | Ptr |
Contextual definition of pointer type. | |
using | SelectMode = Arp::System::Commons::Net::SelectMode |
Contextual definition of available select modes. | |
using | PollMode = Arp::System::Commons::Net::PollMode |
Contextual definition of available poll modes. | |
using | ShutdownMode = Arp::System::Commons::Net::ShutdownMode |
Contextual definition of available poll modes. | |
Public Member Functions | |
IpcSocket (bool blocking=true) | |
Constructs a new socket for interprocess communication. More... | |
IpcSocket (const IpcSocket &arg)=delete | |
Copy constructor is deleted. | |
IpcSocket (IpcSocket &&arg)=default | |
Move constructor. | |
IpcSocket & | operator= (const IpcSocket &arg)=delete |
Assignment operator is deleted. | |
~IpcSocket (void) | |
Closes the socket and releases all ressources. | |
IpcSocketError | Bind (const Arp::String &path) |
Assigns an address to this socket so that other process can connect with it. More... | |
IpcSocketError | Listen (size_t backlog) |
Marks this socket as a passive socket that accepts incoming connection requests. More... | |
IpcSocketError | Connect (const Arp::String &path) |
Tries to connect with a remote socket. More... | |
bool | Select (SelectMode mode, Microseconds timeout, IpcSocketError &error) |
Checks if an i/o operation can be performed without blocking the calling thread. More... | |
int | Poll (PollMode mode, Milliseconds timeout, IpcSocketError &error) |
Checks if an i/o operation can be processed without blocking. More... | |
Ptr | Accept (IpcSocketError &error) |
Accepts a pending connection request. More... | |
IpcSocketError | Shutdown (void) |
Shuts down a connection. More... | |
IpcSocketError | Shutdown (ShutdownMode mode) |
Shuts down a connection. More... | |
IpcSocketError | Close (void) |
Closes the socket. This ends all communication on the socket. More... | |
int | Send (const void *pBuffer, size_t length, IpcSocketError &error) |
Transmit data over the socket that is in a connected state. More... | |
int | Receive (void *pBuffer, size_t length, IpcSocketError &error) |
Reads data from connected socket. More... | |
PeerCredentials | GetPeerCredentials (IpcSocketError &error) |
Reads peer credentials from connected socket. More... | |
Socket for interprocess communication between processes on the same controller.
In contrast to instances of Arp::System::Commons::Net::Socket instances of this class can only be used to perform communication between processes on the same controller. The benefit of using an IpcSocket instead of a common Socket is that the API remains the same but the underlying mechanism is optimized for speed and performance by not using a complete network stack like TCP/IP and is thus much more lightweight.
IpcSockets can be used in a similar manner as normal TCP-sockets. A server needs a socket listening for incoming connection requests of clients. If such a request is processed a new socket instance is created that provides an endport for the communication between the server process and the client process. Server and client can then exchange data using binary read and write calls.
Arp::System::Commons::Ipc::IpcSocket::IpcSocket | ( | bool | blocking = true | ) |
Constructs a new socket for interprocess communication.
blocking | Indicates if calls to accept and receive block until data is available or not. |
IpcSocket::Ptr Arp::System::Commons::Ipc::IpcSocket::Accept | ( | IpcSocketError & | errorVal | ) |
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.
error | Holds error code in case the method returns nullptr. |
IpcSocketError Arp::System::Commons::Ipc::IpcSocket::Bind | ( | const Arp::String & | path | ) |
Assigns an address to this socket so that other process can connect with it.
path | to connect to. |
IpcSocketError Arp::System::Commons::Ipc::IpcSocket::Close | ( | void | ) |
Closes the socket. This ends all communication on the socket.
If this socket is bound to a file the file will be removed.
IpcSocketError Arp::System::Commons::Ipc::IpcSocket::Connect | ( | const Arp::String & | path | ) |
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.
path | Name of socket to connect with. |
PeerCredentials Arp::System::Commons::Ipc::IpcSocket::GetPeerCredentials | ( | IpcSocketError & | error | ) |
Reads peer credentials from connected socket.
error | Container variable holding error code after call returned. |
IpcSocketError Arp::System::Commons::Ipc::IpcSocket::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::Ipc::IpcSocket::Poll | ( | PollMode | mode, |
Milliseconds | timeout, | ||
IpcSocketError & | 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. 0
if this operation timed out. -1
if any error occurs. Check the error to get more infos about the error. If the socket was closed local or remote, the error is <cref name="SocketError::ConnectionReset" >. int Arp::System::Commons::Ipc::IpcSocket::Receive | ( | void * | pBuffer, |
size_t | length, | ||
IpcSocketError & | 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. |
bool Arp::System::Commons::Ipc::IpcSocket::Select | ( | SelectMode | mode, |
Microseconds | timeout, | ||
IpcSocketError & | 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. |
int Arp::System::Commons::Ipc::IpcSocket::Send | ( | const void * | pBuffer, |
size_t | length, | ||
IpcSocketError & | 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. |
IpcSocketError Arp::System::Commons::Ipc::IpcSocket::Shutdown | ( | ShutdownMode | mode | ) |
Shuts down a connection.
After the shutdown has been called, further reception and transmission on the socket is disallowed.
IpcSocketError Arp::System::Commons::Ipc::IpcSocket::Shutdown | ( | void | ) |
Shuts down a connection.
After the shutdown has been called, further reception and transmission on the socket is disallowed.