xbot/connection.hpp

67 lines
1.4 KiB
C++

#pragma once
#include "linebuffer.hpp"
#include "settings.hpp"
#include "thread.hpp"
#include <boost/asio.hpp>
#include <chrono>
#include <concepts>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>
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>
{
boost::asio::ip::tcp::socket stream_;
boost::asio::steady_timer write_timer_;
std::list<std::string> write_strings_;
Dispatcher dispatcher_;
auto writer() -> void;
auto writer_() -> void;
public:
Connection(boost::asio::io_context & io);
auto add_thread(std::shared_ptr<Thread> thread) -> void;
auto add_event(std::shared_ptr<Event> event) -> void;
template <typename T, typename... Args>
auto make_event(Args&& ... args) {
add_event(std::make_shared<T>(std::forward<Args>(args)...));
}
/// Write bytes into the socket. Messages should be properly newline terminated.
auto write_raw(std::string message) -> void;
auto connect(
boost::asio::io_context & io,
std::string host,
std::string port
) -> boost::asio::awaitable<void>;
auto close() -> void;
};