mirror of
https://github.com/Icinga/icinga2.git
synced 2025-09-24 10:17:59 +02:00
Make Connect() not a (unnecessary) template
This commit is contained in:
parent
86a20d4916
commit
1c45d7af4a
@ -1,9 +1,12 @@
|
|||||||
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
|
#include "base/io-engine.hpp"
|
||||||
#include "base/tcpsocket.hpp"
|
#include "base/tcpsocket.hpp"
|
||||||
#include "base/logger.hpp"
|
#include "base/logger.hpp"
|
||||||
#include "base/utility.hpp"
|
#include "base/utility.hpp"
|
||||||
#include "base/exception.hpp"
|
#include "base/exception.hpp"
|
||||||
|
#include <boost/asio/error.hpp>
|
||||||
|
#include <boost/system/system_error.hpp>
|
||||||
#include <boost/exception/errinfo_api_function.hpp>
|
#include <boost/exception/errinfo_api_function.hpp>
|
||||||
#include <boost/exception/errinfo_errno.hpp>
|
#include <boost/exception/errinfo_errno.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -209,3 +212,38 @@ void TcpSocket::Connect(const String& node, const String& service)
|
|||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void icinga::Connect(AsioTcpSocket& socket, const String& node, const String& service, boost::asio::yield_context* yc)
|
||||||
|
{
|
||||||
|
using boost::asio::ip::tcp;
|
||||||
|
|
||||||
|
tcp::resolver resolver (IoEngine::Get().GetIoContext());
|
||||||
|
tcp::resolver::query query (node, service);
|
||||||
|
auto result (yc ? resolver.async_resolve(query, *yc) : resolver.resolve(query));
|
||||||
|
auto current (result.begin());
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
try {
|
||||||
|
socket.open(current->endpoint().protocol());
|
||||||
|
socket.set_option(tcp::socket::keep_alive(true));
|
||||||
|
|
||||||
|
if (yc) {
|
||||||
|
socket.async_connect(current->endpoint(), *yc);
|
||||||
|
} else {
|
||||||
|
socket.connect(current->endpoint());
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
} catch (const std::exception& ex) {
|
||||||
|
auto se (dynamic_cast<const boost::system::system_error*>(&ex));
|
||||||
|
|
||||||
|
if (se && se->code() == boost::asio::error::operation_aborted || ++current == result.end()) {
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (socket.is_open()) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,12 +4,9 @@
|
|||||||
#define TCPSOCKET_H
|
#define TCPSOCKET_H
|
||||||
|
|
||||||
#include "base/i2-base.hpp"
|
#include "base/i2-base.hpp"
|
||||||
#include "base/io-engine.hpp"
|
|
||||||
#include "base/socket.hpp"
|
#include "base/socket.hpp"
|
||||||
#include <boost/asio/error.hpp>
|
|
||||||
#include <boost/asio/ip/tcp.hpp>
|
#include <boost/asio/ip/tcp.hpp>
|
||||||
#include <boost/asio/spawn.hpp>
|
#include <boost/asio/spawn.hpp>
|
||||||
#include <boost/system/system_error.hpp>
|
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
@ -30,55 +27,21 @@ public:
|
|||||||
void Connect(const String& node, const String& service);
|
void Connect(const String& node, const String& service);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef boost::asio::ip::tcp::socket::lowest_layer_type AsioTcpSocket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TCP Connect based on Boost ASIO.
|
* TCP Connect based on Boost ASIO.
|
||||||
*
|
*
|
||||||
* @ingroup base
|
* @ingroup base
|
||||||
*/
|
*/
|
||||||
template<class Socket>
|
void Connect(AsioTcpSocket& socket, const String& node, const String& service, boost::asio::yield_context* yc);
|
||||||
void Connect(Socket& socket, const String& node, const String& service, boost::asio::yield_context* yc)
|
|
||||||
{
|
|
||||||
using boost::asio::ip::tcp;
|
|
||||||
|
|
||||||
tcp::resolver resolver (IoEngine::Get().GetIoContext());
|
inline void Connect(AsioTcpSocket& socket, const String& node, const String& service)
|
||||||
tcp::resolver::query query (node, service);
|
|
||||||
auto result (yc ? resolver.async_resolve(query, *yc) : resolver.resolve(query));
|
|
||||||
auto current (result.begin());
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
try {
|
|
||||||
socket.open(current->endpoint().protocol());
|
|
||||||
socket.set_option(tcp::socket::keep_alive(true));
|
|
||||||
|
|
||||||
if (yc) {
|
|
||||||
socket.async_connect(current->endpoint(), *yc);
|
|
||||||
} else {
|
|
||||||
socket.connect(current->endpoint());
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
} catch (const std::exception& ex) {
|
|
||||||
auto se (dynamic_cast<const boost::system::system_error*>(&ex));
|
|
||||||
|
|
||||||
if (se && se->code() == boost::asio::error::operation_aborted || ++current == result.end()) {
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (socket.is_open()) {
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Socket>
|
|
||||||
inline void Connect(Socket& socket, const String& node, const String& service)
|
|
||||||
{
|
{
|
||||||
Connect(socket, node, service, nullptr);
|
Connect(socket, node, service, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Socket>
|
inline void Connect(AsioTcpSocket& socket, const String& node, const String& service, boost::asio::yield_context yc)
|
||||||
inline void Connect(Socket& socket, const String& node, const String& service, boost::asio::yield_context yc)
|
|
||||||
{
|
{
|
||||||
Connect(socket, node, service, &yc);
|
Connect(socket, node, service, &yc);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user