PLCnext API Documentation  21.0.0.35466
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/SelectMode.hpp"
11 #include "Arp/System/Commons/Net/SocketError.hpp"
12 #include "Arp/System/Commons/Net/SocketDomain.hpp"
13 #include "Arp/System/Commons/Net/SocketOptionName.hpp"
14 
15 // forwards
16 namespace Arp { namespace System { namespace Ve
17 {
18 class ISocketService;
19 }}}
20 
21 namespace Arp { namespace System { namespace Commons { namespace Net
22 {
23 
26 {
28  None = 0,
30  Blocking = 1,
32  NoneBlocking = 2
33 };
34 
115 class Socket
116 {
117 public: // typedefs/usings
118 
120  typedef std::shared_ptr<Socket> Ptr;
121 
122 public: // friends
123  friend class TlsSocket;
124 
125 public: // construction/destruction
126 
132  Socket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode);
133 
135  Socket(const Socket& arg) = delete;
136 
138  Socket& operator=(const Socket& arg) = delete;
139 
143  ~Socket(void);
144 
145 public: // operators
146 
147 public: // static operations
148 
149 public: // setter/getter operations
150 
154  SocketType GetSocketType(void) const;
155 
159  SocketDomain GetSocketDomain(void) const;
160 
164  bool IsBlocking(void) const;
165 
169  bool IsConnected(void) const;
170 
176  IpAddress GetRemoteIpAddress(void) const;
177 
183  int GetRemotePort(void) const;
184 
185 public: // operations
186 
204  Ptr Accept(IpAddress& ip4address, int& port, SocketError& error);
205 
217  SocketError Bind(const IpAddress& ip4Address, int port);
218 
233  SocketError Bind2(const IpAddress& ip4Address, int& port);
234 
244  SocketError Connect(const IpAddress& ip4Address, int port);
245 
257  SocketError Listen(size_t backlog);
258 
264  SocketError Shutdown(void);
265 
269  SocketError Close(void);
270 
278  int Send(const void *pBuffer, size_t length, SocketError& error);
279 
289  int SendTo(const void *pBuffer, size_t length, IpAddress ip4Adress, int port, SocketError& error);
290 
298  int Receive(void *pBuffer, size_t length, SocketError& error);
299 
309  int ReceiveFrom(void *pBuffer, size_t length, IpAddress& ip4Adress, int& port, SocketError& error);
310 
319  bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
320 
328  SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
329 
338  SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t *optionLength) const;
339 
350  SocketError SetOptionReuseAddress(bool enabled);
351 
359  SocketError GetOptionReuseAddress(bool& enabled) const;
360 
371  SocketError SetOptionKeepAlive(bool enabled);
372 
380  SocketError GetOptionKeepAlive(bool& enabled) const;
381 
390  SocketError SetOptionBroadcast(bool enabled);
391 
399  SocketError GetOptionBroadcast(bool& enabled) const;
400 
410  SocketError SetOptionNoDelay(bool enabled);
411 
419  SocketError GetOptionNoDelay(bool& enabled) const;
420 
424  SocketError SetOptionBlocking(bool enable);
425 
441  SocketError SetOptionLinger(bool enable, size_t timeout);
442 
449  SocketError GetOptionLinger(bool& enable, size_t& timeout);
450 
451 protected: // operations
452 
453 private: // usings/typedefs
454 
455  using ISocketService = Arp::System::Ve::ISocketService;
456 
457 private: // construction/destruction
458 
459  Socket(ISocketService *pSocket); //contruct a new Socket Object from a given ISocketService
460 
461 private: // static methods
462 
463 private: // methods
464 
465 private: // fields
466  ISocketService *pSocketService;
467 
468  SocketType socketType;
469  SocketDomain socketDomain;
470  bool isBlocking;
471  IpAddress remoteIpAddress;
472  int remotePort;
473  bool isConnected;
474 
475 private: // static fields
476 
477 };
478 
480 // inline methods of class Socket
481 
482 inline SocketType Socket::GetSocketType(void) const
483 {
484  return socketType;
485 }
486 
487 inline SocketDomain Socket::GetSocketDomain(void) const
488 {
489  return socketDomain;
490 }
491 
492 inline bool Socket::IsBlocking(void) const
493 {
494  return isBlocking;
495 }
496 
497 inline IpAddress Socket::GetRemoteIpAddress(void) const
498 {
499  return remoteIpAddress;
500 }
501 
502 inline int Socket::GetRemotePort(void) const
503 {
504  return remotePort;
505 }
506 
507 inline bool Socket::IsConnected(void) const
508 {
509  return this->isConnected;
510 }
511 
512 }}}} // end of namespace Arp::System::Commons::Net
SocketType
Enumeration of supported socket types.
Definition: SocketType.hpp:13
SocketError
Possible error codes for socket operation results.
Definition: SocketError.hpp:14
Check if a connect request can be performed.
SocketOptionName
Specifies socket options to be set by the application. Copied from Eclr Socket Adaption ...
Definition: SocketOptionName.hpp:16
A connect request was made on an already connected socket.
Interface to realizes ethernet based communications.
Definition: Socket.hpp:115
Socket is in blocking mode, i.e. the Send*, Receive* and Accept methods will block if no data is avai...
std::chrono::microseconds Microseconds
The Arp Microseconds unit class.
Definition: TypeSystem.h:34
std::shared_ptr< Socket > Ptr
Contextual definition of pointer type.
Definition: Socket.hpp:120
SocketDomain
Supported communication domains, selecting the protocol for communication.
Definition: SocketDomain.hpp:13
Root namespace for the PLCnext API
SocketBlockingMode
Supported blocking modes.
Definition: Socket.hpp:25
Check if a connection request is pending.
A request to send or receive data was disallowed because the socket had already been shut down in tha...
SelectMode
Modes for Select call to check different data channels.
Definition: SelectMode.hpp:13
Unified representation for ip address schemes.
Definition: IpAddress.hpp:13
System components used by the System, Device, Plc or Io domains.
Socket is in non-blocking mode, i.e. the Send*, Receive* and Accept methods will not block if no data...
Interface to realize TLS Connection over TCP
Definition: TlsSocket.hpp:31