uvw 3.1.0
Loading...
Searching...
No Matches
Public Member Functions | List of all members
uvw::tcp_handle Class Referencefinal

The TCP handle. More...

#include <tcp.h>

Inheritance diagram for uvw::tcp_handle:
Inheritance graph
[legend]
Collaboration diagram for uvw::tcp_handle:
Collaboration graph
[legend]

Public Member Functions

int init () final
 Initializes the handle. No socket is created as of yet.
 
int open (os_socket_handle socket)
 Opens an existing file descriptor or SOCKET as a TCP handle.
 
bool no_delay (bool value=false)
 Enables/Disables Nagle’s algorithm.
 
bool keep_alive (bool enable=false, time val=time{0})
 Enables/Disables TCP keep-alive.
 
bool simultaneous_accepts (bool enable=true)
 Enables/Disables simultaneous asynchronous accept requests.
 
int bind (const sockaddr &addr, tcp_flags opts=tcp_flags::_UVW_ENUM)
 Binds the handle to an address and port.
 
int bind (const std::string &ip, unsigned int port, tcp_flags opts=tcp_flags::_UVW_ENUM)
 Binds the handle to an address and port.
 
int bind (socket_address addr, tcp_flags opts=tcp_flags::_UVW_ENUM)
 Binds the handle to an address and port.
 
socket_address sock () const noexcept
 Gets the current address to which the handle is bound.
 
socket_address peer () const noexcept
 Gets the address of the peer connected to the handle.
 
int connect (const sockaddr &addr)
 Establishes an IPv4 or IPv6 TCP connection.
 
int connect (const std::string &ip, unsigned int port)
 Establishes an IPv4 or IPv6 TCP connection.
 
int connect (socket_address addr)
 Establishes an IPv4 or IPv6 TCP connection.
 
int close_reset ()
 Resets a TCP connection by sending a RST packet.
 
- Public Member Functions inherited from uvw::stream_handle< tcp_handle, uv_tcp_t >
int shutdown ()
 Shutdowns the outgoing (write) side of a duplex stream.
 
int listen (int backlog=DEFAULT_BACKLOG)
 Starts listening for incoming connections.
 
int accept (S &ref)
 Accepts incoming connections.
 
int read ()
 Starts reading data from an incoming stream.
 
int stop ()
 Stops reading data from the stream.
 
int write (std::unique_ptr< char[], Deleter > data, unsigned int len)
 Writes data to the stream.
 
int write (char *data, unsigned int len)
 Writes data to the stream.
 
int write (S &send, std::unique_ptr< char[], Deleter > data, unsigned int len)
 Extended write function for sending handles over a pipe handle.
 
int write (S &send, char *data, unsigned int len)
 Extended write function for sending handles over a pipe handle.
 
int try_write (std::unique_ptr< char[]> data, unsigned int len)
 Queues a write request if it can be completed immediately.
 
int try_write (std::unique_ptr< char[]> data, unsigned int len, stream_handle< V, W > &send)
 Queues a write request if it can be completed immediately.
 
int try_write (char *data, unsigned int len)
 Queues a write request if it can be completed immediately.
 
