Connect(): add non-async overload

This commit is contained in:
Alexander A. Klimov 2019-02-25 16:40:14 +01:00
parent dc0288fef8
commit d1e87bdc45
1 changed files with 29 additions and 0 deletions

View 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)
{