2026-01-27 09:06:40 -05:00
|
|
|
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
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"
|
2023-02-28 04:57:54 -05:00
|
|
|
#include <boost/asio/error.hpp>
|
2019-02-25 10:18:48 -05:00
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
|
|
|
#include <boost/asio/spawn.hpp>
|
2023-02-28 04:57:54 -05:00
|
|
|
#include <boost/system/system_error.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());
|
2025-04-11 06:27:25 -04:00
|
|
|
auto result (resolver.resolve(node.GetData(), service.GetData()));
|
2019-02-25 10:40:14 -05:00
|
|
|
auto current (result.begin());
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
try {
|
|
|
|
|
socket.open(current->endpoint().protocol());
|
|
|
|
|
socket.set_option(tcp::socket::keep_alive(true));
|
|
|
|
|
socket.connect(current->endpoint());
|
|
|
|
|
|
|
|
|
|
break;
|
2023-02-28 04:57:54 -05:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
auto se (dynamic_cast<const boost::system::system_error*>(&ex));
|
|
|
|
|
|
2023-03-24 10:38:50 -04:00
|
|
|
if ((se && se->code() == boost::asio::error::operation_aborted) || ++current == result.end()) {
|
2019-02-25 10:40:14 -05:00
|
|
|
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());
|
2025-04-11 06:27:25 -04:00
|
|
|
auto result (resolver.async_resolve(node.GetData(), service.GetData(), yc));
|
2019-02-25 10:18:48 -05:00
|
|
|
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;
|
2023-02-28 04:57:54 -05:00
|
|
|
} catch (const std::exception& ex) {
|
2026-04-02 07:58:12 -04:00
|
|
|
Log(LogDebug, "Connect") << "Execption: " << ex.what();
|
2023-02-28 04:57:54 -05:00
|
|
|
auto se (dynamic_cast<const boost::system::system_error*>(&ex));
|
|
|
|
|
|
2023-03-24 10:38:50 -04:00
|
|
|
if ((se && se->code() == boost::asio::error::operation_aborted) || ++current == result.end()) {
|
2019-02-25 10:18:48 -05:00
|
|
|
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 */
|