icinga2/lib/base/tcpsocket.hpp

97 lines
2.0 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#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"
#include "base/io-engine.hpp"
2014-05-25 16:23:35 +02:00
#include "base/socket.hpp"
#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
{
/**
* A TCP socket. DEPRECATED - Use Boost ASIO instead.
2012-05-18 22:21:28 +02:00
*
* @ingroup base
*/
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);
void Connect(const String& node, const String& service);
2012-03-28 13:24:49 +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;
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();
}
}
}
}
template<class Socket>
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());
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
}
#endif /* TCPSOCKET_H */