added comments to chat_networking.hpp to promote better understanding
This commit is contained in:
parent
c256a9a928
commit
406c7b2841
@ -16,6 +16,9 @@ namespace chat
|
|||||||
using boost::asio::ip::tcp;
|
using boost::asio::ip::tcp;
|
||||||
|
|
||||||
|
|
||||||
|
/* receive_policy and send_policy must provide the static methods
|
||||||
|
* that decide what happens with the incoming / outgoing messages.
|
||||||
|
* you can read about the exact requirements in check_policy.hpp */
|
||||||
template <class receive_policy, class send_policy>
|
template <class receive_policy, class send_policy>
|
||||||
class client_network_manager
|
class client_network_manager
|
||||||
{
|
{
|
||||||
@ -30,8 +33,8 @@ namespace chat
|
|||||||
private:
|
private:
|
||||||
asio::io_service& _ios;
|
asio::io_service& _ios;
|
||||||
tcp::socket _socket;
|
tcp::socket _socket;
|
||||||
asio::streambuf _isb;//<-------.
|
asio::streambuf _isb;//<-------. input stream
|
||||||
asio::streambuf _osb;//<---. |
|
asio::streambuf _osb;//<---. | output stream
|
||||||
std::istream _is;//--------|---'
|
std::istream _is;//--------|---'
|
||||||
std::ostream _os;//--------'
|
std::ostream _os;//--------'
|
||||||
|
|
||||||
@ -74,12 +77,14 @@ namespace chat
|
|||||||
|
|
||||||
/* internal methods */
|
/* internal methods */
|
||||||
private:
|
private:
|
||||||
static void throw_if_error(boost::system::error_code& ec)
|
/* throws if something went wrong on the network */
|
||||||
|
static void throw_if_error(const boost::system::error_code& ec)
|
||||||
{
|
{
|
||||||
if (ec)
|
if (ec)
|
||||||
throw std::runtime_error("Networking error: " + ec.message());
|
throw std::runtime_error("Networking error: " + ec.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* asynchronously connect to the host epit points to */
|
||||||
void connect(tcp::resolver::iterator epit)
|
void connect(tcp::resolver::iterator epit)
|
||||||
{
|
{
|
||||||
asio::async_connect
|
asio::async_connect
|
||||||
@ -91,13 +96,14 @@ namespace chat
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* handles the handshake as defined in the common protocol */
|
||||||
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
|
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
|
||||||
{
|
{
|
||||||
throw_if_error(ec);
|
throw_if_error(ec);
|
||||||
|
|
||||||
_os << chat_message(message::HELLO);
|
_os << chat_message(message::HELLO);
|
||||||
_os << chat_message(message::NEPTUN, _login);
|
_os << chat_message(message::NEPTUN, _login);
|
||||||
std::reverse(_login.begin(), _login.end());
|
std::reverse(_login.begin(), _login.end()); // passw is neptun reversed
|
||||||
_os << chat_message(message::PASSW, _login);
|
_os << chat_message(message::PASSW, _login);
|
||||||
asio::write(_socket, _osb); // handshake is handled synchronously
|
asio::write(_socket, _osb); // handshake is handled synchronously
|
||||||
|
|
||||||
@ -110,6 +116,7 @@ namespace chat
|
|||||||
receive(); // then flow goes to receive-loop
|
receive(); // then flow goes to receive-loop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* helper method used by handshake() */
|
||||||
std::string receive_message_sync()
|
std::string receive_message_sync()
|
||||||
{
|
{
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
@ -122,6 +129,7 @@ namespace chat
|
|||||||
return data.substr(1);
|
return data.substr(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* asynchronously receive a message, and decide what to do with it */
|
||||||
void receive()
|
void receive()
|
||||||
{
|
{
|
||||||
asio::async_read_until
|
asio::async_read_until
|
||||||
@ -133,10 +141,10 @@ namespace chat
|
|||||||
std::string data;
|
std::string data;
|
||||||
std::getline(_is, data, byte(message::TERM));
|
std::getline(_is, data, byte(message::TERM));
|
||||||
|
|
||||||
char header = data[0];
|
char header = data[0]; // get command byte
|
||||||
auto content = data.substr(1, data.size()-1);
|
auto content = data.substr(1, data.size()-1); // minus command & term bytes
|
||||||
|
|
||||||
switch (header)
|
switch (header) // decide what to do
|
||||||
{
|
{
|
||||||
case byte(message::PING):
|
case byte(message::PING):
|
||||||
send(chat_message(message::PONG));
|
send(chat_message(message::PONG));
|
||||||
@ -159,7 +167,7 @@ namespace chat
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
receive();
|
receive(); // receive the next message
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user