int try_write (char *data, unsigned int len, stream_handle< 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 write_queue_size () const noexcept
 Gets the amount of queued bytes waiting to be sent.
 
- Public Member Functions inherited from uvw::handle< T, U, E >
handle_category category () const noexcept
 Gets the category of the handle.
 
handle_type 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 send_buffer_size ()
 Gets the size of the send buffer used for the socket.
 
int send_buffer_size (int value)
 Sets the size of the send buffer used for the socket.
 
int recv_buffer_size ()
 Gets the size of the receive buffer used for the socket.
 
int recv_buffer_size (int value)
 Sets the size of the receive buffer used for the socket.
 
os_file_descriptor fd () const
 Gets the platform dependent file descriptor equivalent.
 
- Public Member Functions inherited from uvw::resource< T, U, close_event, E... >
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::uv_type< U >
virtual int init ()
 Initializes the handle.
 
loopparent () const noexcept
 Gets the loop from which the resource was originated.
 
const U * raw () const noexcept
 Gets the underlying raw data structure.
 
U * raw () noexcept
 Gets the underlying raw data structure.
 
- Public Member Functions inherited from uvw::emitter< T, E... >
void on (listener_t< U > f)
 Registers a long-lived listener with the event emitter.
 
void reset () noexcept
 Disconnects the listener for the given event type.
 
void reset () noexcept
 Disconnects all listeners.
 
bool has () const noexcept
 Checks if there is a listener registered for the specific event.
 

Detailed Description

The TCP 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 tcp_handle through a loop, arguments follow:

See the official documentation for further details.

Definition at line 43 of file tcp.h.

Member Function Documentation

◆ bind() [1/3]

int uvw::tcp_handle::bind ( const sockaddr &  addr,
tcp_flags  opts = tcp_flags::_UVW_ENUM 
)

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.

Available flags are:

  • tcp_handle::tcp_flags::IPV6ONLY: it disables dual-stack support and only IPv6 is used.
Parameters
addrInitialized sockaddr_in or sockaddr_in6 data structure.
optsOptional additional flags.
Returns
Underlying return value.

◆ bind() [2/3]

int uvw::tcp_handle::bind ( const std::string &  ip,
unsigned int  port,
tcp_flags  opts = tcp_flags::_UVW_ENUM 
)

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.

Available flags are:

  • tcp_handle::tcp_flags::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.
Returns
Underlying return value.

◆ bind() [3/3]

int uvw::tcp_handle::bind ( socket_address  addr,
tcp_flags  opts = tcp_flags::_UVW_ENUM 
)

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.

Available flags are:

  • tcp_handle::tcp_flags::IPV6ONLY: it disables dual-stack support and only IPv6 is used.
Parameters
addrA valid instance of socket_address.
optsOptional additional flags.
Returns
Underlying return value.

◆ close_reset()

int uvw::tcp_handle::close_reset ( )

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 close_reset calls is not allowed.

A close event is emitted when the connection has been reset.

Returns
Underlying return value.

◆ connect() [1/3]

int uvw::tcp_handle::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 connect event is emitted when the connection has been established.

Parameters
addrInitialized sockaddr_in or sockaddr_in6 data structure.
Returns
Underlying return value.

◆ connect() [2/3]

int uvw::tcp_handle::connect ( const std::string &  ip,
unsigned int  port 
)

Establishes an IPv4 or IPv6 TCP connection.

A connect event is emitted when the connection has been established.

Parameters
ipThe address to which to bind.
portThe port to which to bind.
Returns
Underlying return value.

◆ connect() [3/3]

int uvw::tcp_handle::connect ( socket_address  addr)

Establishes an IPv4 or IPv6 TCP connection.

A connect event is emitted when the connection has been established.

Parameters
addrA valid instance of socket_address.
Returns
Underlying return value.

◆ init()

int uvw::tcp_handle::init ( )
finalvirtual

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

Returns
Underlying return value.

Reimplemented from uvw::uv_type< U >.

◆ keep_alive()

bool uvw::tcp_handle::keep_alive ( bool  enable = false,
time  val = time{0} 
)

Enables/Disables TCP keep-alive.

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

◆ no_delay()

bool uvw::tcp_handle::no_delay ( bool  value = false)

Enables/Disables Nagle’s algorithm.

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

◆ open()

int uvw::tcp_handle::open ( os_socket_handle  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).
Returns
Underlying return value.

◆ peer()

socket_address uvw::tcp_handle::peer ( ) const
noexcept

Gets the address of the peer connected to the handle.

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

◆ simultaneous_accepts()

bool uvw::tcp_handle::simultaneous_accepts ( 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()

socket_address uvw::tcp_handle::sock ( ) const
noexcept

Gets the current address to which the handle is bound.

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

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