diff options
Diffstat (limited to 'network-handler')
| -rw-r--r-- | network-handler/CMakeLists.txt | 13 | ||||
| -rw-r--r-- | network-handler/acceptor.cpp | 59 | ||||
| -rw-r--r-- | network-handler/acceptor.hpp | 18 | ||||
| -rw-r--r-- | network-handler/client.cpp | 41 | ||||
| -rw-r--r-- | network-handler/client.hpp | 24 | ||||
| -rw-r--r-- | network-handler/node.cpp | 21 | ||||
| -rw-r--r-- | network-handler/node.hpp | 21 | ||||
| -rw-r--r-- | network-handler/nodeclient.cpp | 22 | ||||
| -rw-r--r-- | network-handler/nodeclient.hpp | 20 | ||||
| -rw-r--r-- | network-handler/server.cpp | 31 | ||||
| -rw-r--r-- | network-handler/server.hpp | 29 |
11 files changed, 1 insertions, 298 deletions
diff --git a/network-handler/CMakeLists.txt b/network-handler/CMakeLists.txt index b4aef97..072172e 100644 --- a/network-handler/CMakeLists.txt +++ b/network-handler/CMakeLists.txt @@ -3,13 +3,8 @@ find_package(Boost COMPONENTS system program_options REQUIRED) add_executable(network-handler main.cpp - acceptor.hpp acceptor.cpp - server.hpp server.cpp networkhandler.hpp networkhandler.cpp - node.hpp node.cpp - client.hpp client.cpp userclient.hpp userclient.cpp - nodeclient.hpp nodeclient.cpp nodemanager.hpp nodemanager.cpp ) @@ -22,11 +17,5 @@ target_link_libraries(network-handler PRIVATE Boost::program_options PRIVATE Boost::system PRIVATE cmix + PRIVATE cmix-network ) - -if(WIN32) - target_link_libraries(network-handler - PRIVATE wsock32 - PRIVATE ws2_32 - ) -endif() diff --git a/network-handler/acceptor.cpp b/network-handler/acceptor.cpp deleted file mode 100644 index 34b5f0e..0000000 --- a/network-handler/acceptor.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "acceptor.hpp" - -#include <boost/asio/ip/tcp.hpp> -#include <boost/asio/ip/v6_only.hpp> -#include <boost/asio/placeholders.hpp> -#include <boost/bind.hpp> - - -using namespace boost::asio; -using namespace boost::asio::ip; - -void accept_loop(tcp::acceptor& acceptor, std::function<void(tcp::socket&&)> f); - -void accept_connection(tcp::acceptor& acceptor, std::shared_ptr<tcp::socket> socket, boost::system::error_code ec, std::function<void(boost::asio::ip::tcp::socket&&)> f) -{ - if(!bool(ec)) - { - f(std::move(*socket)); - accept_loop(acceptor, f); - } else { - std::stringstream ss; - ss << ec; - throw std::runtime_error(ss.str()); - } - -} - -void accept_loop(tcp::acceptor& acceptor, std::function<void(tcp::socket&&)> f) -{ - std::shared_ptr<tcp::socket> new_socket = std::make_shared<tcp::socket>(acceptor.get_io_service()); - - acceptor.async_accept(*new_socket, boost::bind(accept_connection, boost::ref(acceptor), new_socket, boost::asio::placeholders::error, f)); -} - -Acceptor::Acceptor(boost::asio::io_service &io_service, boost::asio::ip::address address, uint16_t port) -: acceptor(io_service) -, endpoint(address, port) -{} - -void Acceptor::bind_v6_and_v4_any(std::function<void(tcp::socket&&)> accept_handler) { - acceptor.open(endpoint.protocol()); - - v6_only option(false); - acceptor.set_option(option); - - acceptor.bind(endpoint); - acceptor.listen(); - - accept_loop(acceptor, accept_handler); -} - -void Acceptor::setup_listen_socket(std::function<void (tcp::socket &&)> accept_handler) -{ - acceptor.open(endpoint.protocol()); - acceptor.bind(endpoint); - acceptor.listen(); - - accept_loop(acceptor, accept_handler); -} diff --git a/network-handler/acceptor.hpp b/network-handler/acceptor.hpp deleted file mode 100644 index 7d8e0a2..0000000 --- a/network-handler/acceptor.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include <boost/asio/ip/tcp.hpp> - -#include <functional> - -class Acceptor{ - boost::asio::ip::tcp::acceptor acceptor; - boost::asio::ip::tcp::endpoint endpoint; - -public: - Acceptor(boost::asio::io_service& io_service, boost::asio::ip::address address, uint16_t port); - - boost::asio::ip::address get_address() { return endpoint.address(); } - - void bind_v6_and_v4_any(std::function<void(boost::asio::ip::tcp::socket&&)> accept_handler); - void setup_listen_socket(std::function<void(boost::asio::ip::tcp::socket&&)> accept_handler); -}; diff --git a/network-handler/client.cpp b/network-handler/client.cpp deleted file mode 100644 index 3098185..0000000 --- a/network-handler/client.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "client.hpp" - -#include <boost/asio/placeholders.hpp> -#include <boost/bind.hpp> - -#include <iostream> - -using namespace boost::asio::ip; -using namespace boost::system; - -Client::Client(tcp::socket &&socket) -: socket(std::move(socket)) -, buffer() -{} - -void Client::handle_receive(MessageHandler message_handler, const error_code &ec, size_t read_bytes) -{ - buffer.commit(read_bytes); - std::istream is(&buffer); - - if(!ec) { - std::vector<uint8_t> data(std::istream_iterator<uint8_t>(is), {}); - message_handler(data); - receive(message_handler); - } else { - done(); - } -} - -void Client::receive(MessageHandler message_handler) { - using namespace boost::asio::placeholders; - - socket.async_receive( - buffer.prepare(512), - boost::bind(&Client::handle_receive, this, message_handler, error(), bytes_transferred()) - ); -} - -void Client::on_done(Client::OnDoneFT f) { - done = f; -} diff --git a/network-handler/client.hpp b/network-handler/client.hpp deleted file mode 100644 index 2b6a6df..0000000 --- a/network-handler/client.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include <boost/asio/ip/tcp.hpp> -#include <boost/asio/streambuf.hpp> - -#include <array> - -class Client { -public: - typedef std::function<void(void)> OnDoneFT; - typedef std::function<void(std::vector<uint8_t>)> MessageHandler; - -private: - boost::asio::ip::tcp::socket socket; - boost::asio::streambuf buffer; - OnDoneFT done; - -public: - Client(boost::asio::ip::tcp::socket&& socket); - - void handle_receive(MessageHandler message_handler, boost::system::error_code const& ec, size_t read_bytes); - void receive(MessageHandler message_handler); - void on_done(OnDoneFT f); -}; diff --git a/network-handler/node.cpp b/network-handler/node.cpp deleted file mode 100644 index 497a920..0000000 --- a/network-handler/node.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "node.hpp" - -Node::Node(ListenSettings const& listen_settings) -: io_service() -, server(io_service, listen_settings, [this](boost::asio::ip::tcp::socket&& socket){accept_handler(std::move(socket));}) -, clients() -{} - -void Node::accept_handler(boost::asio::ip::tcp::socket&& socket) -{ - clients.emplace_back(std::move(socket)); - - auto it = --clients.end(); - clients.back().on_done( - [this, it]() { - clients.erase(it); - } - ); - - clients.back().receive(); -} diff --git a/network-handler/node.hpp b/network-handler/node.hpp deleted file mode 100644 index 7d30188..0000000 --- a/network-handler/node.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include "server.hpp" -#include "nodeclient.hpp" - -#include <boost/asio/io_service.hpp> - -#include <list> - -class Node -{ - boost::asio::io_service io_service; - Server server; - std::list<NodeClient> clients; - - void accept_handler(boost::asio::ip::tcp::socket&& socket); - -public: - Node(ListenSettings const& listen_settings); -}; - diff --git a/network-handler/nodeclient.cpp b/network-handler/nodeclient.cpp deleted file mode 100644 index 9b026ba..0000000 --- a/network-handler/nodeclient.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "nodeclient.hpp" - -#include <iostream> - -NodeClient::NodeClient(boost::asio::ip::tcp::socket &&socket) -: client(std::move(socket)) -{} - -void NodeClient::handle_message(std::vector<uint8_t> message) -{ - std::cout << std::string(message.begin(), message.end()) << std::endl; -} - -void NodeClient::receive() -{ - client.receive(std::bind(&NodeClient::handle_message, this, std::placeholders::_1)); -} - -void NodeClient::on_done(Client::OnDoneFT done) { - client.on_done(done); -} - diff --git a/network-handler/nodeclient.hpp b/network-handler/nodeclient.hpp deleted file mode 100644 index 9a3799a..0000000 --- a/network-handler/nodeclient.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "client.hpp" - -#include <boost/asio/ip/tcp.hpp> - -#include <vector> - -class NodeClient -{ - Client client; - void handle_message(std::vector<uint8_t> message); - -public: - NodeClient(boost::asio::ip::tcp::socket&& socket); - virtual ~NodeClient() = default; - - void receive(); - void on_done(Client::OnDoneFT done); -}; diff --git a/network-handler/server.cpp b/network-handler/server.cpp deleted file mode 100644 index cb2bd34..0000000 --- a/network-handler/server.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "server.hpp" - -using namespace boost::asio::ip; -using namespace boost::asio; - -Server::Server(io_service& io_service, const ListenSettings& listen_settings, AcceptHandler accept_handler) -: listen_settings(listen_settings) -, v4_acceptor(io_service, address_v4::from_string(listen_settings.ipv4_inaddr), listen_settings.port) -, v6_acceptor(io_service, address_v6::from_string(listen_settings.ipv6_inaddr), listen_settings.port) -{ - /* - * We can't bind both a v4 and v6 socket to both inaddr4_any and inaddr6_any. - * So in case both ipv4 and ipv6 are enabled and both want to bind to their - * respective inaddr_any we need to bind to v6 any and disable ipv6 only on - * the acceptor socket, else we can just bind to the interfaces normally. - */ - bool bind_v4_any = v4_acceptor.get_address().to_v4() == address_v4::any() && listen_settings.enable_ipv4; - bool bind_v6_any = v6_acceptor.get_address().to_v6() == address_v6::any() && listen_settings.enable_ipv6; - if(bind_v4_any && bind_v6_any) { - v6_acceptor.bind_v6_and_v4_any(accept_handler); - } else if(bind_v4_any || bind_v6_any) { - throw std::runtime_error("Cannot bind an INADDR_ANY and a non INADDR_ANY address on ipv4 and ipv6"); - } else { - if(listen_settings.enable_ipv4) { - v4_acceptor.setup_listen_socket(accept_handler); - } - if(listen_settings.enable_ipv6) { - v6_acceptor.setup_listen_socket(accept_handler); - } - } -}
\ No newline at end of file diff --git a/network-handler/server.hpp b/network-handler/server.hpp deleted file mode 100644 index 942cb14..0000000 --- a/network-handler/server.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include <boost/asio/io_service.hpp> - -#include "acceptor.hpp" - -struct ListenSettings { - bool enable_ipv4; - std::string ipv4_inaddr; - bool enable_ipv6; - std::string ipv6_inaddr; - uint16_t port; -}; - -class Server -{ -public: - typedef std::function<void(boost::asio::ip::tcp::socket&& socket)> AcceptHandler; - -private: - ListenSettings const& listen_settings; - - Acceptor v4_acceptor; - Acceptor v6_acceptor; - -public: - Server(boost::asio::io_service& io_service, ListenSettings const& listen_settings, AcceptHandler accept_handler); - -}; |
