Net++
A class-based C++ encapsulation over the POSIX Sockets API
Loading...
Searching...
No Matches
tcp.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <string_view>
6#include <sys/socket.h>
7#include "../include/io_context.hpp"
8#include <event2/bufferevent.h>
9
10namespace tcp {
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 )};
20public:
21 using connection_ptr = std::shared_ptr<Connection>;
22 using event_ptr = std::unique_ptr<bufferevent, decltype(&bufferevent_free)>;
24 Connection(const Connection&) = delete;
25 Connection& operator=(const Connection&) = delete;
27
36 Connection(Connection&& other) noexcept;
37
47 Connection &operator=(Connection&& other) noexcept;
48
56 static connection_ptr create(int32_t socket_fd, async::IOContext::io_context_ptr io_context);
57
63
70
80 void send_async(std::string_view msg, std::function<void()> callback);
81
91 void recv_async(std::vector<std::byte> buf, std::function<void()> callback);
92
101 void send_sync(std::string_view msg);
102
111 void recv_sync(std::string &buf);
112private:
114 Connection(int32_t socket_fd, async::IOContext::io_context_ptr io_context = nullptr) :
115 socket_fd_{ socket_fd },
116 io_context_{ io_context },
117 event_(nullptr, bufferevent_free) {}
119
120 int32_t socket_fd_;
121 std::shared_ptr<async::IOContext> io_context_;
122 event_ptr event_;
123};
124
130class Acceptor {
131public:
139 Acceptor(std::string port, int32_t domain);
140
147
149 Acceptor(const Acceptor&) = delete;
150 Acceptor& operator=(const Acceptor&) = delete;
152
158 Acceptor(Acceptor&& other) noexcept;
159
165 Acceptor& operator=(Acceptor&& other) noexcept;
166
172 void bind() const;
173
177 void listen() const;
178
187private:
188 int32_t listening_socket_fd_;
189 std::string port_;
190};
191}
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.
Definition tcp.hpp:10