PLCnext API Documentation 25.0.2.69
Public Types | Public Member Functions | List of all members
Arp::System::Commons::Ipc::IpcSocket Class Reference

Socket for interprocess communication between processes on the same controller. More...

#include <IpcSocket.hpp>

Public Types

typedef std::shared_ptr< IpcSocketPtr
 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.
 
IpcSocketoperator= (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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ IpcSocket()

Arp::System::Commons::Ipc::IpcSocket::IpcSocket ( bool  blocking = true)

Constructs a new socket for interprocess communication.

Parameters
blockingIndicates if calls to accept and receive block until data is available or not.

Member Function Documentation

◆ Accept()

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

  • blocking mode, the call blocks until a remote peer sends a connection request
  • non-blocking mode, the call returns a nullptr and error is set to #SocketError::WouldBlock

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.

Parameters
errorHolds error code in case the method returns nullptr.
Returns
Pointer to class instance representing accepted socket, nullptr otherwise.

◆ Bind()

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.

Parameters
pathto connect to.
Returns
See Arp::System::Commons::Ipc::IpcSocketError for more information on possible return values.

◆ Close()

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.

Returns
See Arp::System::Commons::Ipc::IpcSocketError more information on possible return values.

◆ Connect()

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.

Parameters
pathName of socket to connect with.
Returns
See #SocketError for more information on possible return values.

◆ GetPeerCredentials()

PeerCredentials Arp::System::Commons::Ipc::IpcSocket::GetPeerCredentials ( IpcSocketError error)

Reads peer credentials from connected socket.

Parameters
errorContainer variable holding error code after call returned.
Returns
Struct filled with peer credentials, -1 in case of failure.

◆ Listen()

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.

Parameters
backlogDefines the maximum number of pending connection requests.
Returns
See Arp::System::Commons::Ipc::IpcSocketError for more information on possible return values.

◆ Poll()

int Arp::System::Commons::Ipc::IpcSocket::Poll ( PollMode  mode,
Milliseconds  timeout,
IpcSocketError error 
)

Checks if an i/o operation can be processed without blocking.

Parameters
modeSpecifies the i/o operation to be checked.
timeoutSpecifies 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.
errorThe resulting socket error, if any error occur.
Returns
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" >.

◆ Receive()

int Arp::System::Commons::Ipc::IpcSocket::Receive ( void *  pBuffer,
size_t  length,
IpcSocketError error 
)

Reads data from connected socket.

Parameters
pBufferPointer to buffer where read data can be stored.
lengthCapacity in bytes of buffer pBuffer.
errorContainer variable holding error code after call returned.
Returns
Amount of bytes read from socket, -1 in case of failure.

◆ Select()

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.

Parameters
modeDetermines the i/o operation to be checked.
timeoutSpecifies the time to wait for the socket to be ready for the i/o operation. If set to zero, the call will return immediately.
errorContainer variable holding error code after call returned.
Returns
True if the specified i/o operation can be performed without blocking, otherwise false is returned.

◆ Send()

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.

Parameters
pBufferPointer to buffer containing serialized data to be send.
lengthAmount of bytes to be send.
errorContainer variable holding error code after call returned.
Returns
Amount of transmitted bytes, -1 in case of failure.

◆ Shutdown() [1/2]

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.

Returns
See Arp::System::Commons::Ipc::IpcSocketError for more information on possible return values.

◆ Shutdown() [2/2]

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.

Returns
See Arp::System::Commons::Ipc::IpcSocketError for more information on possible return values.

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