PLCnext API Documentation  20.0.0.24462
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 
228  SocketError Connect(const IpAddress& ip4Address, int port);
229 
241  SocketError Listen(size_t backlog);
242 
248  SocketError Shutdown(void);
249 
253  SocketError Close(void);
254 
262  int Send(const void *pBuffer, size_t length, SocketError& error);
263 
273  int SendTo(const void *pBuffer, size_t length, IpAddress ip4Adress, int port, SocketError& error);
274 
282  int Receive(void *pBuffer, size_t length, SocketError& error);
283 
293  int ReceiveFrom(void *pBuffer, size_t length, IpAddress& ip4Adress, int& port, SocketError& error);
294 
303  bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
304 
312  SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
313 
322  SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t *optionLength) const;
323 
334  SocketError SetOptionReuseAddress(bool enabled);
335 
343  SocketError GetOptionReuseAddress(bool& enabled) const;
344 
355  SocketError SetOptionKeepAlive(bool enabled);
356 
364  SocketError GetOptionKeepAlive(bool& enabled) const;
365 
374  SocketError SetOptionBroadcast(bool enabled);
375 
383  SocketError GetOptionBroadcast(bool& enabled) const;
384 
394  SocketError SetOptionNoDelay(bool enabled);
395 
403  SocketError GetOptionNoDelay(bool& enabled) const;
404 
408  SocketError SetOptionBlocking(bool enable);
409 
425  SocketError SetOptionLinger(bool enable, size_t timeout);
426 
433  SocketError GetOptionLinger(bool& enable, size_t& timeout);
434 
435 protected: // operations
436 
437 private: // usings/typedefs
438 
439  using ISocketService = Arp::System::Ve::ISocketService;
440 
441 private: // construction/destruction
442 
443  Socket(ISocketService *pSocket); //contruct a new Socket Object from a given ISocketService
444 
445 private: // static methods
446 
447 private: // methods
448 
449 private: // fields
450  ISocketService *pSocketService;
451 
452  SocketType socketType;
453  SocketDomain socketDomain;
454  bool isBlocking;
455  IpAddress remoteIpAddress;
456  int remotePort;
457  bool isConnected;
458 
459 private: // static fields
460 
461 };
462 
464 // inline methods of class Socket
465 
466 inline SocketType Socket::GetSocketType(void) const
467 {
468  return socketType;
469 }
470 
471 inline SocketDomain Socket::GetSocketDomain(void) const
472 {
473  return socketDomain;
474 }
475 
476 inline bool Socket::IsBlocking(void) const
477 {
478  return isBlocking;
479 }
480 
481 inline IpAddress Socket::GetRemoteIpAddress(void) const
482 {
483  return remoteIpAddress;
484 }
485 
486 inline int Socket::GetRemotePort(void) const
487 {
488  return remotePort;
489 }
490 
491 inline bool Socket::IsConnected(void) const
492 {
493  return this->isConnected;
494 }
495 
496 }}}} // 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