From 9c2231c8fb45f32c7b1d23e14125bc58ea405e60 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 30 Nov 2018 15:53:53 +0100 Subject: core: implement basic message type --- src/control_connection.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src/control_connection.cpp') diff --git a/src/control_connection.cpp b/src/control_connection.cpp index 094a058..586b5de 100644 --- a/src/control_connection.cpp +++ b/src/control_connection.cpp @@ -1,5 +1,8 @@ #include "control_connection.hpp" +#include +#include + namespace wanda { @@ -34,10 +37,10 @@ void control_connection::start() } } -void control_connection::send(std::string message) +void control_connection::send(message message) { - m_output << message; - boost::asio::async_write(m_socket, m_out, boost::asio::transfer_exactly(message.size()), [that = shared_from_this(), this](auto const &error, auto const length) { + m_output << message << '\n'; + boost::asio::async_write(m_socket, m_out, boost::asio::transfer_exactly(message.size() + 1), [that = shared_from_this(), this](auto const &error, auto const length) { if (error) { // TODO: Handle error @@ -87,11 +90,19 @@ void control_connection::perform_read() } else { - std::string message{}; - std::getline(m_input, message); - for (auto &listener : m_listeners) + auto msg = message{}; + m_input >> msg; + m_input.ignore(std::numeric_limits::max()); + if (!m_input) + { + m_input.clear(); + } + else { - listener->on_received(shared_from_this(), message); + for (auto &listener : m_listeners) + { + listener->on_received(shared_from_this(), msg); + } } perform_read(); } -- cgit v1.2.3