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/connection.hpp | 116 ++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 source/lib/include/wanda/control/connection.hpp (limited to 'source/lib/include/wanda/control/connection.hpp') diff --git a/source/lib/include/wanda/control/connection.hpp b/source/lib/include/wanda/control/connection.hpp new file mode 100644 index 0000000..0284fd7 --- /dev/null +++ b/source/lib/include/wanda/control/connection.hpp @@ -0,0 +1,116 @@ +#ifndef WANDA_CONTROL_CONNECTION_HPP +#define WANDA_CONTROL_CONNECTION_HPP + +#include "wanda/meta/keyed.hpp" +#include "wanda/proto/message.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace wanda::control +{ + /** + * @brief A connection to a remote control endpoint + */ + struct connection : 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_close(pointer connection) {} + virtual void on_received(pointer connection, proto::message message) {} + virtual void on_error(pointer connection, std::error_code) {} + }; + + /** + * @brief A enum to describe different connection states + */ + enum struct state : std::underlying_type_t + { + unknown, //< Connection is in an unknown state + fresh, //< Connection is freshly created but not established + established, //< Connection has been established + }; + + /** + * @internal + * @brief Construct a new control connection object + * + * @note This constructor is keyed on a private key type so it can only be constructed using the + * #wanda::make_connection factory + */ + connection(key, protocol::socket socket); + + /** + * @brief Add the given listener to this control connection's listener set + * + * @returns true iff. the listener was not already in the listener set + */ + bool add(listener * listener); + + /** + * @brief Remove the given listener from this control connection's listener set + * + * @return true iff. the listener was previously registered with this control connection + */ + bool remove(listener * listener); + + /** + * @brief Start I/O processing for this control connection + */ + void start(); + + /** + * @brief Close this control connection + */ + void close(); + + /** + * @brief Send the given message to the remote endpoint + */ + void send(proto::message message); + + /** + * @brief Set the connection state to the provided state + */ + void update(state state); + + /** + * @brief Get the current connection state + */ + state current_state() const; + + private: + friend pointer make_connection(protocol::socket && socket); + + void perform_read(); + + protocol::socket m_socket; + boost::asio::streambuf m_in{}; + boost::asio::streambuf m_out{}; + std::istream m_input{&m_in}; + std::ostream m_output{&m_out}; + std::set m_listeners{}; + state m_state{}; + }; + + /** + * @brief Create a new control connection + */ + connection::pointer make_connection(connection::protocol::socket && socket); + +} // namespace wanda::control + +#endif \ No newline at end of file -- cgit v1.2.3