Line data Source code
1 : #ifndef THINGER_ASIO_SOCKET_SERVER_BASE_HPP
2 : #define THINGER_ASIO_SOCKET_SERVER_BASE_HPP
3 :
4 : #include "sockets/socket.hpp"
5 : #include "../util/logger.hpp"
6 :
7 : #include <memory>
8 : #include <set>
9 : #include <functional>
10 : #include <boost/noncopyable.hpp>
11 : #include <boost/asio.hpp>
12 :
13 : namespace thinger::asio {
14 :
15 : // Type for providing io_context
16 : using io_context_provider = std::function<boost::asio::io_context&()>;
17 :
18 : class socket_server_base : private boost::noncopyable {
19 : public:
20 : static constexpr int MAX_LISTENING_ATTEMPTS = -1;
21 :
22 : socket_server_base(io_context_provider acceptor_context_provider,
23 : io_context_provider connection_context_provider,
24 : std::set<std::string> allowed_remotes = {},
25 : std::set<std::string> forbidden_remotes = {});
26 :
27 658 : virtual ~socket_server_base() = default;
28 :
29 : // Configuration
30 : void set_max_listening_attempts(int attempts);
31 : void set_handler(std::function<void(std::shared_ptr<socket>)> handler);
32 : void set_allowed_remotes(std::set<std::string> allowed);
33 : void set_forbidden_remotes(std::set<std::string> forbidden);
34 :
35 : // Control
36 : bool start();
37 : virtual bool stop();
38 26 : bool is_running() const { return running_; }
39 : virtual std::string get_service_name() const = 0;
40 : virtual uint16_t local_port() const = 0;
41 :
42 : protected:
43 : // Pure virtual methods that derived classes must implement
44 : virtual bool create_acceptor() = 0;
45 : virtual void accept_connection() = 0;
46 :
47 : // Helper method for IP filtering
48 : bool is_remote_allowed(const std::string& remote_ip) const;
49 :
50 : protected:
51 : std::function<void(std::shared_ptr<socket>)> handler_;
52 : std::set<std::string> allowed_remotes_;
53 : std::set<std::string> forbidden_remotes_;
54 : int max_listening_attempts_;
55 : bool running_ = false;
56 :
57 : // io_context providers
58 : io_context_provider acceptor_context_provider_;
59 : io_context_provider connection_context_provider_;
60 : };
61 :
62 : } // namespace thinger::asio
63 :
64 : #endif // THINGER_ASIO_SOCKET_SERVER_BASE_HPP
|