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