TSDuck v3.45-4686
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::tlv::Connection< SAFETY > Class Template Reference

TCP connection using TLV messages. More...

#include <tstlvConnection.h>

Inheritance diagram for ts::tlv::Connection< SAFETY >:
Collaboration diagram for ts::tlv::Connection< SAFETY >:

Public Types

using MutexType = typename ThreadSafetyMutex< SAFETY >::type
 Generic definition of the mutex for this class.
 
using SuperClass = ts::TCPConnection
 Reference to superclass.
 

Public Member Functions

 Connection (Logger &logger, const Protocol &protocol, bool auto_error_response=true, size_t max_invalid_msg=0, Object *owner=nullptr)
 Constructor.
 
void addSubscription (SocketHandlerInterface *handler)
 Add a subscriber to open/close events.
 
bool bind (const IPSocketAddress &addr)
 Bind to a local address and port.
 
void cancelSubscription (SocketHandlerInterface *handler)
 Remove a subscriber to open/close events.
 
virtual bool close (bool silent=false) final
 Close the socket.
 
virtual bool closeWriter (bool silent=false)
 Close the write direction of the connection.
 
virtual bool connect (const IPSocketAddress &addr, IOSB *iosb=nullptr)
 Connect to a remote address and port.
 
virtual bool disconnect (bool silent=false)
 Disconnect from remote partner.
 
IP generation () const
 Get the IP generation with which the socket was open.
 
bool getAutoErrorResponse () const
 Get invalid incoming messages processing.
 
bool getLocalAddress (IPSocketAddress &addr) const
 Get local socket address.
 
size_t getMaxInvalidMessages () const
 Get invalid message threshold.
 
bool getPeer (IPSocketAddress &addr)
 Get the connected remote peer.
 
SysSocketType getSocket () const
 Get the underlying socket device handle (use with care).
 
bool isConnected () const
 Check if the socket is connected.
 
bool isNonBlocking () const
 Check if the device is in non-blocking mode.
 
bool isOpen () const
 Check if socket is open.
 
bool isOwned ()
 Check if the object is owned.
 
template<class OBJECT >
requires std::derived_from<OBJECT, ts::Object>
bool isOwned ()
 Check if the object is owned by an object of a given type.
 
UString localName ()
 Get the local address as a string.
 
bool muteReport (bool mute)
 Temporarily mute the associated report.
 
virtual bool open (IP gen=IP::Any) final
 Open the socket.
 
Objectowner ()
 Get the address of the optional "owner" object which was specified in the constructor.
 
template<class OBJECT >
requires std::derived_from<OBJECT, ts::Object>
OBJECT * owner ()
 Get the address of the "owner" object which was specified in the constructor.
 
UString peerName ()
 Get the connected remote peer as a string.
 
virtual bool receive (void *buffer, size_t max_size, size_t &ret_size, const AbortInterface *abort=nullptr, IOSB *iosb=nullptr)
 Receive data.
 
virtual bool receive (void *buffer, size_t size, const AbortInterface *abort=nullptr)
 Receive data until buffer is full.
 
bool receiveMessage (MessagePtr &msg, const AbortInterface *abort=nullptr)
 Receive a TLV message.
 
Reportreport () const
 Access the Report which is associated with this object.
 
bool reusePort (bool reuse_port)
 Set the "reuse port" option.
 
virtual bool send (const void *data, size_t size, IOSB *iosb=nullptr)
 Send data.
 
bool sendMessage (const Message &msg)
 Serialize and sendMessage a TLV message.
 
void setAutoErrorResponse (bool on)
 Set invalid incoming messages processing.
 
bool setConnectStatus (IOSB *iosb, int error_code)
 Update the status of an asynchronous connect().
 
bool setKeepAlive (bool active)
 Set the "keep alive" option.
 
bool setLingerTime (int seconds)
 Set the linger time option.
 
