PLCnext API Documentation  22.6.0.43
Socket.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
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
18 namespace Arp { namespace System { namespace Ve
19 {
20 class ISocketService;
21 }}}
22 
23 namespace Arp { namespace System { namespace Commons { namespace Net
24 {
25 
28 {
30  None = 0,
32  Blocking = 1,
34  NoneBlocking = 2
35 };
36 
117 class Socket
118 {
119 public: // typedefs/usings
120 
122  typedef std::shared_ptr<Socket> Ptr;
123 
124 public: // friends
125  friend class TlsSocket;
126 
127 public: // construction/destruction
128 
134  Socket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode);
135 
137  Socket(const Socket& arg) = delete;
138 
140  Socket& operator=(const Socket& arg) = delete;
141 
145  ~Socket(void);
146 
147 public: // operators
148 
149 public: // static operations
150 
151 public: // setter/getter operations
152 
156  SocketType GetSocketType(void) const;
157 
161  SocketDomain GetSocketDomain(void) const;
162 
166  bool IsBlocking(void) const;
167 
171  bool IsConnected(void) const;
172 
178  IpAddress GetRemoteIpAddress(void) const;
179 
185  int GetRemotePort(void) const;
186 
187 public: // operations
188 
206  Ptr Accept(IpAddress& ip4address, int& port, SocketError& error);
207 
219  SocketError Bind(const IpAddress& ip4Address, int port);
220 
235  SocketError Bind2(const IpAddress& ip4Address, int& port);
236 
246  SocketError Connect(const IpAddress& ip4Address, int port);
247 
259  SocketError Listen(size_t backlog);
260 
267 
273 
280 
288  int Send(const void *pBuffer, size_t length, SocketError& error);
289 
299  int SendTo(const void *pBuffer, size_t length, IpAddress ip4Adress, int port, SocketError& error);
300 
308  int Receive(void *pBuffer, size_t length, SocketError& error);
309 
319  int ReceiveFrom(void *pBuffer, size_t length, IpAddress& ip4Adress, int& port, SocketError& error);
320 
333  bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
334 
348  int Poll(PollMode mode, Milliseconds timeout, SocketError& error);
349 
357  SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
358 
367  SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t *optionLength) const;
368 
380 
388  SocketError GetOptionReuseAddress(bool& enabled) const;
389 
401 
409  SocketError GetOptionKeepAlive(bool& enabled) const;
410 
420 
428  SocketError GetOptionBroadcast(bool& enabled) const;
429 
440 
448  SocketError GetOptionNoDelay(bool& enabled) const;
449 
454 
470  SocketError SetOptionLinger(bool enable, size_t timeout);
471 
478  SocketError GetOptionLinger(bool& enable, size_t& timeout);
479 
480 protected: // operations
481 
482 private: // usings/typedefs
483 
484  using ISocketService = Arp::System::Ve::ISocketService;
485 
486 private: // construction/destruction
487 
488  Socket(ISocketService *pSocket); //contruct a new Socket Object from a given ISocketService
489 
490 private: // static methods
491 
492 private: // methods
493 
494 private: // fields
495  ISocketService *pSocketService;
496 
497  SocketType socketType;
498  SocketDomain socketDomain;
499  bool isBlocking;
500  IpAddress remoteIpAddress;
501  int remotePort;
502  bool isConnected;
503 
504 private: // static fields
505 
506 };
507 
509 // inline methods of class Socket
510 
512 {
513  return socketType;
514 }
515 
517 {
518  return socketDomain;
519 }
520 
521 inline bool Socket::IsBlocking(void) const
522 {
523  return isBlocking;
524 }
525 
527 {
528  return remoteIpAddress;
529 }
530 
531 inline int Socket::GetRemotePort(void) const
532 {
533  return remotePort;
534 }
535 
536 inline bool Socket::IsConnected(void) const
537 {
538  return this->isConnected;
539 }
540 
541 }}}} // 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:511
SocketError Shutdown(void)
Shuts down a full-duplex connection.
SocketDomain GetSocketDomain(void) const
Returns the utilized domain.
Definition: Socket.hpp:516
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.
int Receive(void *pBuffer, size_t length, SocketError &error)
Reads data from connected socket.
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.
Socket & operator=(const Socket &arg)=delete
Assignment operator.
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 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:526
SocketError SetOptionBlocking(bool enable)
Enables/disables the blocking mode of this socket..
SocketError Shutdown(ShutdownMode mode)
Shuts down a full-duplex connection.
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 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:531
bool IsBlocking(void) const
Checks if the socket is in blocking mode.
Definition: Socket.hpp:521
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:536
std::shared_ptr< Socket > Ptr
Contextual definition of pointer type.
Definition: Socket.hpp:122
SocketError SetOptionBroadcast(bool enabled)
Enables/Disables broadcast for this socket.
Interface to realize TLS Connection over TCP
Definition: TlsSocket.hpp:32
std::chrono::milliseconds Milliseconds
The Arp Milliseconds unit class.
Definition: TypeSystem.h:36
std::chrono::microseconds Microseconds
The Arp Microseconds unit class.
Definition: TypeSystem.h:34
@ 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