From 38faa173c587606c9cfb0e3fa8fbc77c80617b9e Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 13 Dec 2019 09:12:17 +0300 Subject: don't let the write buffer corrupt --- server/main/session.cpp | 9 ++++----- server/main/session.hpp | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/server/main/session.cpp b/server/main/session.cpp index 0ee7f75..7249f0e 100644 --- a/server/main/session.cpp +++ b/server/main/session.cpp @@ -87,15 +87,14 @@ std::string Session::consume_input(std::size_t bytes) { return input; } -void Session::write(std::string output) { +void Session::write(const std::string& output) { const auto self = shared_from_this(); + std::ostream os(&m_buffer); // Include CR (so that Windows' telnet client works) - output += "\r\n"; + os << output << "\r\n"; - boost::asio::const_buffer buffer{output.c_str(), output.length()}; - - boost::asio::async_write(m_socket, std::move(buffer), boost::asio::bind_executor(m_strand, + boost::asio::async_write(m_socket, m_buffer, boost::asio::bind_executor(m_strand, [this, self] (const boost::system::error_code& ec, std::size_t bytes) { handle_write(ec, bytes); })); diff --git a/server/main/session.hpp b/server/main/session.hpp index ace3755..98f28dd 100644 --- a/server/main/session.hpp +++ b/server/main/session.hpp @@ -25,7 +25,7 @@ private: void close(); void read(); - void write(std::string); + void write(const std::string&); void handle_read(const boost::system::error_code&, std::size_t); void handle_write(const boost::system::error_code&, std::size_t); -- cgit v1.2.3