void setMaxInvalidMessages (size_t n)
 Set invalid message threshold.
 
bool setNoDelay (bool active)
 Set the "no delay" option.
 
bool setNoLinger ()
 Remove the linger time option.
 
bool setNonBlocking (bool non_blocking)
 Set the device in non-blocking mode.
 
bool setReceiveBufferSize (size_t size)
 Set the receive buffer size.
 
bool setReceiveTimeout (cn::milliseconds timeout)
 Set the receive timeout.
 
ReportsetReport (Report *report)
 Associate this object with another Report to log errors.
 
ReporterBasesetReport (ReporterBase *delegate)
 Associate this object with another ReporterBase to log errors.
 
bool setSendBufferSize (size_t size)
 Set the send buffer size.
 
bool setTTL (int ttl)
 Set the Time To Live (TTL) option.
 

Static Public Member Functions

static bool IsPendingStatus (int error_code)
 This static method checks if a system error code means "I/O in progress" (asynchronous I/O) or "I/O would block" (non-blocking I/O).
 
static int SilentLevel (bool silent)
 Compute a log severity level from a "silent" parameter.
 

Protected Member Functions

virtual bool allowSetNonBlocking () const override
 Check that the non-blocking mode can be set.
 
template<typename F >
void callSubscribers (F &&func)
 Call a handler on all subscribers, using a lambda expression.
 
bool checkNonBlocking (bool non_blocking, const UChar *opname)
 Check the blocking mode of a device.
 
bool checkNonBlocking (IOSB *iosb, const UChar *opname)
 Check the blocking mode of a device.
 
bool convert (IPAddress &addr) const
 Convert an IP address to make it compatible with the socket IP generation.
 
bool createSocket (IP gen, int type, int protocol)
 Create the socket.
 
virtual void declareOpened (SysSocketType sock) override
 Set an open socket descriptor from a subclass.
 
virtual void handleSocketCloseComplete (Socket &sock, bool silent, bool success)
 Called after a socket is closed.
 
virtual void handleSocketCloseStart (Socket &sock, bool silent)
 Called when a socket is about to be closed.
 
virtual void handleSocketConnected (TCPConnection &sock) override
 Called when a TCP socket is successfully connected.
 
virtual void handleSocketDisconnected (TCPConnection &sock, bool silent)
 Called when a TCP socket is disconnected.
 
virtual void handleSocketOpenComplete (Socket &sock, bool success)
 Called after a socket is opened (or failed to open).
 
virtual void handleSocketOpenStart (Socket &sock)
 Called when a socket is about to be opened.
 
virtual bool openImplementation (IP gen) override
 Open the socket, actual implementation which must be overriden by subclasses.
 
bool setSystemNonBlocking (SysSocketType fd, bool non_blocking)
 Low-level method to set a system file or socket descriptor in non-blocking mode.
 

Protected Attributes

std::recursive_mutex _mutex {}
 Mutex protecting this object.
 

Detailed Description

template<ThreadSafety SAFETY>
class ts::tlv::Connection< SAFETY >

TCP connection using TLV messages.

Template Parameters
SAFETYThe required type of thread-safety.

Constructor & Destructor Documentation

◆ Connection()

template<ts::ThreadSafety SAFETY>
ts::tlv::Connection< SAFETY >::Connection ( Logger logger,
const Protocol protocol,
bool  auto_error_response = true,
size_t  max_invalid_msg = 0,
Object owner = nullptr 
)

Constructor.

Parameters
[in,out]loggerWhere to report errors and messages. An internal reference is kept. The logger object must remain valid as long as this object exists.
[in]protocolThe incoming messages are interpreted according to this protocol. The reference is kept in this object.
[in]auto_error_responseWhen an invalid message is received, the corresponding error message is automatically sent back to the sender when auto_error_response is true.
[in]max_invalid_msgWhen non-zero, the connection is automatically disconnected when the number of consecutive invalid messages has reached this value.
[in]ownerOptional address of an "owner" object, typically an instance of class containing this object.

