xbot/connection.hpp

110 lines
2.3 KiB
C++
Raw Normal View History

2023-11-22 19:59:34 -08:00
#pragma once
2023-11-27 14:12:20 -08:00
#include "event.hpp"
2023-11-22 19:59:34 -08:00
#include "settings.hpp"
2023-11-25 20:09:20 -08:00
#include <eventpp/eventdispatcher.h>
#include <eventpp/utilities/argumentadapter.h>
2023-11-22 19:59:34 -08:00
#include <boost/asio.hpp>
#include <chrono>
2023-11-25 20:09:20 -08:00
#include <functional>
2023-11-25 09:22:55 -08:00
#include <concepts>
2023-11-22 19:59:34 -08:00
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>
2023-11-25 20:09:20 -08:00
#include <typeinfo>
#include <typeindex>
2023-11-22 19:59:34 -08:00
2023-11-25 09:22:55 -08:00
struct ConnectEvent : Event
{
};
struct DisconnectEvent : Event
{
};
struct LineEvent : Event
{
explicit LineEvent(char * line) : line{line} {}
char * line;
};
2023-11-22 19:59:34 -08:00
class Connection : public std::enable_shared_from_this<Connection>
{
2023-11-25 20:09:20 -08:00
using EventDispatcher = eventpp::EventDispatcher<std::type_index, void(Event&)>;
public:
template <typename T>
class Handle
{
EventDispatcher::Handle handle;
Handle(EventDispatcher::Handle handle) : handle{handle} {}
public:
Handle() : handle{} {}
friend Connection;
};
private:
2023-11-22 19:59:34 -08:00
boost::asio::ip::tcp::socket stream_;
boost::asio::steady_timer write_timer_;
std::list<std::string> write_strings_;
2023-11-25 20:09:20 -08:00
EventDispatcher dispatcher_;
2023-11-22 19:59:34 -08:00
auto writer() -> void;
auto writer_() -> void;
public:
2023-11-25 09:22:55 -08:00
Connection(boost::asio::io_context & io);
2023-11-25 20:09:20 -08:00
template <typename T, typename F>
auto add_listener(F f) -> Handle<T>
{
return Handle<T>{dispatcher_.appendListener(
typeid(T),
eventpp::argumentAdapter<void(T&)>(f)
)};
}
template <typename T>
auto remove_listener(Handle<T> handle) -> void
{
dispatcher_.removeListener(typeid(T), handle.handle);
}
template <typename T>
auto dispatch(T& event) -> void
{
dispatcher_.dispatch(typeid(T), event);
}
auto get_executor() -> boost::asio::any_io_executor {
return stream_.get_executor();
}
2023-11-22 19:59:34 -08:00
2023-11-25 09:22:55 -08:00
template <typename T, typename... Args>
auto make_event(Args&& ... args) {
2023-11-25 20:09:20 -08:00
auto event = T{std::forward<Args>(args)...};
dispatch<T>(event);
2023-11-22 19:59:34 -08:00
}
2023-11-25 09:22:55 -08:00
/// Write bytes into the socket. Messages should be properly newline terminated.
2023-11-26 19:59:12 -08:00
auto write_line(std::string message) -> void;
2023-11-22 19:59:34 -08:00
auto connect(
boost::asio::io_context & io,
2023-11-25 09:22:55 -08:00
std::string host,
std::string port
2023-11-22 19:59:34 -08:00
) -> boost::asio::awaitable<void>;
2023-11-25 09:22:55 -08:00
auto close() -> void;
2023-11-22 19:59:34 -08:00
};