aboutsummaryrefslogtreecommitdiff
path: root/libcmix-network/server.cpp
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-08-31 11:57:15 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-08-31 11:57:15 +0200
commit2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd (patch)
tree1c582e4c5715a93c493582b1e01e51bc960d56c0 /libcmix-network/server.cpp
parent1525c5defe3db08c765477003be73c68bb2c3cb7 (diff)
downloadcmix-2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd.tar.gz
cmix-2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd.tar.bz2
cmix-2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd.zip
Split up the client and server parts in a separate network library
Diffstat (limited to 'libcmix-network/server.cpp')
-rw-r--r--libcmix-network/server.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/libcmix-network/server.cpp b/libcmix-network/server.cpp
new file mode 100644
index 0000000..cb2bd34
--- /dev/null
+++ b/libcmix-network/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