TSDuck Version 3.15-955 (TSDuck - The MPEG Transport Stream Toolkit)
ts::TCPServer Class Reference

Implementation of a TCP/IP server. More...

Inheritance diagram for ts::TCPServer:
Collaboration diagram for ts::TCPServer:

Public Types

typedef TCPSocket SuperClass
 Reference to the superclass.
 

Public Member Functions

 TCPServer ()
 Constructor.
 
bool accept (TCPConnection &client, SocketAddress &addr, Report &report=(*ts::CerrReport::Instance()))
 Wait for an incoming client connection. 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 getLocalAddress (SocketAddress &addr, Report &report=(*ts::CerrReport::Instance()))
 Get local socket address. More...
 
platform_specific getSocket () const
 Get the underlying socket device handle (use with care). More...
 
bool isOpen () const
 Check if socket is open. More...
 
bool listen (int backlog, Report &report=(*ts::CerrReport::Instance()))
 Start the server. More...
 
virtual bool open (Report &report=(*ts::CerrReport::Instance())) override
 Open the socket. More...
 
bool reusePort (bool reuse_port, Report &report=(*ts::CerrReport::Instance()))
 Set the "reuse port" option. 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...
 
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)
 This virtual method can be overriden by subclasses to be notified of close. 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

Implementation of a TCP/IP server.

The following lists the typical server-side scenario in the correct order. Many steps such as setting socket options are optional. The symbol [*] means mandatory. Depending on the platform, some options settings are sensitive to the order. The following order has proven to work on most platforms.

Invoking close() is optional since the destructor of the class will properly close the socket if not already done.

Member Function Documentation

◆ listen()

bool ts::TCPServer::listen ( int  backlog,
Report report = (*ts::CerrReport::Instance()) 
)

Start the server.

Here, starting the server means starting to listen to incoming client connections. Internally to the kernel, the incoming connections are queued up to backlog. When the method accept() is invoked and some incoming connections are already queued in the kernel, the oldest one is immediately accepted. Otherwise, accept() blocks until a new incoming connection arrives.

Parameters
[in]backlogMaximum number of incoming connections which allowed to queue in the kernel until the next call to accept(). Note that this value is a minimum queue size. But the kernel may accept more. There is no guarantee that additional incoming connections will be rejected if more than backlog are already queueing.
[in,out]reportWhere to report error.
Returns
True on success, false on error.

◆ accept()

bool ts::TCPServer::accept ( TCPConnection client,
SocketAddress addr,
Report report = (*ts::CerrReport::Instance()) 
)

Wait for an incoming client connection.

Parameters
[out]clientThis object receives the new connection. Upon successful return from accept(), the TCPConnection object is a properly connected TCP session. Once the connection is completed, the TCPConnection objects on the client side and the server side are symmetric and can be used the same way.
[out]addrThis object receives the socket address of the client. If the server wants to filter client connections based on their IP address, it may use addr for that.
[in,out]reportWhere to report error.
Returns
True on success, false on error.
See also
listen()

◆ close()

virtual bool ts::TCPServer::close ( Report report = (*ts::CerrReport::Instance()))
overridevirtual

Close the socket.

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

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

◆ handleClosed()

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

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 in ts::TCPConnection.

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