Base class for TCP and UDP sockets.
More...
#include <tsSocket.h>
|
| static int | SilentLevel (bool silent) |
| | Compute a log severity level from a "silent" parameter.
|
| |
Base class for TCP and UDP sockets.
◆ Socket() [1/2]
| ts::Socket::Socket |
( |
Report * |
report = nullptr, |
|
|
bool |
non_blocking = false |
|
) |
| |
|
explicit |
Constructor.
- Parameters
-
| [in] | report | Where to report errors. The report object must remain valid as long as this object exists or setReport() is used with another Report object. If report is null, log messages are discarded. |
| [in] | non_blocking | It true, the device is initially set in non-blocking mode. |
◆ Socket() [2/2]
| ts::Socket::Socket |
( |
ReporterBase * |
delegate, |
|
|
bool |
non_blocking = false |
|
) |
| |
|
explicit |
Constructor.
- Parameters
-
| [in] | delegate | Use the report of another ReporterBase. If delegate is null, log messages are discarded. |
| [in] | non_blocking | It true, the device is initially set in non-blocking mode. |
◆ open()
| virtual bool ts::Socket::open |
( |
IP |
gen | ) |
|
|
pure virtual |
◆ close()
| virtual bool ts::Socket::close |
( |
bool |
silent = false | ) |
|
|
virtual |
Close the socket.
If overridden by a subclass, the superclass must be called at the end of the overridden close().
- Parameters
-
| [in] | silent | If true, do not report errors through the logger. This is typically useful when the socket is in some error condition and closing it is necessary although it may generate additional meaningless errors. |
- Returns
- True on success, false on error.
Reimplemented in ts::TCPServer, ts::TCPSocket, ts::TLSServer, and ts::UDPSocket.
◆ isOpen()
| bool ts::Socket::isOpen |
( |
| ) |
const |
|
inline |
Check if socket is open.
- Returns
- True if socket is open.
◆ generation()
| IP ts::Socket::generation |
( |
| ) |
const |
|
inline |
Get the IP generation with which the socket was open.
- Returns
- The IP generation used to open the socket. Never IP::Any.
◆ setSendBufferSize()
| bool ts::Socket::setSendBufferSize |
( |
size_t |
size | ) |
|
Set the send buffer size.
- Parameters
-
| [in] | size | Send buffer size in bytes. |
- Returns
- True on success, false on error.
◆ setReceiveBufferSize()
| bool ts::Socket::setReceiveBufferSize |
( |
size_t |
size | ) |
|
Set the receive buffer size.
- Parameters
-
| [in] | size | Receive buffer size in bytes. |
- Returns
- True on success, false on error.
◆ setReceiveTimeout()
| bool ts::Socket::setReceiveTimeout |
( |
cn::milliseconds |
timeout | ) |
|
Set the receive timeout.
- Parameters
-
| [in] | timeout | Receive timeout in milliseconds. If negative or zero, receive timeout is not used, reception waits forever. |
- Returns
- True on success, false on error.
◆ reusePort()
| bool ts::Socket::reusePort |
( |
bool |
reuse_port | ) |
|
Set the "reuse port" option.
- Parameters
-
| [in] | reuse_port | If true, the socket is allowed to reuse a local UDP port which is already bound. |
- Returns
- True on success, false on error.
◆ bind()
Bind to a local address and port.
The IP address part of the socket address must one of:
- IPAddress::AnyAddress4.
- UDP: Any local interface may be used to send or receive UDP datagrams. For each outgoing packet, the actual interface is selected by the kernel based on the routing rules. Incoming UDP packets for the selected port will be accepted from any local interface.
- TCP client: Any local interface may be used to connect to a server.
- TCP server: Any local interface may be used to receive incoming client connections.
- The IP address of an interface of the local system.
- UDP: Outgoing packets will be unconditionally sent through this interface. Incoming UDP packets for the selected port will be accepted only when they arrive through the selected interface.
- TCP client: Outgoing connections will be only allowed through this interface.
- TCP server: Incoming client connections will be accepted only when they arrive through the selected interface.
Special note for receiving multicast on most Unix systems (at least Linux and macOS): The IP address shall be either AnyAddress4 or the multicast group address. Do not specify a local address to receive multicast on Unix.
The port number part of the socket address must be one of:
- IPSocketAddress::AnyPort. The socket is bound to an arbitrary unused local UDP or TCP port. This is the usual configuration for a TCP client.
- A specific port number. If this UDP or TCP port is already bound by another local socket of the same type, the bind operation fails, unless the "reuse port" option has already been set.
- Parameters
-
| [in] | addr | Local socket address to bind to. |
- Returns
- True on success, false on error.
◆ getLocalAddress()
Get local socket address.
- Parameters
-
| [out] | addr | Local socket address of the connection. |
- Returns
- True on success, false on error.
◆ getSocket()
Get the underlying socket device handle (use with care).
This method is reserved for low-level operations and should not be used by normal applications.
- Returns
- The underlying socket system device handle or file descriptor. Return SYS_SOCKET_INVALID if the socket is not open.
◆ addSubscription()
Add a subscriber to open/close events.
- Parameters
-
◆ cancelSubscription()
Remove a subscriber to open/close events.
- Parameters
-
◆ createSocket()
| bool ts::Socket::createSocket |
( |
IP |
gen, |
|
|
int |
type, |
|
|
int |
protocol |
|
) |
| |
|
protected |
Create the socket.
- Parameters
-
| [in] | gen | IP generation. |
| [in] | type | Socket type: SOCK_STREAM, SOCK_DGRAM |
| [in] | protocol | Socket protocol: IPPROTO_TCP, IPPROTO_UDP |
- Returns
- True on success, false on error.
- See also
- open(ge, Report&)
◆ declareOpened()
Set an open socket descriptor from a subclass.
This method is used by a server to declare that a client socket has just become opened.
- Parameters
-
| [in] | sock | New socket descriptor. |
Reimplemented in ts::TCPSocket.
◆ convert()
| bool ts::Socket::convert |
( |
IPAddress & |
addr | ) |
const |
|
protected |
Convert an IP address to make it compatible with the socket IP generation.
- Parameters
-
| addr | [in,out] The address to convert. |
- Returns
- True on success, false on error.
◆ allowSetNonBlocking()
| virtual bool ts::Socket::allowSetNonBlocking |
( |
| ) |
const |
|
overrideprotectedvirtual |
Check that the non-blocking mode can be set.
Must be implemented by subclasses which do not support setting the non-blocking in certain states, such as after being opened. The default implementation always allows setting the non-blocking mode.
- Returns
- True if setting the non-blocking mode is allowed, false otherwise.
Reimplemented from ts::NonBlockingDevice.
◆ setNonBlocking()
| bool ts::NonBlockingDevice::setNonBlocking |
( |
bool |
non_blocking | ) |
|
|
inherited |
Set the device in non-blocking mode.
Important: Usually, this method must be called before opening the device, whatever it means. Otherwise it is ignored and the device blocking mode is unchanged.
- Parameters
-
| [in] | non_blocking | It true, the device is set in non-blocking mode. |
- Returns
- True on success, false if the device is already open and the non-blocking mode is unchanged.
◆ isNonBlocking()
| bool ts::NonBlockingDevice::isNonBlocking |
( |
| ) |
const |
|
inlineinherited |
Check if the device is in non-blocking mode.
- Returns
- True if the device is in non-blocking mode, false otherwise.
- See also
- setNonBlocking()
◆ checkNonBlocking() [1/2]
| bool ts::NonBlockingDevice::checkNonBlocking |
( |
bool |
non_blocking, |
|
|
const UChar * |
opname |
|
) |
| |
|
protectedinherited |
Check the blocking mode of a device.
Called by subclass methods which are explicitly called in blocking or non-blocking mode.
- Parameters
-
| [in] | non_blocking | The required non-blocking mode. |
| [in] | opname | Name of the operation, for the error message. |
- Returns
- True on success, false on error.
◆ checkNonBlocking() [2/2]
| bool ts::NonBlockingDevice::checkNonBlocking |
( |
IOSB * |
iosb, |
|
|
const UChar * |
opname |
|
) |
| |
|
protectedinherited |
Check the blocking mode of a device.
Called by subclass methods which are explicitly called in blocking or non-blocking mode.
- Parameters
-
| [in,out] | iosb | Address of an IOSB structure. If non-null, we are in non-blocking mode. When null, we are in blocking mode. When non-null, pending is reset to false and overlap is zeroed. |
| [in] | opname | Name of the operation, for the error message. |
- Returns
- True on success, false on error.
◆ setSystemNonBlocking()
| bool ts::NonBlockingDevice::setSystemNonBlocking |
( |
SysSocketType |
fd, |
|
|
bool |
non_blocking |
|
) |
| |
|
protectedinherited |
Convenience method to set a system file descriptor or socket handle in non-blocking mode.
- Parameters
-
| [in] | fd | System file descriptor (UNIX) or socket handle (Windows). On Windows, non-socket devices shall be opened with flag FILE_FLAG_OVERLAPPED instead of using this method. |
| [in] | non_blocking | It true, the device is set in non-blocking mode. |
- Returns
- True on success, false on error.
◆ report()
| Report & ts::ReporterBase::report |
( |
| ) |
const |
|
inherited |
Access the Report which is associated with this object.
Can be called from another thread only if the Report object is thread-safe.
- Returns
- A reference to the associated report.
◆ setReport() [1/2]
Associate this object with another Report to log errors.
- Parameters
-
| [in] | report | Where to report errors. The report object must remain valid as long as this object exists or setReport() is used with another Report object. If report is null, log messages are discarded. |
- Returns
- The address of the previous Report object or a null pointer if there was none.
◆ setReport() [2/2]
Associate this object with another ReporterBase to log errors.
- Parameters
-
| [in] | delegate | Use the report of another ReporterBase. If delegate is null, the previous explicit Report is used.. |
- Returns
- The address of the previous ReporterBase object or a null pointer if there was none.
◆ muteReport()
| bool ts::ReporterBase::muteReport |
( |
bool |
mute | ) |
|
|
inherited |
Temporarily mute the associated report.
- Parameters
-
| [in] | mute | It true, report() will return a null report (log messages are discarded), until muteReport() is invoked again with mute set to false. |
- Returns
- Previous state of the mute field.
◆ SilentLevel()
| static int ts::ReporterBase::SilentLevel |
( |
bool |
silent | ) |
|
|
inlinestaticinherited |
Compute a log severity level from a "silent" parameter.
Some subclass methods have a "silent" parameter to avoid reporting errors which may be insignificant, typically when closing a device after an error, in which case the close operation may produce other errors if the previous error left the device in an inconsistent state. While those errors should not be displayed as errors, we still display them at debug level.
- Parameters
-
| [in] | silent | If true, do not report errors, report debug messages instead. |
- Returns
- Error when silent is false, Debug otherwise.
The documentation for this class was generated from the following file: