#include <myapp/rcon/unixconnection.h>

namespace myapp::rcon {

unix_connection::unix_connection(std::unique_ptr<myapp::async::pipe> pipe)
  : pipe_(std::move(pipe))
  , data_handler_([](std::string msg) { })
  , closed_handler_([] { })
{
    pipe_->read([this](std::string msg) { data_handler_(std::move(msg)); },
                [this] { closed_handler_(); });
}

unix_connection::~unix_connection()
{
    pipe_->stop_read();
}

void unix_connection::write(std::string data)
{
    pipe_->write(std::move(data), [] { });
}

void unix_connection::on_data(std::function<void(std::string)> handler) noexcept
{
    data_handler_ = std::move(handler);
}

void unix_connection::on_peer_closed(std::function<void()> handler) noexcept
{
    closed_handler_ = std::move(handler);
}

} /* namespace myapp::rcon */
