Socket class.
More...
|
enum | tOpenMode { OM_Block = 0x0
, OM_NonBlock = 0x01
} |
| The mode in which to run the sockets during and after a connection. More...
|
|
enum | tShutdownMode { SM_Read = 0
, SM_Write = 1
, SM_ReadWrite = 2
} |
| The mode in which to switch the sockets to on shutdown. More...
|
|
enum | tOptionLevel { OL_SOCKET = 1
, OL_IPPROTO_TCP = 2
, OL_IPPROTO_IP = 3
} |
| Abstraction of the system specific socket option levels (e.g. More...
|
|
enum | tOptionId {
OI_DEBUG
, OI_BROADCAST
, OI_REUSEADDR
, OI_KEEPALIVE
,
OI_LINGER
, OI_SNDBUF
, OI_RCVBUF
, OI_DONTROUTE
,
OI_TCP_NODELAY
, OI_IP_MULTICAST_LOOP
} |
| Abstraction of the system specific socket options (e.g. More...
|
|
|
| cSocket () |
| Default constructor.
|
|
virtual | ~cSocket () |
| Default destructor.
|
|
tVoid | Close () |
| Closes the socket and frees all resources. More...
|
|
tVoid | Close (tShutdownMode nHow) |
| Closes the socket and frees all resources. More...
|
|
tUInt32 | GetAddress () const |
| Retrieves the IP address of the socket. More...
|
|
tInt | GetPort () const |
| Retrieves the port of the socket. More...
|
|
tSocketHandle | GetHandle () const |
| Get the internal socket handle. More...
|
|
tBool | SetTimeout (tTimeStamp nReadTimeout=-1, tTimeStamp nWriteTimeout=-1) |
| Sets timouts fo the read and write socket operations. More...
|
|
virtual tBool | IsConnected (tTimeStamp nTimeout=0) |
| Polls the socket for its connection state. More...
|
|
tBool | DataAvailable () |
| Check if data is available for reading (a subsequent read succeed immediately). More...
|
|
tBool | WaitForData (tTimeStamp nTimeout=-1) |
| Blocks the current process until data is available for reading. More...
|
|
tInt | SetOption (tInt nLevel, tInt nOptionId, const tVoid *pValue, tInt nValueLen) |
| Sets a specific option for the socket. More...
|
|
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.g. More...
|
|
tResult | GetOption (tInt nLevel, tInt nOptionId, tVoid *o_pValue, tInt *o_pValueLen) |
| gets a specific option for the socket. More...
|
|
tResult | GetOption (cSocket::tOptionLevel eLevel, cSocket::tOptionId eId, tVoid *o_pValue, tInt *o_pValueLen) |
| Equivalent with GetOption(tInt, tInt, tVoid*, tInt*). More...
|
|
Socket class.
Definition at line 23 of file socket.h.
◆ tOpenMode
The mode in which to run the sockets during and after a connection.
The blocking mode is the default mode and suspends the thread until a Read or Write call can either actually be completed or runs into a predefined read/write timeout (infinity by default). When calling Connect in blocking mode, an internal timeout will be used. On Windows this appears to be around 30 seconds whereas on Linux this tends to be around 2-5 seconds and is not to be confused with the read/write timeout which does not apply here.
In non-blocking mode, however, Connect returns immediately and a connection has to be checked for a successful establishment by calling IsConnected() (applies to StreamSockets only) Subsequent Read and Write calls will return immediately as well, however, possible return codes yielding failed operations do not imply a generally failed socket but only, that no data is currently available. For convenience use WaitForData() to periodically poll the socket for data.
- Note
- : Switching mode applies globally to a socket instance and cannot be changed in between connect and read/write operations.
Definition at line 69 of file socket.h.
◆ tOptionId
Abstraction of the system specific socket options (e.g.
SO_RCVBUF)
Enumerator |
---|
OI_DEBUG | Equivalent with SO_DEBUG.
|
OI_BROADCAST | Equivalent with SO_BROADCAST.
|
OI_REUSEADDR | Equivalent with SO_REUSEADDR.
|
OI_KEEPALIVE | Equivalent with SO_KEEPALIVE.
|
OI_LINGER | Equivalent with SO_LINGER.
|
OI_SNDBUF | Equivalent with SO_SNDBUF.
|
OI_RCVBUF | Equivalent with SO_RCVBUF.
|
OI_DONTROUTE | Equivalent with SO_DONTROUTE.
|
OI_TCP_NODELAY | Equivalent with TCP_NODELAY.
|
OI_IP_MULTICAST_LOOP | Equivalent with IP_MULTICAST_LOOP.
|
Definition at line 247 of file socket.h.
◆ tOptionLevel
Abstraction of the system specific socket option levels (e.g.
SOL_SOCKET)
Enumerator |
---|
OL_SOCKET | Equivalent with SOL_SOCKET.
|
OL_IPPROTO_TCP | Equivalent with IPPROTO_TCP.
|
OL_IPPROTO_IP | Equivalent with IPPROTO_IP.
|
Definition at line 235 of file socket.h.
◆ tShutdownMode
The mode in which to switch the sockets to on shutdown.
This has significant effect on error codes thrown on the remote side.
Definition at line 80 of file socket.h.
◆ AddressToUInt32()
Converts a aaa.bbb.ccc.ddd formated address string to an integer.
- Parameters
-
strAddress | the address string to convert. |
- Returns
- the address.
- Return values
-
SOCKET_NO_ADDRESS | if strAddress is not a valid IPv4 address. |
- This method is real-time safe.\nSee @ref page_real_time_safe.\n
◆ Close() [1/2]
Closes the socket and frees all resources.
- Returns
- void
◆ Close() [2/2]
Closes the socket and frees all resources.
- Note
- Extends cSocket::Close()
- Parameters
-
[in] | nHow | Parameter on how to close the socket. |
- Returns
- void
◆ DataAvailable()
Check if data is available for reading (a subsequent read succeed immediately).
- Returns
- tTrue if data is available, tFalse otherwise.
◆ FindInterfaceForAddress()
If more than one network interface is used in the host the FindInterfaceForAddress finds out the local address of the interface where the far address is reachable.
- Parameters
-
[in] | ui32FarAddress | The Address you want to reach |
[in] | ui32LocalAddress | return value of the local address of the interface |
- Returns
- Matching A_UTILS error
◆ GetAddress()
Retrieves the IP address of the socket.
- Returns
- the IP address of the socket.
- This method is real-time safe.\nSee @ref page_real_time_safe.\n
◆ GetHandle()
Get the internal socket handle.
- Returns
- internal socket handle.
- This method is real-time safe.\nSee @ref page_real_time_safe.\n
◆ GetHostAddress()
◆ GetHostName()
Returns the hostname for a given address.
Use
- Parameters
-
dwHostAddress | the interface address, if 0 -> localhost. |
- Returns
- the hostname. An empty string will returned in case of an error.
- See also
- AddLibraryRef
-
ReleaseLibraryRef
◆ GetLastErrorCode()
static tInt GetLastErrorCode |
( |
| ) |
|
|
static |
Gets the error code of the last failure.
- Returns
- The error code of the last failure.
◆ GetLocalAddresses()
Returns a list of addresses from all local interfaces.
- Parameters
-
[out] | oAddresses | The list of addresses. |
- Returns
- Standard result.
◆ GetOption() [1/2]
◆ GetOption() [2/2]
gets a specific option for the socket.
For further information about the GetOption method have a look at your manpage (getsockopt)
- Parameters
-
[in] | nLevel | option level, e.g. SOL_SOCKET |
[in] | nOptionId | option id, allowed values (in terms of platform independence) are:
- SO_DEBUG
- SO_BROADCAST
- SO_REUSEADDR
- SO_KEEPALIVE
- SO_LINGER
- SO_SNDBUF
- SO_RCVBUF
- SO_DONTROUTE
|
[out] | o_pValue | return value of the given nOptionId |
[out] | o_pValueLen | size of the returned value |
- Return values
-
ERR_NOERROR | everything is OK |
ERR_UNKNOWN | something went wrong |
◆ GetPort()
Retrieves the port of the socket.
- Returns
- the port of the socket.
- This method is real-time safe.\nSee @ref page_real_time_safe.\n
◆ Initialize()
Sets the params for the socket with 0/nullptr.
- Returns
- tTrue
◆ IsConnected()
Polls the socket for its connection state.
This is particularly useful when running the sockets in non-blocking mode and Connect() has returned with ERR_TIMEOUT (which does not necessarily imply a failed connection).
- Note
- : The current connection is being verified in read and write direction! If using a connection-based socket, please see cServerSocket::IsConnected() and cStreamSocket::IsConnected() respectively! Otherwise, using this method will require active traffic on the socket object when calling.
- Parameters
-
nTimeout | The timeout after which the connection is deemed to have failed. |
- Returns
- tTrue if the connection has found to be established successfully in both directions, implying that the remote socket accepted the TCP handshake and is already transmitting traffic on our local socket. In case of UDP this only implies an opened datagram socket.
-
tFalse if the connection has actually failed.
Reimplemented in cServerSocket, and cStreamSocket.
◆ ResolveOptionId()
Resolve the option id of the socket.
- Parameters
-
[in] | id | system specific socket option |
- Returns
- the resolved option
◆ ResolveOptionLevel()
Resolve the option value of the socket.
- Parameters
-
[in] | level | system specific socket option level |
- Returns
- the resolved option level
◆ SetOption() [1/2]
Equivalent to SetOption (tInt, tInt, const tVoid*, tInt) Instead of the system specific constants (e.g.
SOL_SOCKET) use cSocket::tOptionLevel and cSocket::tOptionId
- Parameters
-
[in] | eLevel | option level, allowed values: |
- See also
- cSocket::tOptionLevel
- Parameters
-
[in] | eId | option id, allowed values (in terms of platform independence): |
- See also
- cSocket::tOptionLevel
- Parameters
-
[in] | pValue | pointer to the new value |
[in] | nValueLen | size of the data pointed to by pValue |
- Returns
- returns an option specific return value.
◆ SetOption() [2/2]
Sets a specific option for the socket.
For further information about SetOption have a look at your manpage,... for setsockopt
- Parameters
-
[in] | nLevel | option level, e.g. SOL_SOCKET |
[in] | nOptionId | option id, allowed values ( in terms of platform independence) are:
- SO_DEBUG
- SO_BROADCAST
- SO_REUSEADDR
- SO_KEEPALIVE
- SO_LINGER
- SO_SNDBUF
- SO_RCVBUF
- SO_DONTROUTE
|
[in] | pValue | pointer to the new value. |
[in] | nValueLen | size of the data pointed to by pValue. |
- Returns
- returns an option specific return value.
◆ SetTimeout()
Sets timouts fo the read and write socket operations.
- Parameters
-
nReadTimeout | the timeout for read operations (microseconds). |
nWriteTimeout | the timeout for write operations (microseconds). |
- Returns
- tTrue if successful, tFalse otherwise.
◆ SocketError()
Maps a socket error to an A_UTILS error.
- Parameters
-
[in] | nSocketError | socket error returned by any socket function |
- Returns
- Matching A_UTILS error
◆ UInt32ToAddress()
Converts an integer to an aaa.bbb.ccc.ddd formated address string.
- Parameters
-
ui32Address | the address to convert. |
- Returns
- the address string.
◆ WaitForData() [1/2]
Blocks the current thread until data are available for reading in at least one of i_lstSockets
.
- Parameters
-
[in] | i_lstSockets | List of Sockets checked for data |
[out] | o_ppSocket | first cSocket Pointer to the cSocket out of i_lstSockets |
[in] | tmTimeOut | Maximum time to wait in microseconds, -1 = forever. |
- Returns
- Standard Error Code.
- Return values
-
ERR_POINTER | o_pSocket is set to nullptr |
◆ WaitForData() [2/2]
Blocks the current process until data is available for reading.
- Parameters
-
nTimeout | Maximum time to wait in microseconds, -1 = forever. |
- Returns
- true if data is available, false otherwise.