uvw 2.12.1
Loading...
Searching...
No Matches
uvw::TCPHandle Class Referencefinal

The TCPHandle handle. More...

#include <tcp.h>

Inheritance diagram for uvw::TCPHandle:
Collaboration diagram for uvw::TCPHandle:

Public Member Functions

bool init ()
 Initializes the handle. No socket is created as of yet.
 
void open (OSSocketHandle socket)
 Opens an existing file descriptor or SOCKET as a TCP handle.
 
bool noDelay (bool value=false)
 Enables/Disables Nagle’s algorithm.
 
bool keepAlive (bool enable=false, Time time=Time{0})
 Enables/Disables TCP keep-alive.
 
bool simultaneousAccepts (bool enable=true)
 Enables/Disables simultaneous asynchronous accept requests.
 
void bind (const sockaddr &addr, Flags< Bind > opts=Flags< Bind >{})
 Binds the handle to an address and port.
 
template<typename I = IPv4>
void bind (const std::string &ip, unsigned int port, Flags< Bind > opts=Flags< Bind >{})
 Binds the handle to an address and port.
 
template<typename I = IPv4>
void bind (Addr addr, Flags< Bind > opts=Flags< Bind >{})
 Binds the handle to an address and port.
 
template<typename I = IPv4>
Addr sock () const noexcept
 Gets the current address to which the handle is bound.
 
template<typename I = IPv4>
Addr peer () const noexcept
 Gets the address of the peer connected to the handle.
 
void connect (const sockaddr &addr)
 Establishes an IPv4 or IPv6 TCP connection.
 
template<typename I = IPv4>
void connect (const std::string &ip, unsigned int port)
 Establishes an IPv4 or IPv6 TCP connection.
 
template<typename I = IPv4>
void connect (Addr addr)
 Establishes an IPv4 or IPv6 TCP connection.
 
void closeReset ()
 Resets a TCP connection by sending a RST packet.
 
- Public Member Functions inherited from uvw::StreamHandle< TCPHandle, uv_tcp_t >
void shutdown ()
 Shutdowns the outgoing (write) side of a duplex stream.
 
void listen (int backlog=DEFAULT_BACKLOG)
 Starts listening for incoming connections.
 
void accept (S &ref)
 Accepts incoming connections.
 
void read ()
 Starts reading data from an incoming stream.
 
void stop ()
 Stops reading data from the stream.
 
void write (std::unique_ptr< char[], Deleter > data, unsigned int len)
 Writes data to the stream.
 
void write (char *data, unsigned int len)
 Writes data to the stream.
 
void write (S &send, std::unique_ptr< char[], Deleter > data, unsigned int len)
 Extended write function for sending handles over a pipe handle.
 
void write (S &send, char *data, unsigned int len)
 Extended write function for sending handles over a pipe handle.
 
int tryWrite (std::unique_ptr< char[]> data, unsigned int len)
 Queues a write request if it can be completed immediately.
 
int tryWrite (std::unique_ptr< char[]> data, unsigned int len, StreamHandle< V, W > &send)
 Queues a write request if it can be completed immediately.
 
int tryWrite (char *data, unsigned int len)
 Queues a write request if it can be completed immediately.
 
int tryWrite (char *data, unsigned int len, StreamHandle< V, W > &send)
 Queues a write request if it can be completed immediately.
 
bool readable () const noexcept
 Checks if the stream is readable.
 
bool writable () const noexcept
 Checks if the stream is writable.
 
bool blocking (bool enable=false)
 Enables or disables blocking mode for a stream.
 
size_t writeQueueSize () const noexcept
 Gets the amount of queued bytes waiting to be sent.
 
- Public Member Functions inherited from uvw::Handle< TCPHandle, uv_tcp_t >
HandleCategory category () const noexcept
 Gets the category of the handle.
 
HandleType type () const noexcept
 Gets the type of the handle.
 
bool active () const noexcept
 Checks if the handle is active.
 
bool closing () const noexcept
 Checks if a handle is closing or closed.
 
void close () noexcept
 Request handle to be closed.
 
void reference () noexcept
 Reference the given handle.
 
void unreference () noexcept
 Unreference the given handle.
 
bool referenced () const noexcept
 Checks if the given handle referenced.
 
