diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-21 14:01:26 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-21 18:15:46 +0200 |
| commit | 510ce3bec7915a790fbf75ace5521e437d9d416a (patch) | |
| tree | 7b9286875652b677a110287d11d024f85879cc7a /libcmix-network/client.cpp | |
| parent | 640e0ad7a762d0473581c2114c2c945961bea80f (diff) | |
| download | cmix-510ce3bec7915a790fbf75ace5521e437d9d416a.tar.gz cmix-510ce3bec7915a790fbf75ace5521e437d9d416a.tar.bz2 cmix-510ce3bec7915a790fbf75ace5521e437d9d416a.zip | |
Adds SSL connections between nodes.
Diffstat (limited to 'libcmix-network/client.cpp')
| -rw-r--r-- | libcmix-network/client.cpp | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/libcmix-network/client.cpp b/libcmix-network/client.cpp deleted file mode 100644 index 09ea98c..0000000 --- a/libcmix-network/client.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "client.hpp" - -#include "connect.hpp" - -#include <iostream> - -using namespace boost::asio::ip; -using namespace boost::system; - -Client::Client(tcp::socket &&socket) -: socket(std::move(socket)) -, buffer(new boost::asio::streambuf()) -, done() -{} - -Client::~Client() -{ - close(); -} - -void Client::async_connect(std::string next_host, std::string next_port, std::function<void ()> on_connect) -{ - ::async_connect(socket, next_host, next_port, on_connect); -} - -std::array<uint8_t, 4> Client::prepare_length_prefix(uint32_t length) -{ - length = htonl(length); - std::array<uint8_t, 4> buf; - - std::copy(reinterpret_cast<uint8_t*>(&length), reinterpret_cast<uint8_t*>(&length) + 4, buf.data()); - return buf; -} - -std::vector<uint8_t> Client::received_bytes_to_vector(size_t read_bytes) -{ - buffer->commit(read_bytes); - std::istream is(buffer.get()); - is.unsetf(std::ios::skipws); - - return std::vector<uint8_t>(std::istream_iterator<uint8_t>(is), {}); -} - -void Client::handle_receive_message(MessageHandler message_handler, const error_code &ec, size_t read_bytes) -{ - if(!ec) { - std::vector<uint8_t> data = received_bytes_to_vector(read_bytes); - message_handler(data); - } else { - BOOST_LOG_TRIVIAL(error) << ec; - if(done) { - done(); - } - } -} - -void Client::handle_receive_size(Client::MessageHandler message_handler, error_code const& ec, size_t read_bytes) -{ - using namespace boost::asio::placeholders; - - if(!ec) { - std::vector<uint8_t> data = received_bytes_to_vector(read_bytes); - uint32_t size; - std::copy(data.begin(), data.end(), reinterpret_cast<uint8_t*>(&size)); - size = ntohl(size); - - socket.async_receive( - buffer->prepare(size), - boost::bind(&Client::handle_receive_message, this, message_handler, error(), bytes_transferred()) - ); - } else { - BOOST_LOG_TRIVIAL(error) << ec; - if(done) { - done(); - } - } -} - -void Client::close() -{ - socket.close(); -} - -void Client::on_done(Client::OnDoneFT f) { - done = f; -} - -bool Client::is_open() const -{ - return socket.is_open(); -} |
