|
Net++
A class-based C++ encapsulation over the POSIX Sockets API
|
Represents a TCP connection. More...
#include <tcp.hpp>
Public Types | |
| using | connection_ptr = std::shared_ptr< Connection > |
| using | event_ptr = std::unique_ptr< bufferevent, decltype(&bufferevent_free)> |
Public Member Functions | |
| Connection (Connection &&other) noexcept | |
| The move constructor for the Connection class. | |
| Connection & | operator= (Connection &&other) noexcept |
| The move constructor for the Connection class. | |
| void | set_nonblocking () |
| Sets the underlying socket handle as nonblocking and creates a bufferevent for the socket. | |
| bool | is_nonblocking () |
| Checks if the underlying socket handle is nonblocking. | |
| void | send_async (std::string_view msg, std::function< void()> callback) |
| Sends a message through the endpoint asynchronously. | |
| void | recv_async (std::vector< std::byte > buf, std::function< void()> callback) |
| Receives a message from the endpoint asynchronously. | |
| void | send_sync (std::string_view msg) |
| Sends a message through the endpoint synchronously. | |
| void | recv_sync (std::string &buf) |
| Receives a message from the endpoint synchronously. | |
Static Public Member Functions | |
| static connection_ptr | create (int32_t socket_fd, async::IOContext::io_context_ptr io_context) |
| Creates a shared ptr to a new Connection instance. | |
Represents a TCP connection.
Encapsulates a connected socket, allowing send and receive capabilities, as well as I/O multiplexing using libevent.
| using tcp::Connection::connection_ptr = std::shared_ptr<Connection> |
| using tcp::Connection::event_ptr = std::unique_ptr<bufferevent, decltype(&bufferevent_free)> |
|
noexcept |
The move constructor for the Connection class.
Moves the source socket handle, io context, and event to the current Connection instance, and invalidates the source instance's socket handle.
| other | An rvalue reference to the object to steal resources from |
|
static |
Creates a shared ptr to a new Connection instance.
| socket_fd | The Connected socket file descriptor to pass into the private constructor |
| io_context | An optional IOContext object |
| bool tcp::Connection::is_nonblocking | ( | ) |
Checks if the underlying socket handle is nonblocking.
|
noexcept |
The move constructor for the Connection class.
Closes the current Connection's socket handle if applicable, moves the source socket handle, io context, and event to the current connection instance , and invalidates the source instance's socket handle.
| other | An rvalue reference to the object to steal resources from |
| void tcp::Connection::recv_async | ( | std::vector< std::byte > | buf, |
| std::function< void()> | callback | ||
| ) |
Receives a message from the endpoint asynchronously.
Only use when the socket is set to nonblocking. Asynchronously receives all bytes into buf and calls the callback function when the operation is completed
| buf | The buffer of bytes to receive data |
| callback | The function to call once the receive operation completes |
| void tcp::Connection::recv_sync | ( | std::string & | buf | ) |
Receives a message from the endpoint synchronously.
Only use when is_nonblocking() returns false. Synchronously receives all bytes into buf.
| buf | The buffer of bytes to receive data |
| void tcp::Connection::send_async | ( | std::string_view | msg, |
| std::function< void()> | callback | ||
| ) |
Sends a message through the endpoint asynchronously.
Only use when the socket is set to nonblocking. Asynchronously sends all bytes in msg and calls the callback function when the operation is completed
| msg | The buffer of bytes to send |
| callback | The function to call once the send operation completes |
| void tcp::Connection::send_sync | ( | std::string_view | msg | ) |
Sends a message through the endpoint synchronously.
Only use when is_nonblocking() returns false. Synchronously sends all bytes in msg.
| msg | the buffer of bytes to send |
| void tcp::Connection::set_nonblocking | ( | ) |
Sets the underlying socket handle as nonblocking and creates a bufferevent for the socket.