TSDuck Version 3.15-964 (TSDuck - The MPEG Transport Stream Toolkit)
ts::tlv::Connection< MUTEX > Class Template Reference

TCP connection using TLV messages. More...

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

Public Types

typedef ts::TCPConnection SuperClass
 Reference to superclass.
 

Public Member Functions

 Connection (const Protocol *protocol, bool auto_error_response=true, size_t max_invalid_msg=0)
 Constructor. More...
 
bool bind (const SocketAddress &addr, Report &report=(*ts::CerrReport::Instance()))
 Bind to a local address and port. More...
 
virtual bool close (Report &report=(*ts::CerrReport::Instance())) override
 Close the socket. More...
 
bool closeWriter (Report &report=(*ts::CerrReport::Instance()))
 Close the write direction of the connection. More...
 
bool connect (const SocketAddress &addr, Report &report=(*ts::CerrReport::Instance()))
 Connect to a remote address and port. More...
 
bool disconnect (Report &report=(*ts::CerrReport::Instance()))
 Disconnect from remote partner. More...
 
bool getAutoErrorResponse () const
 Get invalid incoming messages processing. More...
 
bool getLocalAddress (SocketAddress &addr, Report &report=(*ts::CerrReport::Instance()))
 Get local socket address. More...
 
size_t getMaxInvalidMessages () const
 Get invalid message threshold. More...
 
bool getPeer (SocketAddress &addr, Report &report=(*ts::CerrReport::Instance())) const
 Get the connected remote peer. More...
 
platform_specific getSocket () const
 Get the underlying socket device handle (use with care). More...
 
bool isConnected () const
 Check if the socket is connected. More...
 
bool isOpen () const
 Check if socket is open. More...
 
virtual bool open (Report &report=(*ts::CerrReport::Instance())) override
 Open the socket. More...
 
UString peerName () const
 Get the connected remote peer as a string. More...
 
bool receive (MessagePtr &msg, const AbortInterface *abort, Report &report)
 Receive a TLV message. More...
 
bool receive (MessagePtr &msg, const AbortInterface *abort, Logger &logger)
 Receive a TLV message. More...
 
bool receive (void *buffer, size_t max_size, size_t &ret_size, const AbortInterface *abort=0, Report &report=(*ts::CerrReport::Instance()))
 Receive data. More...
 
bool receive (void *buffer, size_t size, const AbortInterface *abort=0, Report &report=(*ts::CerrReport::Instance()))
 Receive data until buffer is full. More...
 
bool reusePort (bool reuse_port, Report &report=(*ts::CerrReport::Instance()))
 Set the "reuse port" option. More...
 
bool send (const Message &msg, Report &report)
 Serialize and send a TLV message. More...
 
bool send (const Message &msg, Logger &logger)
 Serialize and send a TLV message. More...
 
bool send (const void *data, size_t size, Report &report=(*ts::CerrReport::Instance()))
 Send data. More...
 
void setAutoErrorResponse (bool on)
 Set invalid incoming messages processing. More...
 
bool setKeepAlive (bool active, Report &report=(*ts::CerrReport::Instance()))
 Set the "keep alive" option. More...
 
bool setLingerTime (int seconds, Report &report=(*ts::CerrReport::Instance()))
 Set the linger time option. More...
 
void setMaxInvalidMessages (size_t n)
 Set invalid message threshold. More...
 
bool setNoDelay (bool active, Report &report=(*ts::CerrReport::Instance()))
 Set the "no delay" option. More...
 
bool setNoLinger (Report &report=(*ts::CerrReport::Instance()))
 Remove the linger time option. More...
 
bool setReceiveBufferSize (size_t size, Report &report=(*ts::CerrReport::Instance()))
 Set the receive buffer size. More...
 
bool setReceiveTimeout (MilliSecond timeout, Report &report=(*ts::CerrReport::Instance()))
 Set the receive timeout. More...
 
bool setSendBufferSize (size_t size, Report &report=(*ts::CerrReport::Instance()))
 Set the send buffer size. More...
 
bool setTTL (int ttl, Report &report=(*ts::CerrReport::Instance()))
 Set the Time To Live (TTL) option. More...
 

Protected Member Functions

bool createSocket (int domain, int type, int protocol, Report &report)
 Create the socket. More...
 
virtual void declareOpened (platform_specific sock, Report &report) override
 Set an open socket descriptor from a subclass. More...
 
virtual void handleClosed (Report &report=(*ts::CerrReport::Instance())) override
 This virtual method can be overriden by subclasses to be notified of close. More...
 
virtual void handleConnected (Report &) override
 This virtual method can be overriden by subclasses to be notified of connection. More...
 