Member Function Documentation

◆ sendMessage()

template<ts::ThreadSafety SAFETY>
bool ts::tlv::Connection< SAFETY >::sendMessage ( const Message msg)

Serialize and sendMessage a TLV message.

Parameters
[in]msgThe message to sendMessage.
Returns
True on success, false on error.

◆ receiveMessage()

template<ts::ThreadSafety SAFETY>
bool ts::tlv::Connection< SAFETY >::receiveMessage ( MessagePtr msg,
const AbortInterface abort = nullptr 
)

Receive a TLV message.

Wait for the message, deserialize it and validate it. Process invalid messages and loop until a valid message is received.

Parameters
[out]msgA safe pointer to the received message.
[in]abortIf non-zero, invoked when I/O is interrupted (in case of user-interrupt, return, otherwise retry).
Returns
True on success, false on error.

◆ getAutoErrorResponse()

template<ThreadSafety SAFETY>
bool ts::tlv::Connection< SAFETY >::getAutoErrorResponse ( ) const
inline

Get invalid incoming messages processing.

Returns
True if, when an invalid message is received, the corresponding error message is automatically sent back to the sender.

◆ setAutoErrorResponse()

template<ThreadSafety SAFETY>
void ts::tlv::Connection< SAFETY >::setAutoErrorResponse ( bool  on)
inline

Set invalid incoming messages processing.

Parameters
[in]onWhen an invalid message is received, the corresponding error message is automatically sent back to the sender when on is true.

◆ getMaxInvalidMessages()

template<ThreadSafety SAFETY>
size_t ts::tlv::Connection< SAFETY >::getMaxInvalidMessages ( ) const
inline

Get invalid message threshold.

Returns
When non-zero, the connection is automatically disconnected when the number of consecutive invalid messages has reached this value.

◆ setMaxInvalidMessages()

template<ThreadSafety SAFETY>
void ts::tlv::Connection< SAFETY >::setMaxInvalidMessages ( size_t  n)
inline

Set invalid message threshold.

Parameters
[in]nWhen non-zero, the connection is automatically disconnected when the number of consecutive invalid messages has reached this value.

◆ handleSocketConnected()

template<ts::ThreadSafety SAFETY>
void ts::tlv::Connection< SAFETY >::handleSocketConnected ( TCPConnection< SAFETY > &  sock)
overrideprotectedvirtual

Called when a TCP socket is successfully connected.

Parameters
[in,out]sockSocket for which the handler is invoked.

Reimplemented from ts::SocketHandlerInterface.

◆ connect()

virtual bool ts::TCPConnection::connect ( const IPSocketAddress addr,
IOSB iosb = nullptr 
)
virtualinherited

Connect to a remote address and port.

Use this method when acting as TCP client. Do not use on server side: the TCPConnection object is passed to TCPServer::accept() which establishes the connection.

Parameters
[in]addrIP address and port of the server to connect to.
[in,out]iosbAddress of an IOSB structure. If non-null, the socket must be in non-blocking mode. When null, the socket must be in blocking mode (the default). See the description of IOSB. Important: The parameter iosb should not be used by applications. It should be used only by "reactive classes", which work in combination with a Reactor.
Returns
True on success, false on error. In case of non-blocking mode, if the connection is successfully started but still pending, iosb->pending is set to true and the method returns true.

Reimplemented in ts::TLSConnection.

◆ setConnectStatus()

bool ts::TCPConnection::setConnectStatus ( IOSB iosb,
int  error_code 
)
inherited

Update the status of an asynchronous connect().

  • With non-blocking I/O, error_code shall be the error from the write-ready notification.
  • With asynchronous I/O, iosb is used to complete the socket state.
    Parameters
    [in,out]iosbAddress of the IOSB structure which was used when connect() was called.
    [in]error_codeError code in the context of the connection.
    Returns
    True on success, false on error.

