made interface of message and derived classes more reasonable

This commit is contained in:
Kjistóf 2016-11-19 17:40:00 +01:00
parent 6cb3cb0c79
commit 15e6e869be
4 changed files with 28 additions and 12 deletions

View File

@ -1,8 +1,20 @@
#include "message.h"
using namespace boost;
void message::send(boost::asio::ip::tcp::socket& socket)
{
system::error_code ec;
asio::write(socket, asio::buffer(get_message_length()), ec);
asio::write(socket, asio::buffer(":", sizeof(char)), ec);
asio::write(socket, asio::buffer(get_message()), ec);
if (ec)
throw std::runtime_error("Networking error: " + ec.message());
}
message::~message() {}

View File

@ -6,8 +6,13 @@
class message
{
protected:
virtual std::string get_message_length() = 0;
virtual std::string get_message() = 0;
public:
virtual void send(boost::asio::ip::tcp::socket&) = 0;
void send(boost::asio::ip::tcp::socket&);
virtual void receive(boost::asio::ip::tcp::socket&) = 0;
virtual ~message();

View File

@ -11,19 +11,15 @@ text_message::text_message(std::string text):_text(text)
{}
void text_message::send(boost::asio::ip::tcp::socket& socket)
std::string text_message::get_message_length()
{
boost::system::error_code ec;
return std::to_string(_text.size());
}
asio::write(socket, asio::buffer(std::to_string(_text.size())), ec);
asio::write(socket, asio::buffer(":", sizeof(char)), ec);
asio::write(socket, asio::buffer(_text), ec);
if (ec)
throw std::runtime_error("Networking error: " + ec.message());
std::string text_message::get_message()
{
return _text;
}

View File

@ -10,11 +10,14 @@ class text_message : public message
private:
std::string _text;
protected:
virtual std::string get_message_length() override;
virtual std::string get_message() override;
public:
text_message();
text_message(std::string);
virtual void send(boost::asio::ip::tcp::socket&) override;
virtual void receive(boost::asio::ip::tcp::socket&) override;
virtual ~text_message() override;