PLCnext API Documentation 24.6.0.58
Socket.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Commons/Net/IpAddress.hpp"
9#include "Arp/System/Commons/Net/SocketType.hpp"
10#include "Arp/System/Commons/Net/PollMode.hpp"
11#include "Arp/System/Commons/Net/SelectMode.hpp"
12#include "Arp/System/Commons/Net/ShutdownMode.hpp"
13#include "Arp/System/Commons/Net/SocketError.hpp"
14#include "Arp/System/Commons/Net/SocketDomain.hpp"
15#include "Arp/System/Commons/Net/SocketOptionName.hpp"
16
17// forwards
18namespace Arp { namespace System { namespace Ve
19{
20class ISocketService;
21}}}
22
23namespace Arp { namespace System { namespace Commons { namespace Net
24{
25
28{
30 None = 0,
32 Blocking = 1,
34 NoneBlocking = 2
35};
36
118{
119public: // typedefs/usings
120
122 typedef std::shared_ptr<Socket> Ptr;
123
124public: // friends
125 friend class TlsSocket;
126 friend class TlsSocket2;
127
128public: // construction/destruction
129
136
138 Socket(const Socket& arg) = delete;
139
141 Socket& operator=(const Socket& arg) = delete;
142
146 ~Socket(void);
147
148public: // operators
149
150public: // static operations
151
152public: // setter/getter operations
153
157 SocketType GetSocketType(void) const;
158
162 SocketDomain GetSocketDomain(void) const;
163
167 bool IsBlocking(void) const;
168
172 bool IsConnected(void) const;
173
179 IpAddress GetRemoteIpAddress(void) const;
180
186 int GetRemotePort(void) const;
187
188public: // operations
189
207 Ptr Accept(IpAddress& ip4address, int& port, SocketError& error);
208
220 SocketError Bind(const IpAddress& ip4Address, int port);
221
236 SocketError Bind2(const IpAddress& ip4Address, int& port);
237
247 SocketError Connect(const IpAddress& ip4Address, int port);
248
260 SocketError Listen(size_t backlog);
261
268
274
281
289 int Send(const void *pBuffer, size_t length, SocketError& error);
290
300 int SendTo(const void *pBuffer, size_t length, IpAddress ip4Adress, int port, SocketError& error);
301
309 int Receive(void *pBuffer, size_t length, SocketError& error);
310
320 int ReceiveFrom(void *pBuffer, size_t length, IpAddress& ip4Adress, int& port, SocketError& error);
321
334 bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
335
349 int Poll(PollMode mode, Milliseconds timeout, SocketError& error);
350
358 SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
359
368 SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t *optionLength) const;
369
381
389 SocketError GetOptionReuseAddress(bool& enabled) const;
390
402
410 SocketError GetOptionKeepAlive(bool& enabled) const;
411
421
429 SocketError GetOptionBroadcast(bool& enabled) const;
430
441
449 SocketError GetOptionNoDelay(bool& enabled) const;
450
455
471 SocketError SetOptionLinger(bool enable, size_t timeout);
472
479 SocketError GetOptionLinger(bool& enable, size_t& timeout);
480
501
511
520
530
539
549
558
568
569
570protected: // operations
571
572private: // usings/typedefs
573
575
576private: // construction/destruction
577
578 Socket(ISocketService *pSocket); //contruct a new Socket Object from a given ISocketService
579
580private: // static methods
581
582private: // methods
583
584private: // fields
585 ISocketService *pSocketService;
586
587 SocketType socketType;
588 SocketDomain socketDomain;
589 bool isBlocking;
590 IpAddress remoteIpAddress;
591 int remotePort;
592 bool isConnected;
593
594private: // static fields
595
596};
597
599// inline methods of class Socket
600
602{
603 return socketType;
604}
605
607{
608 return socketDomain;
609}
610
611inline bool Socket::IsBlocking(void) const
612{
613 return isBlocking;
614}
615
617{
618 return remoteIpAddress;
619}
620
621inline int Socket::GetRemotePort(void) const
622{
623 return remotePort;
624}
625
626inline bool Socket::IsConnected(void) const
627{
628 return this->isConnected;
629}
630
631}}}} // end of namespace Arp::System::Commons::Net
Unified representation for ip address schemes.
Definition: IpAddress.hpp:14
Interface to realizes ethernet based communications.
Definition: Socket.hpp:118
SocketType GetSocketType(void) const
Returns the type of the socket.
Definition: Socket.hpp:601
SocketError Shutdown(void)
Shuts down a full-duplex connection.
SocketDomain GetSocketDomain(void) const
Returns the utilized domain.
Definition: Socket.hpp:606
int Send(const void *pBuffer, size_t length, SocketError &error)
Transmit data over the socket that is in a connected state.
SocketError SetSocketOption(SocketOptionName optionName, const void *optionValue, size_t optionLength)
Sets a single option on the socket.
SocketError SetOptionKeepAliveProbeInterval(int seconds)
Sets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
SocketError GetOptionKeepAliveProbeInterval(int &seconds)
Gets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
int Receive(void *pBuffer, size_t length, SocketError &error)
Reads data from connected socket.
Socket & operator=(const Socket &arg)=delete
Assignment operator.
SocketError Close(void)
Closes the socket. This ends all communication on the socket.
int Poll(PollMode mode, Milliseconds timeout, SocketError &error)
Checks if an i/o operation can be processed without blocking.
SocketError SetOptionLinger(bool enable, size_t timeout)
Sets the amount of time a socket resides in TIME_WAIT state after active close.
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.
SocketError SetOptionNoDelay(bool enabled)
Enables/Disables no-delay for this socket.
Socket(const Socket &arg)=delete
Copy contructor.
Ptr Accept(IpAddress &ip4address, int &port, SocketError &error)
Accepts a pending connection request.
Socket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode)
Constructs an Socket instance.
SocketError GetOptionKeepAlive(bool &enabled) const
Checks if keep-alive is enabled.
SocketError GetOptionKeepAliveIdleTime(int &seconds)
Gets the time that the socket needs to be idle for, before the Keep Alive mechanism can start if the ...
SocketError GetOptionBroadcast(bool &enabled) const
Checks if broadcast is enabled.
IpAddress GetRemoteIpAddress(void) const
If this socket is connected this method returns the ip-address (v4).
Definition: Socket.hpp:616
SocketError SetOptionUserTimeout(size_t timeout_ms)
Sets the retransmission timeout of a socket. This only works on Linux.
SocketError SetOptionBlocking(bool enable)
Enables/disables the blocking mode of this socket..
SocketError SetOptionKeepAliveIdleTime(int seconds)
Sets the time that the socket needs to be idle for, before the Keep Alive mechanism can start if the ...
SocketError Shutdown(ShutdownMode mode)
Shuts down a full-duplex connection.
SocketError SetOptionKeepAliveProbeCount(int probeCount)
Sets the amount of probes to be sent, if the KeepAlive SocketOption is set to true.
SocketError GetOptionNoDelay(bool &enabled) const
Checks if no-delay is enabled.
SocketError Bind(const IpAddress &ip4Address, int port)
Binds the socket to a specific address and port combination.
SocketError GetSocketOption(SocketOptionName optionName, void *optionValue, size_t *optionLength) const
Returns current value of queried socket option.
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.
bool Select(SelectMode mode, Microseconds timeout, SocketError &error)
Checks if an i/o operation can be performed without blocking the calling thread.
~Socket(void)
Destructs this instance and frees all resouces.
SocketError GetOptionKeepAliveProbeCount(int &probeCount)
Gets the amount of probes to be sent, if the KeepAlive SocketOption is set to true....
SocketError GetOptionReuseAddress(bool &enabled) const
Checks if reuse-address is enabled.
SocketError GetOptionLinger(bool &enable, size_t &timeout)
Returns the current linger options. For more information see Arp::System::Commons::Net::Socket::SetOp...
SocketError Connect(const IpAddress &ip4Address, int port)
Tries to connect with a remote socket.
SocketError Bind2(const IpAddress &ip4Address, int &port)
Binds the socket to a specific address and port combination.
int GetRemotePort(void) const
If this socket is connected this method returns the port of the connection.
Definition: Socket.hpp:621
bool IsBlocking(void) const
Checks if the socket is in blocking mode.
Definition: Socket.hpp:611
SocketError SetOptionReuseAddress(bool enabled)
Enables/Disables the reuse-address option for this socket.
SocketError SetOptionKeepAlive(bool enabled)
Enables/Disables the keep-alive option for this socket.
SocketError Listen(size_t backlog)
Marks this socket as a passive socket that accepts incoming connection requests.
bool IsConnected(void) const
Checks if this socket is connected with a remote peer.
Definition: Socket.hpp:626
std::shared_ptr< Socket > Ptr
Contextual definition of pointer type.
Definition: Socket.hpp:122
SocketError SetOptionBroadcast(bool enabled)
Enables/Disables broadcast for this socket.
SocketError GetOptionUserTimeout(size_t &timeout_ms)
Gets the retransmission timeout of a socket. This only works on Linux. For more information see Arp::...
Interface to realize TLS Connection over TCP
Definition: TlsSocket2.hpp:34
Definition: TlsSocket.hpp:40
Definition: ISocketService.hpp:13
std::chrono::milliseconds Milliseconds
The Arp Milliseconds unit class.
Definition: TypeSystem.h:52
std::chrono::microseconds Microseconds
The Arp Microseconds unit class.
Definition: TypeSystem.h:49
@ System
System components used by the System, Device, Plc or Io domains.
PollMode
This enum is used to specifiy the poll mode of the <cref name="Socket::Poll" > operation.
Definition: PollMode.hpp:15
SelectMode
Modes for Select call to check different data channels.
Definition: SelectMode.hpp:14
SocketDomain
Supported communication domains, selecting the protocol for communication.
Definition: SocketDomain.hpp:14
SocketType
Enumeration of supported socket types.
Definition: SocketType.hpp:14
ShutdownMode
This enum is used to specifiy the shutdown mode of the <cref name="Socket::Shutdown(ShutdownMode)" > ...
Definition: ShutdownMode.hpp:15
SocketError
Possible error codes for socket operation results.
Definition: SocketError.hpp:15
SocketOptionName
Specifies socket options to be set by the application. Copied from Eclr Socket Adaption
Definition: SocketOptionName.hpp:17
SocketBlockingMode
Supported blocking modes.
Definition: Socket.hpp:28
@ Blocking
Socket is in blocking mode, i.e. the Send*, Receive* and Accept methods will block if no data is avai...
@ NoneBlocking
Socket is in non-blocking mode, i.e. the Send*, Receive* and Accept methods will not block if no data...
Root namespace for the PLCnext API