simplified protocol and implemented text_message::receive()
This commit is contained in:
parent
15e6e869be
commit
5d124db96e
@ -9,7 +9,6 @@ void message::send(boost::asio::ip::tcp::socket& socket)
|
|||||||
system::error_code ec;
|
system::error_code ec;
|
||||||
|
|
||||||
asio::write(socket, asio::buffer(get_message_length()), 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);
|
asio::write(socket, asio::buffer(get_message()), ec);
|
||||||
|
|
||||||
if (ec)
|
if (ec)
|
||||||
|
@ -11,9 +11,14 @@ text_message::text_message(std::string text):_text(text)
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
std::string text_message::get_text()
|
||||||
|
{ return _text; }
|
||||||
|
|
||||||
|
|
||||||
std::string text_message::get_message_length()
|
std::string text_message::get_message_length()
|
||||||
{
|
{
|
||||||
return std::to_string(_text.size());
|
auto sizetext = std::to_string(_text.size());
|
||||||
|
return std::string(sizeof(size_t) - sizetext.size(), '0').append(sizetext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -24,7 +29,26 @@ std::string text_message::get_message()
|
|||||||
|
|
||||||
|
|
||||||
void text_message::receive(boost::asio::ip::tcp::socket& socket)
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
text_message::~text_message()
|
text_message::~text_message()
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -18,6 +19,8 @@ public:
|
|||||||
text_message();
|
text_message();
|
||||||
text_message(std::string);
|
text_message(std::string);
|
||||||
|
|
||||||
|
std::string get_text();
|
||||||
|
|
||||||
virtual void receive(boost::asio::ip::tcp::socket&) override;
|
virtual void receive(boost::asio::ip::tcp::socket&) override;
|
||||||
|
|
||||||
virtual ~text_message() override;
|
virtual ~text_message() override;
|
||||||
|
Loading…
Reference in New Issue
Block a user