2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 12:06:41 +02:00
|
|
|
|
2012-04-02 08:56:30 +02:00
|
|
|
#ifndef TCPSOCKET_H
|
|
|
|
#define TCPSOCKET_H
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
2019-06-07 16:30:34 +02:00
|
|
|
#include "base/io-engine.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/socket.hpp"
|
2019-02-25 16:18:48 +01:00
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
|
|
#include <boost/asio/spawn.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2012-05-15 10:58:14 +02:00
|
|
|
/**
|
2019-05-29 14:17:36 +02:00
|
|
|
* A TCP socket. DEPRECATED - Use Boost ASIO instead.
|
2012-05-18 22:21:28 +02:00
|
|
|
*
|
|
|
|
* @ingroup base
|
2012-05-15 10:58:14 +02:00
|
|
|
*/
|
2018-01-04 06:11:04 +01:00
|
|
|
class TcpSocket final : public Socket
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-11-07 12:32:25 +01:00
|
|
|
DECLARE_PTR_TYPEDEFS(TcpSocket);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
void Bind(const String& service, int family);
|
|
|
|
void Bind(const String& node, const String& service, int family);
|
2017-05-23 12:04:08 +02:00
|
|
|
|
2017-05-23 12:05:01 +02:00
|
|
|
void Connect(const String& node, const String& service);
|
2012-03-28 13:24:49 +02:00
|
|
|
};
|
|
|
|
|
2019-05-29 14:17:36 +02:00
|
|
|
/**
|
|
|
|
* TCP Connect based on Boost ASIO.
|
|
|
|
*
|
|
|
|
* @ingroup base
|
|
|
|
*/
|
2019-02-25 16:40:14 +01:00
|
|
|
template<class Socket>
|
|
|
|
void Connect(Socket& socket, const String& node, const String& service)
|
|
|
|
{
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
2019-09-09 15:11:38 +02:00
|
|
|
tcp::resolver resolver (IoEngine::Get().GetIoContext());
|
2019-02-25 16:40:14 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 16:18:48 +01:00
|
|
|
template<class Socket>
|
|
|
|
void Connect(Socket& socket, const String& node, const String& service, boost::asio::yield_context yc)
|
|
|
|
{
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
2019-09-09 15:11:38 +02:00
|
|
|
tcp::resolver resolver (IoEngine::Get().GetIoContext());
|
2019-02-25 16:18:48 +01:00
|
|
|
tcp::resolver::query query (node, service);
|
|
|
|
auto result (resolver.async_resolve(query, yc));
|
|
|
|
auto current (result.begin());
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
try {
|
|
|
|
socket.open(current->endpoint().protocol());
|
|
|
|
socket.set_option(tcp::socket::keep_alive(true));
|
|
|
|
socket.async_connect(current->endpoint(), yc);
|
|
|
|
|
|
|
|
break;
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
if (++current == result.end()) {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socket.is_open()) {
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 08:56:30 +02:00
|
|
|
#endif /* TCPSOCKET_H */
|