◆ isConnected()

bool ts::TCPConnection::isConnected ( ) const
inlineinherited

Check if the socket is connected.

Returns
True if the socket was successfully connected to the peer.

◆ getPeer()

bool ts::TCPConnection::getPeer ( IPSocketAddress addr)
inherited

Get the connected remote peer.

Parameters
[out]addrIP address and port of the remote socket.
Returns
True on success, false on error.

◆ peerName()

UString ts::TCPConnection::peerName ( )
inherited

Get the connected remote peer as a string.

Returns
A string representation of the IP address and port of the remote socket.

◆ closeWriter()

virtual bool ts::TCPConnection::closeWriter ( bool  silent = false)
virtualinherited

Close the write direction of the connection.

The application shall call this routine after sending the last message but may still want to receive messages, waiting for the peer to voluntary disconnect.

Parameters
[in]silentIf 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::TLSConnection.

◆ disconnect()

virtual bool ts::TCPConnection::disconnect ( bool  silent = false)
virtualinherited

Disconnect from remote partner.

Parameters
[in]silentIf 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::TLSConnection.

◆ send()

virtual bool ts::TCPConnection::send ( const void *  data,
size_t  size,
IOSB iosb = nullptr 
)
virtualinherited

Send data.

Parameters
[in]dataAddress of the data to send.
[in]sizeSize in bytes of the data to send.
[in,out]iosbAddress of an IOSB structure. If non-null, the socket must be in non-blocking mode. When null, the socket must be in blocking mode (the default). See the description of IOSB. Important: The parameter iosb should not be used by applications. It should be used only by "reactive classes", which work in combination with a Reactor.
Returns
True on success, false on error. In case of non-blocking mode, if the I/O is successfully started but still pending, iosb->pending is set to true and the method returns true.

Reimplemented in ts::TLSConnection.

◆ receive() [1/2]

virtual bool ts::TCPConnection::receive ( void *  buffer,
size_t  max_size,
size_t &  ret_size,
const AbortInterface abort = nullptr,
IOSB iosb = nullptr 
)
virtualinherited

Receive data.

This version of receiveMessage() returns when "some" data are received into the user buffer. The actual received data may be shorter than the user buffer size.

The version is typically useful when the application cannot predict how much data will be received and must respond even if the user buffer is not full.

Parameters
[out]bufferAddress of the buffer for the received data.
[in]max_sizeSize in bytes of the reception buffer.
[out]ret_sizeSize in bytes of the received data. Will never be larger than max_size.
[in]abortIf non-zero, invoked when I/O is interrupted (in case of user-interrupt, return, otherwise retry).
[in,out]iosbAddress of an IOSB structure. If non-null, the socket must be in non-blocking mode. When null, the socket must be in blocking mode (the default). See the description of IOSB. Important: The parameter iosb should not be used by applications. It should be used only by "reactive classes", which work in combination with a Reactor.
Returns
True on success, false on error. In case of non-blocking mode, if the I/O is successfully started but still pending, iosb->pending is set to true and the method returns true.

Reimplemented in ts::TLSConnection.

◆ receive() [2/2]

virtual bool ts::TCPConnection::receive ( void *  buffer,
size_t  size,
const AbortInterface abort = nullptr 
)
virtualinherited

Receive data until buffer is full.

This version of receive() returns only when sufficient data are received to completely fill the user buffer. The size of the actual received data is identical to the user buffer size. If some data, but not all, were received before the connection was closed, these data are ignored and the method returns false. The version is typically useful when the application knows that a certain amount of data is expected and must wait for them.

This method is only allowed when the socket is in blocking-mode (the default) because this method is blocking by definition. Therefore, there is no iosb parameter.

This base implementation uses the variable-length version of receive(). Therefore, a subclass may only override the variable-length version and not this one.

