uvw 3.1.0
|
The TCP handle. More...
#include <tcp.h>
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. | |
loop & | parent () 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. | |
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.
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.addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
opts | Optional additional flags. |
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.ip | The address to which to bind. |
port | The port to which to bind. |
opts | Optional additional flags. |
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.addr | A valid instance of socket_address. |
opts | Optional additional flags. |
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.
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.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
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.
ip | The address to which to bind. |
port | The port to which to bind. |
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.
addr | A valid instance of socket_address. |
|
finalvirtual |
Initializes the handle. No socket is created as of yet.
Reimplemented from uvw::uv_type< U >.
bool uvw::tcp_handle::keep_alive | ( | bool | enable = false , |
time | val = time{0} |
||
) |
Enables/Disables TCP keep-alive.
enable | True to enable it, false otherwise. |
val | Initial delay in seconds (use std::chrono::duration<unsigned int> ). |
bool uvw::tcp_handle::no_delay | ( | bool | value = false | ) |
Enables/Disables Nagle’s algorithm.
value | True to enable it, false otherwise. |
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.
socket | A valid socket handle (either a file descriptor or a SOCKET). |
|
noexcept |
Gets the address of the peer connected to the handle.
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.
enable | True to enable it, false otherwise. |
|
noexcept |
Gets the current address to which the handle is bound.