Base class for TCP/IP sockets.
More...
#include <tsTCPSocket.h>
|
| TCPSocket ()=default |
| Constructor.
|
|
virtual | ~TCPSocket () override |
| Destructor.
|
|
bool | bind (const IPSocketAddress &addr, Report &report=(ts::CerrReport::Instance())) |
| Bind to a local address and port.
|
|
virtual bool | close (Report &report=(ts::CerrReport::Instance())) override |
| Close the socket.
|
|
IP | generation () const |
| Get the IP generation with which the socket was open.
|
|
bool | getLocalAddress (IPSocketAddress &addr, Report &report=(ts::CerrReport::Instance())) |
| Get local socket address.
|
|
SysSocketType | getSocket () const |
| Get the underlying socket device handle (use with care).
|
|
bool | isOpen () const |
| Check if socket is open.
|
|
virtual bool | open (IP gen, Report &report=(ts::CerrReport::Instance())) override |
| Open the socket.
|
|
bool | reusePort (bool reuse_port, Report &report=(ts::CerrReport::Instance())) |
| Set the "reuse port" option.
|
|
bool | setKeepAlive (bool active, Report &report=(ts::CerrReport::Instance())) |
| Set the "keep alive" option.
|
|
bool | setLingerTime (int seconds, Report &report=(ts::CerrReport::Instance())) |
| Set the linger time option.
|
|
bool | setNoDelay (bool active, Report &report=(ts::CerrReport::Instance())) |
| Set the "no delay" option.
|
|
bool | setNoLinger (Report &report=(ts::CerrReport::Instance())) |
| Remove the linger time option.
|
|
bool | setReceiveBufferSize (size_t size, Report &report=(ts::CerrReport::Instance())) |
| Set the receive buffer size.
|
|
bool | setReceiveTimeout (cn::milliseconds timeout, Report &report=(ts::CerrReport::Instance())) |
| Set the receive timeout.
|
|
bool | setSendBufferSize (size_t size, Report &report=(ts::CerrReport::Instance())) |
| Set the send buffer size.
|
|
bool | setTTL (int ttl, Report &report=(ts::CerrReport::Instance())) |
| Set the Time To Live (TTL) option.
|
|
|
bool | convert (IPAddress &addr, Report &report) const |
| Convert an IP address to make it compatible with the socket IP generation.
|
|
bool | createSocket (IP gen, int type, int protocol, Report &report) |
| Create the socket.
|
|
virtual void | declareOpened (SysSocketType sock, Report &report) override |
| Set an open socket descriptor from a subclass.
|
|
virtual void | handleClosed (Report &report) |
| This virtual method can be overriden by subclasses to be notified of close.
|
|
virtual void | handleOpened (Report &report) |
| This virtual method can be overriden by subclasses to be notified of open.
|
|
|
std::recursive_mutex | _mutex {} |
| Mutex protecting this object.
|
|
Base class for TCP/IP sockets.
This base class is not supposed to be directly instantiated. The two concrete subclasses of TCPSocket are:
- TCPServer: A TCP/IP server socket which listens to incoming connections. This type is socket is not designed to exchange data.
- TCPConnection: A TCP/IP session between a client and a server. This socket can exchange data.
- A TCP/IP client creates a TCPConnection instance and connects to a server.
- A TCP/IP server creates a TCPServer instance and waits for clients. For each client session, a TCPConnection instance is created.
◆ setTTL()
Set the Time To Live (TTL) option.
- Parameters
-
[in] | ttl | The TTL value, ie. the maximum number of "hops" between routers before an IP packet is dropped. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ setNoLinger()
Remove the linger time option.
- Parameters
-
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ setLingerTime()
Set the linger time option.
- Parameters
-
[in] | seconds | Number of seconds to wait after shuting down the socket. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ setKeepAlive()
Set the "keep alive" option.
- Parameters
-
[in] | active | If true, the socket periodically sends "keep alive" packets when the connection is idle. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ setNoDelay()
Set the "no delay" option.
- Parameters
-
[in] | active | If 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] | report | Where to report error. |
- 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. 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:
- IPSocketAddress::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] | addr | Local socket address to bind to. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ open()
Open the socket.
- Parameters
-
[in] | gen | IP generation, IPv4 or IPv6. If set to IP::Any, open an IPv6 socket (IPv4 connections allowed). |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
Implements ts::Socket.
◆ close()
Close the socket.
- Parameters
-
[in,out] | report | Where 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 | ) |
|
|
protectedvirtual |
This virtual method can be overriden by subclasses to be notified of open.
All subclasses should explicitly invoke their superclass' handlers.
- Parameters
-
[in,out] | report | Where to report error. |
◆ handleClosed()
virtual void ts::TCPSocket::handleClosed |
( |
Report & |
report | ) |
|
|
protectedvirtual |
This virtual method can be overriden by subclasses to be notified of close.
All subclasses should explicitly invoke their superclass' handlers.
- Parameters
-
[in,out] | report | Where to report error. |
Reimplemented in ts::TCPConnection.
◆ 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. |
[in,out] | report | Where 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.
◆ 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()
Set the send buffer size.
- Parameters
-
[in] | size | Send buffer size in bytes. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ setReceiveBufferSize()
Set the receive buffer size.
- Parameters
-
[in] | size | Receive buffer size in bytes. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ setReceiveTimeout()
Set the receive timeout.
- Parameters
-
[in] | timeout | Receive timeout in milliseconds. If negative, receive timeout is not used. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ reusePort()
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. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
◆ getLocalAddress()
Get local socket address.
- Parameters
-
[out] | addr | Local socket address of the connection. |
[in,out] | report | Where to report error. |
- 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.
◆ createSocket()
bool ts::Socket::createSocket |
( |
IP |
gen, |
|
|
int |
type, |
|
|
int |
protocol, |
|
|
Report & |
report |
|
) |
| |
|
protectedinherited |
Create the socket.
- Parameters
-
[in] | gen | IP generation. |
[in] | type | Socket type: SOCK_STREAM, SOCK_DGRAM |
[in] | protocol | Socket protocol: IPPROTO_TCP, IPPROTO_UDP |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
- See also
- open(ge, Report&)
◆ convert()
Convert an IP address to make it compatible with the socket IP generation.
- Parameters
-
| addr | [in,out] The address to convert. |
[in,out] | report | Where to report error. |
- Returns
- True on success, false on error.
The documentation for this class was generated from the following file: