ADTF  3.18.2
socket.h
Go to the documentation of this file.
1 
7 #ifndef _SOCKET_BASE_CLASS_HEADER_
8 #define _SOCKET_BASE_CLASS_HEADER_
9 
10 namespace A_UTILS_NS
11 {
12 
13 
23 class DOEXPORT cSocket
24 {
26 
27  private:
29  static std::atomic<tInt> m_nSocketLibInitCounter;
30 
31  public:
33  static const tUInt32 SOCKET_NO_ADDRESS;
34 
35  protected:
42 
43  public:
44 
69  typedef enum
70  {
71  OM_Block = 0x0, //< Standard blocking mode
72  OM_NonBlock = 0x01 //< Non-blocking mode.
73  } tOpenMode;
74 
80  typedef enum
81  {
82  SM_Read = 0, //< Shutdown in read-direction only.
83  SM_Write = 1, //< Shutdown in write-direction only.
84  SM_ReadWrite = 2 //< Shutdown in both directions (clean shutdown).
85  } tShutdownMode;
86 
87  public:
92  static tVoid AddLibraryRef();
97 
98  public:
102  virtual ~cSocket();
103 
109 
117 
127 
136  tInt GetPort() const;
137 
147 
160  static cString GetHostName(tUInt32 dwHostAddress=0);
174  static tUInt32 GetHostAddress(const cString& strHostName=cString());
175 
187  static tUInt32 AddressToUInt32(const cString& strAddress);
188 
198  static cString UInt32ToAddress(tUInt32 ui32Address);
199 
205 
211  static tResult GetLocalAddresses(std::set< tUInt32, std::less< tUInt32 >, std::allocator< tUInt32 > >& oAddresses);
212 
218  static tResult SocketError(tInt nSocketError);
219 
228  static tResult FindInterfaceForAddress(tUInt32 ui32FarAddress,
229  tUInt32& ui32LocalAddress);
230 
231  public:
235  typedef enum {
237  OL_SOCKET = 1,
239  OL_IPPROTO_TCP = 2,
241  OL_IPPROTO_IP = 3,
242  } tOptionLevel;
243 
247  typedef enum {
267  OI_IP_MULTICAST_LOOP
268  } tOptionId;
269 
280  tBool SetTimeout(tTimeStamp nReadTimeout=-1, tTimeStamp nWriteTimeout=-1);
281 
282 
299  virtual tBool IsConnected(tTimeStamp nTimeout = 0);
300 
309 
318 
329  static tResult WaitForData(const std::list<cSocket*>& i_lstSockets,
330  cSocket** o_ppSocket,
331  tTimeStamp tmTimeOut = -1);
332 
357  tInt SetOption(tInt nLevel, tInt nOptionId, const tVoid* pValue, tInt nValueLen);
358 
374  tInt SetOption(cSocket::tOptionLevel eLevel, cSocket::tOptionId eId, const tVoid* pValue, tInt nValueLen);
375 
396  tResult GetOption(tInt nLevel, tInt nOptionId, tVoid* o_pValue, tInt* o_pValueLen);
397 
398 
411  tResult GetOption(cSocket::tOptionLevel eLevel, cSocket::tOptionId eId, tVoid* o_pValue, tInt* o_pValueLen);
412 
413  protected:
419 
428 
437 };
438 
439 } // namespace A_UTILS_NS
440 
441 #endif // _SOCKET_BASE_CLASS_HEADER_
void tVoid
The tVoid is always the definition for the void (non-type).
int tInt
type definition for signed integer value (platform and compiler dependent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
uint32_t tUInt32
type definition for unsigned integer values (32bit) (platform and compiler independent type).
Socket class.
Definition: socket.h:24
tBool Initialize()
Sets the params for the socket with 0/nullptr.
tShutdownMode
The mode in which to switch the sockets to on shutdown.
Definition: socket.h:81
static const tUInt32 SOCKET_NO_ADDRESS
Constance for check No Address.
Definition: socket.h:33
tInt ResolveOptionId(tOptionId id) const
Resolve the option id of the socket.
static tUInt32 AddressToUInt32(const cString &strAddress)
Converts a aaa.bbb.ccc.ddd formated address string to an integer.
tVoid Close(tShutdownMode nHow)
Closes the socket and frees all resources.
static tResult WaitForData(const std::list< cSocket * > &i_lstSockets, cSocket **o_ppSocket, tTimeStamp tmTimeOut=-1)
Blocks the current thread until data are available for reading in at least one of i_lstSockets.
static std::atomic< tInt > m_nSocketLibInitCounter
For internal use only.
Definition: socket.h:29
tInt SetOption(tInt nLevel, tInt nOptionId, const tVoid *pValue, tInt nValueLen)
Sets a specific option for the socket.
tSocketHandle m_hSocket
this is internal handle of the socket, only use it if you really need that!!
Definition: socket.h:37
tOptionId
Abstraction of the system specific socket options (e.g.
Definition: socket.h:247
@ OI_TCP_NODELAY
Equivalent with TCP_NODELAY.
Definition: socket.h:265
@ OI_SNDBUF
Equivalent with SO_SNDBUF.
Definition: socket.h:259
@ OI_DONTROUTE
Equivalent with SO_DONTROUTE.
Definition: socket.h:263
@ OI_REUSEADDR
Equivalent with SO_REUSEADDR.
Definition: socket.h:253
@ OI_RCVBUF
Equivalent with SO_RCVBUF.
Definition: socket.h:261
@ OI_KEEPALIVE
Equivalent with SO_KEEPALIVE.
Definition: socket.h:255
@ OI_LINGER
Equivalent with SO_LINGER.
Definition: socket.h:257
@ OI_BROADCAST
Equivalent with SO_BROADCAST.
Definition: socket.h:251
@ OI_DEBUG
Equivalent with SO_DEBUG.
Definition: socket.h:249
tVoid Close()
Closes the socket and frees all resources.
static cString GetHostName(tUInt32 dwHostAddress=0)
Returns the hostname for a given address.
tBool WaitForData(tTimeStamp nTimeout=-1)
Blocks the current process until data is available for reading.
tOptionLevel
Abstraction of the system specific socket option levels (e.g.
Definition: socket.h:235
tInt m_nPort
Port of the socket (valid only if opened)
Definition: socket.h:41
tInt ResolveOptionLevel(tOptionLevel level) const
Resolve the option value of the socket.
static tResult SocketError(tInt nSocketError)
Maps a socket error to an A_UTILS error.
static cString UInt32ToAddress(tUInt32 ui32Address)
Converts an integer to an aaa.bbb.ccc.ddd formated address string.
virtual tBool IsConnected(tTimeStamp nTimeout=0)
Polls the socket for its connection state.
static tInt GetLastErrorCode()
Gets the error code of the last failure.
static tResult FindInterfaceForAddress(tUInt32 ui32FarAddress, tUInt32 &ui32LocalAddress)
If more than one network interface is used in the host the FindInterfaceForAddress finds out the loca...
tResult GetOption(cSocket::tOptionLevel eLevel, cSocket::tOptionId eId, tVoid *o_pValue, tInt *o_pValueLen)
Equivalent with GetOption(tInt, tInt, tVoid*, tInt*).
tResult GetOption(tInt nLevel, tInt nOptionId, tVoid *o_pValue, tInt *o_pValueLen)
gets a specific option for the socket.
tInt SetOption(cSocket::tOptionLevel eLevel, cSocket::tOptionId eId, const tVoid *pValue, tInt nValueLen)
Equivalent to SetOption (tInt, tInt, const tVoid*, tInt) Instead of the system specific constants (e....
static tResult GetLocalAddresses(std::set< tUInt32, std::less< tUInt32 >, std::allocator< tUInt32 > > &oAddresses)
Returns a list of addresses from all local interfaces.
tInt GetPort() const
Retrieves the port of the socket.
static tVoid ReleaseLibraryRef()
to correctly unload the socket-libs you have to call this function
static tVoid AddLibraryRef()
for the work with the static methods you have to use this function to load the nessessary libs,...
virtual ~cSocket()
Default destructor.
tBool SetTimeout(tTimeStamp nReadTimeout=-1, tTimeStamp nWriteTimeout=-1)
Sets timouts fo the read and write socket operations.
tSocketHandle GetHandle() const
Get the internal socket handle.
static tUInt32 GetHostAddress(const cString &strHostName=cString())
Returns the address for a given hostname.
tBool DataAvailable()
Check if data is available for reading (a subsequent read succeed immediately).
cSocket()
Default constructor.
tUInt32 m_ui32Address
Address of the socket (valid only if opened)
Definition: socket.h:39
tUInt32 GetAddress() const
Retrieves the IP address of the socket.
#define A_UTILS_D(__pclassname_)
Helper macro for d-pattern definitions.
Definition: d_ptr.h:270
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2778
int tSocketHandle
work with platform undependend typedefinitions for socket handlers
Definition: sockettypes.h:15
bool set(void *dest, std::size_t dest_size, std::uint8_t value, std::size_t bytes_to_set)
Portable safe memset.