diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-12 14:26:12 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-12 14:26:12 +0200 |
| commit | 7bca48bc5b5e37a3a8b0b23e57b88d069fa50589 (patch) | |
| tree | 47cd62512e631a064852015c65bb1965bc72414a /client/cmixclient.cpp | |
| parent | 0fb433690c0ca5f9561fe9e2e973e2cd61b873ba (diff) | |
| download | cmix-7bca48bc5b5e37a3a8b0b23e57b88d069fa50589.tar.gz cmix-7bca48bc5b5e37a3a8b0b23e57b88d069fa50589.tar.bz2 cmix-7bca48bc5b5e37a3a8b0b23e57b88d069fa50589.zip | |
Major network rewrite.
One generic class has been introduced to handle all connection types.
Typedefs provide Sender Receiver and SenderReceiver types, which limit
the functionality of the types. As to not accidentally communicate with
the wrong node about things.
Diffstat (limited to 'client/cmixclient.cpp')
| -rw-r--r-- | client/cmixclient.cpp | 38 |
1 files changed, 11 insertions, 27 deletions
diff --git a/client/cmixclient.cpp b/client/cmixclient.cpp index 195c5e0..1ffd904 100644 --- a/client/cmixclient.cpp +++ b/client/cmixclient.cpp @@ -2,15 +2,16 @@ #include "cmixclient.hpp" void CMixClient::key_exchange(int i) { - shared_keys.resize(network_connections.size()); + BOOST_LOG_TRIVIAL(trace) << "Sending KeyExchange for node: " << i; + shared_keys.resize(network_details.size()); cmix_proto::KeyExchange ke; ke.set_public_key(keypair.pub, keypair.pub_len); - network_connections[i].send(ke); + network_connections[i].async_send(ke); cmix_proto::Bye bye; - network_connections[i].send(bye); + network_connections[i].async_send(bye); } void CMixClient::initialize_connections() { @@ -20,25 +21,14 @@ void CMixClient::initialize_connections() { auto handler = [this, i]() mutable { cmix_proto::ImAClient imaclient; BOOST_LOG_TRIVIAL(trace) << "sending imaclient to node: " << i; - network_connections.at(i).send(imaclient); + network_connections.at(i).async_send(imaclient); key_exchange(i); }; network_connections.emplace_back(boost::asio::ip::tcp::socket(io_service)); network_connections.back().async_connect(network_details[i].host, network_details[i].port, handler); - - } -} - -cmix_proto::CMixMessage CMixClient::parse_cmix_message(std::vector<uint8_t> const& buffer) -{ - cmix_proto::CMixMessage message; - if(!message.ParseFromArray(buffer.data(), buffer.size())) { - BOOST_LOG_TRIVIAL(error) << "Received something which was not a CMixMessage"; - throw std::runtime_error("Network communication was disrupted in a unrecoverable way."); } - return message; } void CMixClient::handle_key_exchange(int node_id, cmix_proto::KeyExchange const& ke) @@ -46,19 +36,8 @@ void CMixClient::handle_key_exchange(int node_id, cmix_proto::KeyExchange const& shared_keys[node_id] = api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(ke.public_key().c_str()), false); } -void CMixClient::handle_message(int node_id, std::vector<uint8_t> const& message_buffer) +void CMixClient::handle_message(int node_id, cmix_proto::CMixMessage message) { - cmix_proto::CMixMessage message; - try { - message = parse_cmix_message(message_buffer); - } catch(std::runtime_error const& e) { - for(auto&& connection : network_connections) { - connection.close(); - } - io_service.stop(); - return; - } - switch(message.contents_case()) { case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: { handle_key_exchange(node_id, *message.mutable_keyexchange()); @@ -85,6 +64,11 @@ CMixClient::CMixClient(std::vector<NodeDetails> details) initialize_connections(); } +CMixClient::~CMixClient() +{ + api.free_key_pair(keypair); +} + void CMixClient::run() { io_service.run(); } |