std::size_t size () const noexcept
 Returns the size of the underlying handle type.
 
int sendBufferSize ()
 Gets the size of the send buffer used for the socket.
 
bool sendBufferSize (int value)
 Sets the size of the send buffer used for the socket.
 
int recvBufferSize ()
 Gets the size of the receive buffer used for the socket.
 
bool recvBufferSize (int value)
 Sets the size of the receive buffer used for the socket.
 
OSFileDescriptor fd () const
 Gets the platform dependent file descriptor equivalent.
 
- Public Member Functions inherited from uvw::Resource< TCPHandle, uv_tcp_t >
std::shared_ptr< R > data () const
 Gets user-defined data. uvw won't use this field in any case.
 
void data (std::shared_ptr< void > uData)
 Sets arbitrary data. uvw won't use this field in any case.
 
- Public Member Functions inherited from uvw::UnderlyingType< TCPHandle, uv_tcp_t >
Looploop () const noexcept
 Gets the loop from which the resource was originated.
 
const uv_tcp_t * raw () const noexcept
 Gets the underlying raw data structure.
 
uv_tcp_t * raw () noexcept
 Gets the underlying raw data structure.
 
- Public Member Functions inherited from uvw::Emitter< TCPHandle >
Connection< E > on (Listener< E > f)
 Registers a long-lived listener with the event emitter.
 
Connection< E > once (Listener< E > f)
 Registers a short-lived listener with the event emitter.
 
void erase (Connection< E > conn) noexcept
 Disconnects a listener from the event emitter.
 
void clear () noexcept
 Disconnects all the listeners for the given event type.
 
void clear () noexcept
 Disconnects all the listeners.
 
bool empty () const noexcept
 Checks if there are listeners registered for the specific event.
 
bool empty () const noexcept
 Checks if there are listeners registered with the event emitter.
 

Additional Inherited Members

- Public Types inherited from uvw::Emitter< TCPHandle >
- Static Public Member Functions inherited from uvw::UnderlyingType< TCPHandle, uv_tcp_t >
static std::shared_ptr< TCPHandlecreate (Args &&...args)
 Creates a new resource of the given type.
 
- Protected Types inherited from uvw::Resource< TCPHandle, uv_tcp_t >
- Protected Member Functions inherited from uvw::Handle< TCPHandle, uv_tcp_t >
- Protected Member Functions inherited from uvw::Resource< TCPHandle, uv_tcp_t >
- Protected Member Functions inherited from uvw::UnderlyingType< TCPHandle, uv_tcp_t >
- Protected Member Functions inherited from uvw::Emitter< TCPHandle >
- Static Protected Member Functions inherited from uvw::Handle< TCPHandle, uv_tcp_t >

Detailed Description

The TCPHandle handle.

TCP handles are used to represent both TCP streams and servers.
By default, IPv4 is used as a template parameter. The handle already supports IPv6 out-of-the-box by using uvw::IPv6.

To create a TCPHandle through a Loop, arguments follow:

  • An optional integer value that indicates the flags used to initialize the socket.

See the official documentation for further details.

Definition at line 40 of file tcp.h.

Member Function Documentation

◆ bind() [1/3]

template<typename I = IPv4>
void uvw::TCPHandle::bind ( Addr addr,
Flags< Bind > opts = Flags< Bind >{} )

Binds the handle to an address and port.

A successful call to this function does not guarantee that the call to listen() or connect() will work properly.
ErrorEvent events can be emitted because of either this function or the ones mentioned above.

Available flags are:

  • TCPHandle::Bind::IPV6ONLY: it disables dual-stack support and only IPv6 is used.
Parameters
addrA valid instance of Addr.
optsOptional additional flags.

◆ bind() [2/3]

void uvw::TCPHandle::bind ( const sockaddr & addr,
Flags< Bind > opts = Flags< Bind >{} )

Binds the handle to an address and port.

A successful call to this function does not guarantee that the call to listen() or connect() will work properly.
ErrorEvent events can be emitted because of either this function or the ones mentioned above.

Available flags are:

  • TCPHandle::Bind::IPV6ONLY: it disables dual-stack support and only IPv6 is used.
Parameters
addrInitialized sockaddr_in or sockaddr_in6 data structure.
optsOptional additional flags.

◆ bind() [3/3]

