7#include "../include/io_context.hpp"
8#include <event2/bufferevent.h>
17class Connection :
public std::enable_shared_from_this<Connection> {
18 using length_header = int32_t;
19 static constexpr size_t header_size{
sizeof(length_header )};
22 using event_ptr = std::unique_ptr<bufferevent,
decltype(&bufferevent_free)>;
80 void send_async(std::string_view msg, std::function<
void()> callback);
91 void recv_async(std::vector<std::byte> buf, std::function<
void()> callback);
115 socket_fd_{ socket_fd },
116 io_context_{ io_context },
117 event_(nullptr, bufferevent_free) {}
121 std::shared_ptr<async::IOContext> io_context_;
188 int32_t listening_socket_fd_;
std::shared_ptr< async::IOContext > io_context_ptr
Definition io_context.hpp:14
Accepts connections and assigns a dedicated TCPConnection object.
Definition tcp.hpp:130
Connection::connection_ptr accept() const
Accepts incoming connections synchronously.
Acceptor(std::string port, int32_t domain)
Constructor for the Acceptor object.
Acceptor(Acceptor &&other) noexcept
Copy constructor for the Acceptor object.
Acceptor & operator=(Acceptor &&other) noexcept
Copy operator for the Acceptor object.
void bind() const
Binds the socket handle to the host machine's address.
void listen() const
Starts listening on the socket for incoming connections.
~Acceptor()
Destructor of the Acceptor object.
Represents a TCP connection.
Definition tcp.hpp:17
void set_nonblocking()
Sets the underlying socket handle as nonblocking and creates a bufferevent for the socket.
void recv_sync(std::string &buf)
Receives a message from the endpoint synchronously.
Connection & operator=(Connection &&other) noexcept
The move constructor for the Connection class.
Connection(Connection &&other) noexcept
The move constructor for the Connection class.
std::unique_ptr< bufferevent, decltype(&bufferevent_free)> event_ptr
Definition tcp.hpp:22
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.
static connection_ptr create(int32_t socket_fd, async::IOContext::io_context_ptr io_context)
Creates a shared ptr to a new Connection instance.
std::shared_ptr< Connection > connection_ptr
Definition tcp.hpp:21
void send_sync(std::string_view msg)
Sends a message through the endpoint synchronously.
bool is_nonblocking()
Checks if the underlying socket handle is nonblocking.