virtual void handleDisconnected (Report &report=(*ts::CerrReport::Instance()))
 This virtual method can be overriden by subclasses to be notified of disconnection. More...
 
virtual void handleOpened (Report &report)
 This virtual method can be overriden by subclasses to be notified of open. More...
 

Protected Attributes

Mutex _mutex
 Mutex protecting this object.
 

Detailed Description

template<class MUTEX = Mutex>
class ts::tlv::Connection< MUTEX >

TCP connection using TLV messages.

Template Parameters
MUTEXMutex type for synchronization. Serialization & deserialization need synchronized access. By default, use thread-safe implementation. Instantiate with MUTEX = NullMutex for mono-thread appli.

Constructor & Destructor Documentation

◆ Connection()

template<class MUTEX = Mutex>
ts::tlv::Connection< MUTEX >::Connection ( const Protocol protocol,
bool  auto_error_response = true,
size_t  max_invalid_msg = 0 
)

Constructor.

Parameters
[in]protocolThe incoming messages are interpreted according to this protocol.
[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.

Member Function Documentation

◆ send() [1/3]

template<class MUTEX = Mutex>
bool ts::tlv::Connection< MUTEX >::send ( const Message msg,
Report report 
)

Serialize and send a TLV message.

Parameters
[in]msgThe message to send.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ send() [2/3]

template<class MUTEX = Mutex>
bool ts::tlv::Connection< MUTEX >::send ( const Message msg,
Logger logger 
)

Serialize and send a TLV message.

Parameters
[in]msgThe message to send.
[in,out]loggerWhere to report errors and messages.
Returns
True on success, false on error.

◆ receive() [1/4]

template<class MUTEX = Mutex>
bool ts::tlv::Connection< MUTEX >::receive ( MessagePtr msg,
const AbortInterface abort,
Report report 
)

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).
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ receive() [2/4]

template<class MUTEX = Mutex>
bool ts::tlv::Connection< MUTEX >::receive ( MessagePtr msg,
const AbortInterface abort,
Logger logger 
)

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).
[in,out]loggerWhere to report errors and messages.
Returns
True on success, false on error.

◆ getAutoErrorResponse()

template<class MUTEX = Mutex>
bool ts::tlv::Connection< MUTEX >::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<class MUTEX = Mutex>
void ts::tlv::Connection< MUTEX >::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<class MUTEX = Mutex>
size_t ts::tlv::Connection< MUTEX >::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<class MUTEX = Mutex>
void ts::tlv::Connection< MUTEX >::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.

◆ handleConnected()

template<class MUTEX = Mutex>
virtual void ts::tlv::Connection< MUTEX >::handleConnected ( Report report)
overrideprotectedvirtual

This virtual method can be overriden by subclasses to be notified of connection.

All subclasses should explicitly invoke their superclass' handlers.

Parameters
[in,out]reportWhere to report error.

Reimplemented from ts::TCPConnection.

◆ connect()

bool ts::TCPConnection::connect ( const SocketAddress addr,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

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.
[in,out]reportWhere to report error.
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 ( SocketAddress addr,
Report report = (*ts::CerrReport::Instance()) 
) const
inherited

Get the connected remote peer.

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

◆ peerName()

UString ts::TCPConnection::peerName ( ) const
inherited

Get the connected remote peer as a string.

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

◆ closeWriter()

bool ts::TCPConnection::closeWriter ( Report report = (*ts::CerrReport::Instance()))
inherited

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,out]reportWhere to report error.
Returns
True on success, false on error.

◆ disconnect()

bool ts::TCPConnection::disconnect ( Report report = (*ts::CerrReport::Instance()))
inherited

Disconnect from remote partner.

Parameters
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ send() [3/3]

