From 577fc0845718ed8ad5bebf02a277c0579a817f77 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 17 May 2024 17:58:38 +0200 Subject: wanda: restructure source layout --- source/lib/include/wanda/control/interface.hpp | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 source/lib/include/wanda/control/interface.hpp (limited to 'source/lib/include/wanda/control/interface.hpp') diff --git a/source/lib/include/wanda/control/interface.hpp b/source/lib/include/wanda/control/interface.hpp new file mode 100644 index 0000000..e895b68 --- /dev/null +++ b/source/lib/include/wanda/control/interface.hpp @@ -0,0 +1,97 @@ +/** + * @file interface.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_CONTROL_INTERFACE_HPP +#define WANDA_CONTROL_INTERFACE_HPP + +#include "wanda/control/connection.hpp" +#include "wanda/meta/keyed.hpp" +#include "wanda/proto/command.hpp" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace wanda::control +{ + /** + * @brief An RAII type to delete a socket file upon destruction + */ + struct socket_deleter + { + ~socket_deleter(); + + std::filesystem::path path; + }; + + /** + * @brief The daemon control interface + */ + struct interface : connection::listener, meta::keyed, std::enable_shared_from_this + { + using protocol = boost::asio::local::stream_protocol; + using pointer = std::shared_ptr; + + /** + * @brief The interface to be implemented by the control interface listener + */ + struct listener + { + virtual void on_received(interface & interface, proto::command command){}; + }; + + /** + * @internal + * @brief Construct a new control interface object + * + * @note This constructor is keyed on a private key type so it can only be constructed using the #wanda::make_interface factory + */ + interface(key, boost::asio::io_context & service, protocol::endpoint endpoint, listener & listener); + + /** + * @brief Start handling of controller connections + */ + std::error_code start(); + + /** + * @brief Stop the control interface + */ + std::error_code shutdown(); + + void on_close(connection::pointer connection) override; + void on_received(connection::pointer connection, proto::message message) override; + + private: + void perform_accept(); + + friend pointer make_interface(boost::asio::io_context & service, std::filesystem::path file, interface::listener & listener); + + boost::asio::io_context & m_service; + protocol::endpoint m_endpoint; + protocol::socket m_socket; + protocol::acceptor m_acceptor; + listener & m_listener; + socket_deleter m_deleter{m_endpoint.path()}; + std::set m_connections; + }; + + /** + * @brief A factory to create new #interface instances + */ + interface::pointer make_interface(boost::asio::io_context & service, std::filesystem::path socket, interface::listener & listener); + +} // namespace wanda::control + +#endif \ No newline at end of file -- cgit v1.2.3