uvw 2.12.1
|
#include <tcp.h>
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. | |
int | tryWrite (std::unique_ptr< char[]> data, unsigned int len) |
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. | |
int | recvBufferSize () |
Gets 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. | |
Public Member Functions inherited from uvw::UnderlyingType< TCPHandle, uv_tcp_t > | |
Loop & | loop () const noexcept |
Gets the loop from which the resource was originated. | |
const uv_tcp_t * | raw () const 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. | |
bool | empty () const noexcept |
Checks if there are listeners registered for the specific event. |
Additional Inherited Members | |
Static Public Member Functions inherited from uvw::UnderlyingType< TCPHandle, uv_tcp_t > | |
static std::shared_ptr< TCPHandle > | create (Args &&...args) |
Creates a new resource of the given type. |
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:
See the official documentation for further details.
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:
addr | A valid instance of Addr. |
opts | Optional additional flags. |
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:
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
opts | Optional additional flags. |
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:
ip | The address to which to bind. |
port | The port to which to bind. |
opts | Optional additional flags. |
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.
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.
addr | A valid instance of Addr. |
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.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
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.
ip | The address to which to bind. |
port | The port to which to bind. |
bool uvw::TCPHandle::init | ( | ) |
Initializes the handle. No socket is created as of yet.
bool uvw::TCPHandle::keepAlive | ( | bool | enable = false, |
Time | time = Time{0} ) |
Enables/Disables TCP keep-alive.
enable | True to enable it, false otherwise. |
time | Initial delay in seconds (use std::chrono::duration<unsigned int>). |
bool uvw::TCPHandle::noDelay | ( | bool | value = false | ) |
Enables/Disables Nagle’s algorithm.
value | True to enable it, false otherwise. |
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.
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::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.
enable | True to enable it, false otherwise. |
|
noexcept |
Gets the current address to which the handle is bound.