template<typename I = IPv4>
void uvw::TCPHandle::bind ( const std::string & ip,
unsigned int port,
Flags< Bind > opts = Flags< Bind >{} )

Binds the handle to an address and port.

A successful call to this function does not guarantee that the call to listen() or connect() will work properly.
ErrorEvent events can be emitted because of either this function or the ones mentioned above.

Available flags are:

  • TCPHandle::Bind::IPV6ONLY: it disables dual-stack support and only IPv6 is used.
Parameters
ipThe address to which to bind.
portThe port to which to bind.
optsOptional additional flags.

◆ closeReset()

void uvw::TCPHandle::closeReset ( )

Resets a TCP connection by sending a RST packet.

This is accomplished by setting the SO_LINGER socket option with a linger interval of zero and then calling close.
Due to some platform inconsistencies, mixing of shutdown and closeReset calls is not allowed.

A CloseEvent event is emitted when the connection has been reset.
An ErrorEvent event is emitted in case of errors.

◆ connect() [1/3]

template<typename I = IPv4>
void uvw::TCPHandle::connect ( Addr addr)

Establishes an IPv4 or IPv6 TCP connection.

A ConnectEvent event is emitted when the connection has been established.
An ErrorEvent event is emitted in case of errors during the connection.

Parameters
addrA valid instance of Addr.

◆ connect() [2/3]

void uvw::TCPHandle::connect ( const sockaddr & addr)

Establishes an IPv4 or IPv6 TCP connection.

On Windows if the addr is initialized to point to an unspecified address (0.0.0.0 or ::) it will be changed to point to localhost. This is done to match the behavior of Linux systems.

A ConnectEvent event is emitted when the connection has been established.
An ErrorEvent event is emitted in case of errors during the connection.

Parameters
addrInitialized sockaddr_in or sockaddr_in6 data structure.

◆ connect() [3/3]

template<typename I = IPv4>
void uvw::TCPHandle::connect ( const std::string & ip,
unsigned int port )

Establishes an IPv4 or IPv6 TCP connection.

A ConnectEvent event is emitted when the connection has been established.
An ErrorEvent event is emitted in case of errors during the connection.

Parameters
ipThe address to which to bind.
portThe port to which to bind.

◆ init()

bool uvw::TCPHandle::init ( )

Initializes the handle. No socket is created as of yet.

Returns
True in case of success, false otherwise.

◆ keepAlive()

bool uvw::TCPHandle::keepAlive ( bool enable = false,
Time time = Time{0} )

Enables/Disables TCP keep-alive.

Parameters
enableTrue to enable it, false otherwise.
timeInitial delay in seconds (use std::chrono::duration<unsigned int>).
Returns
True in case of success, false otherwise.

◆ noDelay()

bool uvw::TCPHandle::noDelay ( bool value = false)

Enables/Disables Nagle’s algorithm.

Parameters
valueTrue to enable it, false otherwise.
Returns
True in case of success, false otherwise.

◆ open()

void uvw::TCPHandle::open ( OSSocketHandle socket)

Opens an existing file descriptor or SOCKET as a TCP handle.

The passed file descriptor or SOCKET is not checked for its type, but it’s required that it represents a valid stream socket.

Parameters
socketA valid socket handle (either a file descriptor or a SOCKET).

◆ peer()

template<typename I = IPv4>
Addr uvw::TCPHandle::peer ( ) const
noexcept

Gets the address of the peer connected to the handle.

Returns
A valid instance of Addr, an empty one in case of errors.

◆ simultaneousAccepts()

bool uvw::TCPHandle::simultaneousAccepts ( bool enable = true)

Enables/Disables simultaneous asynchronous accept requests.

Enables/Disables simultaneous asynchronous accept requests that are queued by the operating system when listening for new TCP connections.
This setting is used to tune a TCP server for the desired performance. Having simultaneous accepts can significantly improve the rate of accepting connections (which is why it is enabled by default) but may lead to uneven load distribution in multi-process setups.

Parameters
enableTrue to enable it, false otherwise.
Returns
True in case of success, false otherwise.

◆ sock()

template<typename I = IPv4>
Addr uvw::TCPHandle::sock ( ) const
noexcept

Gets the current address to which the handle is bound.

Returns
A valid instance of Addr, an empty one in case of errors.

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