2016-11-19 16:25:39 +00:00
|
|
|
#include "text_message.h"
|
|
|
|
|
|
|
|
using namespace boost;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text_message::text_message() {}
|
|
|
|
|
|
|
|
|
|
|
|
text_message::text_message(std::string text):_text(text)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2016-11-19 22:52:43 +00:00
|
|
|
std::string text_message::get_text()
|
|
|
|
{ return _text; }
|
|
|
|
|
|
|
|
|
2016-11-19 16:40:00 +00:00
|
|
|
std::string text_message::get_message_length()
|
2016-11-19 16:25:39 +00:00
|
|
|
{
|
2016-11-19 22:52:43 +00:00
|
|
|
auto sizetext = std::to_string(_text.size());
|
|
|
|
return std::string(sizeof(size_t) - sizetext.size(), '0').append(sizetext);
|
2016-11-19 16:40:00 +00:00
|
|
|
}
|
2016-11-19 16:25:39 +00:00
|
|
|
|
|
|
|
|
2016-11-19 16:40:00 +00:00
|
|
|
std::string text_message::get_message()
|
|
|
|
{
|
|
|
|
return _text;
|
2016-11-19 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void text_message::receive(boost::asio::ip::tcp::socket& socket)
|
2016-11-19 22:52:43 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2016-11-19 16:25:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
text_message::~text_message()
|
|
|
|
{}
|