PLCnext API Documentation  21.9.0.40
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 
271  SocketError Close(void);
272 
280  int Send(const void *pBuffer, size_t length, SocketError& error);
281 
291  int SendTo(const void *pBuffer, size_t length, IpAddress ip4Adress, int port, SocketError& error);
292 
300  int Receive(void *pBuffer, size_t length, SocketError& error);
301 
311  int ReceiveFrom(void *pBuffer, size_t length, IpAddress& ip4Adress, int& port, SocketError& error);
312 
321  bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
322 
330  SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
331 
340  SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t *optionLength) const;
341 
352  SocketError SetOptionReuseAddress(bool enabled);
353 
361  SocketError GetOptionReuseAddress(bool& enabled) const;
362 
373  SocketError SetOptionKeepAlive(bool enabled);
374 
382  SocketError GetOptionKeepAlive(bool& enabled) const;
383 
392  SocketError SetOptionBroadcast(bool enabled);
393 
401  SocketError GetOptionBroadcast(bool& enabled) const;
402 
412  SocketError SetOptionNoDelay(bool enabled);
413 
421  SocketError GetOptionNoDelay(bool& enabled) const;
422 
426  SocketError SetOptionBlocking(bool enable);
427 
443  SocketError SetOptionLinger(bool enable, size_t timeout);
444 
451  SocketError GetOptionLinger(bool& enable, size_t& timeout);
452 
453 protected: // operations
454 
455 private: // usings/typedefs
456 
457  using ISocketService = Arp::System::Ve::ISocketService;
458 
459 private: // construction/destruction
460 
461  Socket(ISocketService *pSocket); //contruct a new Socket Object from a given ISocketService
462 
463 private: // static methods
464 
465 private: // methods
466 
467 private: // fields
468  ISocketService *pSocketService;
469 
470  SocketType socketType;
471  SocketDomain socketDomain;
472  bool isBlocking;
473  IpAddress remoteIpAddress;
474  int remotePort;
475  bool isConnected;
476 
477 private: // static fields
478 
479 };
480 
482 // inline methods of class Socket
483 
484 inline SocketType Socket::GetSocketType(void) const
485 {
486  return socketType;
487 }
488 
489 inline SocketDomain Socket::GetSocketDomain(void) const
490 {
491  return socketDomain;
492 }
493 
494 inline bool Socket::IsBlocking(void) const
495 {
496  return isBlocking;
497 }
498 
499 inline IpAddress Socket::GetRemoteIpAddress(void) const
500 {
501  return remoteIpAddress;
502 }
503 
504 inline int Socket::GetRemotePort(void) const
505 {
506  return remotePort;
507 }
508 
509 inline bool Socket::IsConnected(void) const
510 {
511  return this->isConnected;
512 }
513 
514 }}}} // 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