diff options
Diffstat (limited to 'network-handler/server.cpp')
| -rw-r--r-- | network-handler/server.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/network-handler/server.cpp b/network-handler/server.cpp new file mode 100644 index 0000000..cb2bd34 --- /dev/null +++ b/network-handler/server.cpp @@ -0,0 +1,31 @@ +#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 |
