]> granicus.if.org Git - icinga2/commitdiff
Connect(): add non-async overload
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 25 Feb 2019 15:40:14 +0000 (16:40 +0100)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 1 Apr 2019 15:11:09 +0000 (17:11 +0200)
lib/base/tcpsocket.hpp

index 069288aad2027c78e1f7d1db401c7bb6b559aa50..0f8334f0130979a71c38ca929d2583a1279b9ea9 100644 (file)
@@ -27,6 +27,35 @@ public:
        void Connect(const String& node, const String& service);
 };
 
+template<class Socket>
+void Connect(Socket& socket, const String& node, const String& service)
+{
+       using boost::asio::ip::tcp;
+
+       tcp::resolver resolver (socket.get_io_service());
+       tcp::resolver::query query (node, service);
+       auto result (resolver.resolve(query));
+       auto current (result.begin());
+
+       for (;;) {
+               try {
+                       socket.open(current->endpoint().protocol());
+                       socket.set_option(tcp::socket::keep_alive(true));
+                       socket.connect(current->endpoint());
+
+                       break;
+               } catch (const std::exception&) {
+                       if (++current == result.end()) {
+                               throw;
+                       }
+
+                       if (socket.is_open()) {
+                               socket.close();
+                       }
+               }
+       }
+}
+
 template<class Socket>
 void Connect(Socket& socket, const String& node, const String& service, boost::asio::yield_context yc)
 {