Parameters
[out]bufferAddress of the buffer for the received data.
[in]sizeSize in bytes of the buffer.
[in]abortIf non-zero, invoked when I/O is interrupted (in case of user-interrupt, return, otherwise retry).
Returns
True on success, false on error.

Reimplemented in ts::TLSConnection.

◆ setTTL()

bool ts::TCPSocket::setTTL ( int  ttl)
inherited

Set the Time To Live (TTL) option.

Parameters
[in]ttlThe TTL value, ie. the maximum number of "hops" between routers before an IP packet is dropped.
Returns
True on success, false on error.

◆ setNoLinger()

bool ts::TCPSocket::setNoLinger ( )
inherited

Remove the linger time option.

Returns
True on success, false on error.

◆ setLingerTime()

bool ts::TCPSocket::setLingerTime ( int  seconds)
inherited

Set the linger time option.

Parameters
[in]secondsNumber of seconds to wait after shuting down the socket.
Returns
True on success, false on error.

◆ setKeepAlive()

bool ts::TCPSocket::setKeepAlive ( bool  active)
inherited

Set the "keep alive" option.

Parameters
[in]activeIf true, the socket periodically sends "keep alive" packets when the connection is idle.
Returns
True on success, false on error.

◆ setNoDelay()

bool ts::TCPSocket::setNoDelay ( bool  active)
inherited

Set the "no delay" option.

Parameters
[in]activeIf true, the socket immediately sends outgoing packets. By default, a TCP socket waits a small amount of time after a send() operation to get a chance to group outgoing data from successive send() operations into one single packet.
Returns
True on success, false on error.

◆ openImplementation()

virtual bool ts::TCPSocket::openImplementation ( IP  gen)
overrideprotectedvirtualinherited

Open the socket, actual implementation which must be overriden by subclasses.

Never called when the application tries to open a socket which is already open.

Parameters
[in]genIP generation, IPv4 or IPv6. If set to IP::Any, open an IPv6 socket (IPv4 connections allowed).
Returns
True on success, false on error.

Implements ts::Socket.

◆ declareOpened()

virtual void ts::TCPSocket::declareOpened ( SysSocketType  sock)
overrideprotectedvirtualinherited

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]sockNew socket descriptor.

Reimplemented from ts::Socket.

◆ open()

virtual bool ts::Socket::open ( IP  gen = IP::Any)
finalvirtualinherited

Open the socket.

Subclasses are not permitted to override this one, they should implement openImplementation().

Parameters
[in]genIP generation, IPv4 or IPv6. If set to IP::Any, open an IPv6 socket (IPv4 connections allowed).
Returns
True on success, false on error.

◆ close()

virtual bool ts::Socket::close ( bool  silent = false)
finalvirtualinherited

Close the socket.

Subclasses are not permitted to override this one, they should implement closeImplementation().

Parameters
[in]silentIf 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.

◆ isOpen()

bool ts::Socket::isOpen ( ) const
inlineinherited

Check if socket is open.

Returns
True if socket is open.

◆ generation()

IP ts::Socket::generation ( ) const
inlineinherited

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)
inherited

Set the send buffer size.

Parameters
[in]sizeSend buffer size in bytes.
Returns
True on success, false on error.

◆ setReceiveBufferSize()

bool ts::Socket::setReceiveBufferSize ( size_t  size)
inherited

Set the receive buffer size.

Parameters
[in]sizeReceive buffer size in bytes.
Returns
True on success, false on error.

◆ setReceiveTimeout()

bool ts::Socket::setReceiveTimeout ( cn::milliseconds  timeout)
inherited

Set the receive timeout.

Parameters
[in]timeoutReceive 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)
inherited

Set the "reuse port" option.

Parameters
[in]reuse_portIf true, the socket is allowed to reuse a local UDP port which is already bound.
Returns
True on success, false on error.

◆ bind()

bool ts::Socket::bind ( const IPSocketAddress addr)
inherited

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]addrLocal socket address to bind to.
Returns
True on success, false on error.