bool ts::TCPConnection::send ( const void *  data,
size_t  size,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Send data.

Parameters
[in]dataAddress of the data to send.
[in]sizeSize in bytes of the data to send.
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ receive() [3/4]

bool ts::TCPConnection::receive ( void *  buffer,
size_t  max_size,
size_t &  ret_size,
const AbortInterface abort = 0,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Receive data.

This version of receive() 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]reportWhere to report error.
Returns
True on success, false on error.

◆ receive() [4/4]

bool ts::TCPConnection::receive ( void *  buffer,
size_t  size,
const AbortInterface abort = 0,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

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.

The version is typically useful when the application knows that a certain amount of data is expected and must wait for them.

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).
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ handleDisconnected()

virtual void ts::TCPConnection::handleDisconnected ( Report report = (*ts::CerrReport::Instance()))
inlineprotectedvirtualinherited

This virtual method can be overriden by subclasses to be notified of disconnection.

All subclasses should explicitly invoke their superclass' handlers.

Parameters
[in,out]reportWhere to report error.

◆ handleClosed()

virtual void ts::TCPConnection::handleClosed ( Report report = (*ts::CerrReport::Instance()))
overrideprotectedvirtualinherited

This virtual method can be overriden by subclasses to be notified of close.

All subclasses should explicitly invoke their superclass' handlers.

Parameters
[in,out]reportWhere to report error.

Reimplemented from ts::TCPSocket.

◆ setTTL()

bool ts::TCPSocket::setTTL ( int  ttl,
Report report = (*ts::CerrReport::Instance()) 
)
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.
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ setNoLinger()

bool ts::TCPSocket::setNoLinger ( Report report = (*ts::CerrReport::Instance()))
inherited

Remove the linger time option.

Parameters
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ setLingerTime()

bool ts::TCPSocket::setLingerTime ( int  seconds,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Set the linger time option.

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

◆ setKeepAlive()

bool ts::TCPSocket::setKeepAlive ( bool  active,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Set the "keep alive" option.

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

◆ setNoDelay()

bool ts::TCPSocket::setNoDelay ( bool  active,
Report report = (*ts::CerrReport::Instance()) 
)
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.
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ bind()

bool ts::TCPSocket::bind ( const SocketAddress addr,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Bind to a local address and port.

The IP address part of the socket address must one of:

  • IPAddress::AnyAddress. Any local interface may be used to connect to a server (client side) or to receive incoming client connections (server side).
  • The IP address of an interface of the local system. Outgoing connections (client side) will be only allowed through this interface. Incoming client connections (server side) will be accepted only when they arrive through the selected interface.

The port number part of the socket address must be one of:

  • SocketAddress::AnyPort. The socket is bound to an arbitrary unused local TCP port. This is the usual configuration for a TCP client.
  • A specific port number. This is the usual configuration for a TCP server. If this TCP port is already bound by another local TCP socket, the bind operation fails, unless the "reuse port" option has already been set.
Parameters
[in]addrLocal socket address to bind to.
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ open()

virtual bool ts::TCPSocket::open ( Report report = (*ts::CerrReport::Instance()))
overridevirtualinherited

Open the socket.

Parameters
[in,out]reportWhere to report error.
Returns
True on success, false on error.

Implements ts::Socket.

◆ close()

virtual bool ts::TCPSocket::close ( Report report = (*ts::CerrReport::Instance()))
overridevirtualinherited

Close the socket.

Parameters
[in,out]reportWhere to report error.
Returns
True on success, false on error.

Reimplemented from ts::Socket.

Reimplemented in ts::TCPServer.

◆ handleOpened()

virtual void ts::TCPSocket::handleOpened ( Report report)
inlineprotectedvirtualinherited

This virtual method can be overriden by subclasses to be notified of open.

All subclasses should explicitly invoke their superclass' handlers.

Parameters
[in,out]reportWhere to report error.

◆ declareOpened()

virtual void ts::TCPSocket::declareOpened ( platform_specific  sock,
Report report 
)
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.
[in,out]reportWhere to report error.

Reimplemented from ts::Socket.

◆ isOpen()

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

Check if socket is open.

Returns
True if socket is open.

◆ setSendBufferSize()

bool ts::Socket::setSendBufferSize ( size_t  size,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Set the send buffer size.

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

◆ setReceiveBufferSize()

bool ts::Socket::setReceiveBufferSize ( size_t  size,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Set the receive buffer size.

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

◆ setReceiveTimeout()

bool ts::Socket::setReceiveTimeout ( MilliSecond  timeout,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Set the receive timeout.

Parameters
[in]timeoutReceive timeout in milliseconds. If negative, receive timeout is not used.
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ reusePort()

bool ts::Socket::reusePort ( bool  reuse_port,
Report report = (*ts::CerrReport::Instance()) 
)
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.
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ getLocalAddress()

bool ts::Socket::getLocalAddress ( SocketAddress addr,
Report report = (*ts::CerrReport::Instance()) 
)
inherited

Get local socket address.

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

◆ getSocket()

platform_specific 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 TS_SOCKET_T_INVALID if the socket is not open.

◆ createSocket()

bool ts::Socket::createSocket ( int  domain,
int  type,
int  protocol,
Report report 
)
protectedinherited

Create the socket.

Parameters
[in]domainSocket domain: PF_INET
[in]typeSocket type: SOCK_STREAM, SOCK_DGRAM
[in]protocolSocket protocol: IPPROTO_TCP, IPPROTO_UDP
[in,out]reportWhere to report error.
Returns
True on success, false on error.

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