xbot/connection.hpp

111 lines
2.4 KiB
C++

#pragma once
#include "linebuffer.hpp"
#include "settings.hpp"
#include "thread.hpp"
#include <eventpp/eventdispatcher.h>
#include <eventpp/utilities/argumentadapter.h>
#include <boost/asio.hpp>
#include <chrono>
#include <functional>
#include <concepts>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>
#include <typeinfo>
#include <typeindex>
struct ConnectEvent : Event
{
};
struct DisconnectEvent : Event
{
};
struct LineEvent : Event
{
explicit LineEvent(char * line) : line{line} {}
char * line;
};
class Connection : public std::enable_shared_from_this<Connection>
{
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:
boost::asio::ip::tcp::socket stream_;
boost::asio::steady_timer write_timer_;
std::list<std::string> write_strings_;
EventDispatcher dispatcher_;
auto writer() -> void;
auto writer_() -> void;
public:
Connection(boost::asio::io_context & io);
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();
}
template <typename T, typename... Args>
auto make_event(Args&& ... args) {
auto event = T{std::forward<Args>(args)...};
dispatch<T>(event);
}
/// Write bytes into the socket. Messages should be properly newline terminated.
auto write_line(std::string message) -> void;
auto connect(
boost::asio::io_context & io,
std::string host,
std::string port
) -> boost::asio::awaitable<void>;
auto close() -> void;
};