◆ getLocalAddress()

bool ts::Socket::getLocalAddress ( IPSocketAddress addr) const
inherited

Get local socket address.

Parameters
[out]addrLocal socket address of the connection.
Returns
True on success, false on error.

◆ localName()

UString ts::Socket::localName ( )
inherited

Get the local address as a string.

Returns
A string representation of the IP address and port of the socket.

◆ getSocket()

SysSocketType ts::Socket::getSocket ( ) const
inlineinherited

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()

void ts::Socket::addSubscription ( SocketHandlerInterface handler)
inherited

Add a subscriber to open/close events.

Parameters
[in]handlerThe object to call on open() and close().

◆ cancelSubscription()

void ts::Socket::cancelSubscription ( SocketHandlerInterface handler)
inherited

Remove a subscriber to open/close events.

Parameters
[in]handlerThe object to no longer call on open() and close().

◆ createSocket()

bool ts::Socket::createSocket ( IP  gen,
int  type,
int  protocol 
)
protectedinherited

Create the socket.

Parameters
[in]genIP generation.
[in]typeSocket type: SOCK_STREAM, SOCK_DGRAM
[in]protocolSocket protocol: IPPROTO_TCP, IPPROTO_UDP
Returns
True on success, false on error.
See also
open(ge, Report&)

◆ convert()

bool ts::Socket::convert ( IPAddress addr) const
protectedinherited

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.

◆ callSubscribers()

template<typename F >
void ts::Socket::callSubscribers ( F &&  func)
inlineprotectedinherited

Call a handler on all subscribers, using a lambda expression.

Parameters
[in]funcFunction to call as lambda expression.

◆ allowSetNonBlocking()

virtual bool ts::Socket::allowSetNonBlocking ( ) const
overrideprotectedvirtualinherited

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_blockingIt 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()

◆ IsPendingStatus()

static bool ts::NonBlockingDevice::IsPendingStatus ( int  error_code)
staticinherited

This static method checks if a system error code means "I/O in progress" (asynchronous I/O) or "I/O would block" (non-blocking I/O).

Parameters
[in]error_codeSystem error code.
Returns
True if error_code is an in-progress/would-block one.

◆ 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_blockingThe required non-blocking mode.
[in]opnameName 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]iosbAddress 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]opnameName 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

Low-level method to set a system file or socket descriptor in non-blocking mode.

Parameters
[in]fdSystem file or socket descriptor.
[in]non_blockingIt true, the device is set in non-blocking mode.
Returns
True on success, false on error.

Summary: Do not use this method unless you exactly know what you are doing.

UNIX: Depending on the way a file descriptor is created, it may be possible to specify the non-blocking mode from the beginning. Or it can be somehow inherited. However, this is not portable.

Examples:

  • On Linux and FreeBSD, a socket can be directly created in non-blocking mode using the flag SOCK_NONBLOCK in the 'type' parameter of the socket() system call. However, it does not work on macOS.
  • On macOS (and maybe FreeBSD), when a server socket is in non-blocking mode, all client session sockets which are created by accept() are also in non-blocking mode. However, on Linux, they are in blocking mode.

In all cases, it is possible to set a file descriptor in non-blocking mode at any time using the method setSystemNonBlocking(). This method uses fcntl(F_SETFL) to alter the file descriptor's flags.

Windows: The natural way of not being blocked on I/O on Windows is asynchronous I/O. To increase the general confusion, there is some form of non-blocking mode on Windows sockets, and only sockets, not other forms of file handles. This mode is activated using "ioctlsocket(fd, FIONBIO, &mode)". When this mode is active, socket I/O become similar to UNIX: they immediately either succeed or fail, but never block. However, there is no way to get notified when the I/O becomes possible. There is no equivalent to epoll (Linux) or kqueue (macOS and BSD). The Windows I/O Completion Ports can only work on asynchronous I/O, using OVERLAPPED structures. Because this form of non-blocking mode is mostly useless in practice, we do not use it and the method setSystemNonBlocking() does nothing on Windows.

