From 41f81518c2d9a953950c12605beae282c4326e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 20 Nov 2016 00:18:14 +0100 Subject: [PATCH] reworked message's receive interface --- message.cpp | 22 ++++++++++++++++++++++ message.h | 3 ++- text_message.cpp | 33 +++++---------------------------- text_message.h | 3 +-- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/message.cpp b/message.cpp index f1bb8d0..a2cf025 100644 --- a/message.cpp +++ b/message.cpp @@ -15,5 +15,27 @@ void message::send(boost::asio::ip::tcp::socket& socket) throw std::runtime_error("Networking error: " + ec.message()); } +void message::receive(boost::asio::ip::tcp::socket& socket) +{ + system::error_code ec; + asio::streambuf sb; + std::istream is(&sb); + std::string data; + + asio::read(socket, sb, asio::transfer_exactly(sizeof(size_t)), ec); + is >> data; + size_t len = std::stoull(data); + + asio::read(socket, sb, asio::transfer_exactly(len), ec); + data.clear(); + is.clear(); + is >> data; + + if (ec) + throw std::runtime_error("Networking error: " + ec.message()); + + process_data(data); +} + message::~message() {} \ No newline at end of file diff --git a/message.h b/message.h index 8fd69e7..0a6bef5 100644 --- a/message.h +++ b/message.h @@ -9,11 +9,12 @@ class message protected: virtual std::string get_message_length() = 0; virtual std::string get_message() = 0; + virtual void process_data(std::string&) = 0; public: void send(boost::asio::ip::tcp::socket&); - virtual void receive(boost::asio::ip::tcp::socket&) = 0; + void receive(boost::asio::ip::tcp::socket&); virtual ~message(); }; diff --git a/text_message.cpp b/text_message.cpp index 4489c79..0f0c8a8 100644 --- a/text_message.cpp +++ b/text_message.cpp @@ -7,8 +7,7 @@ using namespace boost; text_message::text_message() {} -text_message::text_message(std::string text):_text(text) -{} +text_message::text_message(std::string text):_text(text) {} std::string text_message::get_text() @@ -23,33 +22,11 @@ std::string text_message::get_message_length() std::string text_message::get_message() -{ - return _text; -} +{ return _text; } -void text_message::receive(boost::asio::ip::tcp::socket& socket) -{ - system::error_code ec; - asio::streambuf sb; - std::istream is(&sb); - std::string data; - - asio::read(socket, sb, asio::transfer_exactly(sizeof(size_t)), ec); - is >> data; - size_t len = std::stoull(data); - - asio::read(socket, sb, asio::transfer_exactly(len), ec); - data.clear(); - is.clear(); - is >> data; - - if (ec) - throw std::runtime_error("Networking error: " + ec.message()); - - _text = data; -} +void text_message::process_data(std::string& string) +{ _text = string; } -text_message::~text_message() -{} +text_message::~text_message() {} diff --git a/text_message.h b/text_message.h index 1bfc027..bf98fe5 100644 --- a/text_message.h +++ b/text_message.h @@ -14,6 +14,7 @@ private: protected: virtual std::string get_message_length() override; virtual std::string get_message() override; + virtual void process_data(std::string&) override; public: text_message(); @@ -21,7 +22,5 @@ public: std::string get_text(); - virtual void receive(boost::asio::ip::tcp::socket&) override; - virtual ~text_message() override; };