rearranged code in client_network_manager, so it is more readable
This commit is contained in:
parent
ee9e5c812b
commit
6f34d28458
@ -17,6 +17,7 @@ namespace chat
|
||||
|
||||
class client_network_manager
|
||||
{
|
||||
/* members */
|
||||
private:
|
||||
asio::io_service& _ios;
|
||||
tcp::socket _socket;
|
||||
@ -28,19 +29,42 @@ namespace chat
|
||||
std::string _login;
|
||||
|
||||
|
||||
/* interface */
|
||||
public:
|
||||
client_network_manager(asio::io_service& ioservice,
|
||||
tcp::resolver::iterator epit,
|
||||
std::string login)
|
||||
: _ios(ioservice), _socket(_ios), _isb(), _osb(), _is(&_isb), _os(&_osb), _login(login)
|
||||
{ connect(epit); }
|
||||
|
||||
void send(chat_message message)
|
||||
{
|
||||
_ios.post([this, message]
|
||||
{
|
||||
asio::async_write(_socket, asio::buffer(message.get()),
|
||||
[](boost::system::error_code, size_t)
|
||||
{});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/* internal methods */
|
||||
private:
|
||||
static void throw_if_error(boost::system::error_code& ec)
|
||||
{
|
||||
if (ec)
|
||||
throw std::runtime_error("Networking error: " + ec.message());
|
||||
}
|
||||
|
||||
std::string receive_message_sync()
|
||||
void connect(tcp::resolver::iterator epit)
|
||||
{
|
||||
std::string data;
|
||||
asio::read_until(_socket, _isb, byte(message::TERM));
|
||||
std::getline(_is, data, byte(message::TERM));
|
||||
|
||||
return data;
|
||||
asio::async_connect
|
||||
(
|
||||
_socket, epit,
|
||||
std::bind(&client_network_manager::handshake,
|
||||
this, std::placeholders::_1,
|
||||
std::placeholders::_2)
|
||||
);
|
||||
}
|
||||
|
||||
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
|
||||
@ -62,6 +86,15 @@ namespace chat
|
||||
receive();
|
||||
}
|
||||
|
||||
std::string receive_message_sync()
|
||||
{
|
||||
std::string data;
|
||||
asio::read_until(_socket, _isb, byte(message::TERM));
|
||||
std::getline(_is, data, byte(message::TERM));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void receive()
|
||||
{
|
||||
asio::async_read_until(_socket, _isb, byte(message::TERM),
|
||||
@ -86,33 +119,5 @@ namespace chat
|
||||
receive();
|
||||
});
|
||||
}
|
||||
|
||||
void connect(tcp::resolver::iterator epit)
|
||||
{
|
||||
asio::async_connect
|
||||
(
|
||||
_socket, epit,
|
||||
std::bind(&client_network_manager::handshake,
|
||||
this, std::placeholders::_1,
|
||||
std::placeholders::_2)
|
||||
);
|
||||
}
|
||||
|
||||
public:
|
||||
client_network_manager(asio::io_service& ioservice,
|
||||
tcp::resolver::iterator epit,
|
||||
std::string login)
|
||||
: _ios(ioservice), _socket(_ios), _isb(), _osb(), _is(&_isb), _os(&_osb), _login(login)
|
||||
{ connect(epit); }
|
||||
|
||||
void send(chat_message message)
|
||||
{
|
||||
_ios.post([this, message]
|
||||
{
|
||||
asio::async_write(_socket, asio::buffer(message.get()),
|
||||
[](boost::system::error_code, size_t)
|
||||
{});
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user