See also
https://learn.microsoft.com/en-us/archive/blogs/csliu/io-concept-blockingnon-blocking-vs-syncasync

◆ 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]

Report * ts::ReporterBase::setReport ( Report report)
inherited

Associate this object with another Report to log errors.

Parameters
[in]reportWhere 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]

ReporterBase * ts::ReporterBase::setReport ( ReporterBase delegate)
inherited

Associate this object with another ReporterBase to log errors.

Parameters
[in]delegateUse 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]muteIt 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]silentIf true, do not report errors, report debug messages instead.
Returns
Error when silent is false, Debug otherwise.

◆ owner() [1/2]

Object * ts::OwnedObject::owner ( )
inlineinherited

Get the address of the optional "owner" object which was specified in the constructor.

Returns
Address of the "owner" object or a null pointer if there was none.

◆ owner() [2/2]

template<class OBJECT >
requires std::derived_from<OBJECT, ts::Object>
OBJECT * ts::OwnedObject::owner ( )
inherited

Get the address of the "owner" object which was specified in the constructor.

This template version requires that the owner objet is set and of type OBJECT, or some subclass of it. If there is no owner object or if it is not compatible with the template class OBJECT, this is a fatal error and the application is terminated.

Template Parameters
OBJECTA subclass of Object
Returns
Address of the "owner" object or a null pointer if there was none.

◆ isOwned() [1/2]

bool ts::OwnedObject::isOwned ( )
inlineinherited

Check if the object is owned.

Returns
True if this object has an owner, false otherwise.

◆ isOwned() [2/2]

template<class OBJECT >
requires std::derived_from<OBJECT, ts::Object>
bool ts::OwnedObject::isOwned ( )
inlineinherited

Check if the object is owned by an object of a given type.

Template Parameters
OBJECTA subclass of Object
Returns
True if this object has an owner by an object of type OBJECT, false otherwise.

◆ handleSocketOpenStart()

virtual void ts::SocketHandlerInterface::handleSocketOpenStart ( Socket sock)
virtualinherited

Called when a socket is about to be opened.

Never called when the application tries to open a socket which is already open.

Parameters
[in,out]sockSocket for which the handler is invoked.

◆ handleSocketOpenComplete()

virtual void ts::SocketHandlerInterface::handleSocketOpenComplete ( Socket sock,
bool  success 
)
virtualinherited

Called after a socket is opened (or failed to open).

Parameters
[in,out]sockSocket for which the handler is invoked.
[in]successTrue if the open was successful, false if the open failed. In case of error, the socket shall be considered as closed.

◆ handleSocketDisconnected()

virtual void ts::SocketHandlerInterface::handleSocketDisconnected ( TCPConnection sock,
bool  silent 
)
virtualinherited

Called when a TCP socket is disconnected.

Parameters
[in,out]sockSocket for which the handler is invoked.
[in]silentSame value as passed to disconnect() or close().

◆ handleSocketCloseStart()

virtual void ts::SocketHandlerInterface::handleSocketCloseStart ( Socket sock,
bool  silent 
)
virtualinherited

Called when a socket is about to be closed.

Never called when the application tries to close a socket which is not open.

Parameters
[in,out]sockSocket for which the handler is invoked.
[in]silentSame value as passed to close().

◆ handleSocketCloseComplete()

virtual void ts::SocketHandlerInterface::handleSocketCloseComplete ( Socket sock,
bool  silent,
bool  success 
)
virtualinherited

Called after a socket is closed.

Parameters
[in,out]sockSocket for which the handler is invoked.
[in]silentSame value as passed to close().
[in]successTrue if the close was successful, false if there was an error during the close. The socket shall be considered as closed on all cases, regardless of success.

The documentation for this class was generated from the following file: