2016-11-19 16:24:39 +00:00
|
|
|
#include "message.h"
|
|
|
|
|
2016-11-19 16:40:00 +00:00
|
|
|
using namespace boost;
|
2016-11-19 16:24:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-19 16:40:00 +00:00
|
|
|
void message::send(boost::asio::ip::tcp::socket& socket)
|
|
|
|
{
|
|
|
|
system::error_code ec;
|
|
|
|
|
2016-11-20 10:14:20 +00:00
|
|
|
asio::write(socket, asio::buffer(get_message_length() + get_message()), ec);
|
2016-11-19 16:40:00 +00:00
|
|
|
|
|
|
|
if (ec)
|
|
|
|
throw std::runtime_error("Networking error: " + ec.message());
|
|
|
|
}
|
2016-11-19 16:24:39 +00:00
|
|
|
|
2016-11-19 23:18:14 +00:00
|
|
|
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();
|
2016-11-20 10:14:20 +00:00
|
|
|
data.reserve(len);
|
2016-11-19 23:18:14 +00:00
|
|
|
is.clear();
|
2016-11-20 10:14:20 +00:00
|
|
|
data = std::string(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>());
|
2016-11-19 23:18:14 +00:00
|
|
|
|
|
|
|
if (ec)
|
|
|
|
throw std::runtime_error("Networking error: " + ec.message());
|
|
|
|
|
|
|
|
process_data(data);
|
|
|
|
}
|
|
|
|
|
2016-11-19 16:24:39 +00:00
|
|
|
|
|
|
|
message::~message() {}
|