xbot/connection.hpp

67 lines
1.4 KiB
C++
Raw Normal View History

2023-11-22 19:59:34 -08:00
#pragma once
#include "linebuffer.hpp"
#include "settings.hpp"
2023-11-25 09:22:55 -08:00
#include "thread.hpp"
2023-11-22 19:59:34 -08:00
#include <boost/asio.hpp>
#include <chrono>
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 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>
{
boost::asio::ip::tcp::socket stream_;
boost::asio::steady_timer write_timer_;
std::list<std::string> write_strings_;
2023-11-25 09:22:55 -08:00
Dispatcher 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);
auto add_thread(std::shared_ptr<Thread> thread) -> void;
auto add_event(std::shared_ptr<Event> event) -> void;
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) {
add_event(std::make_shared<T>(std::forward<Args>(args)...));
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.
auto write_raw(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
};