]> granicus.if.org Git - icinga2/blob - lib/base/tcpsocket.hpp
Merge pull request #7591 from Icinga/feature/docs-api-joins
[icinga2] / lib / base / tcpsocket.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef TCPSOCKET_H
4 #define TCPSOCKET_H
5
6 #include "base/i2-base.hpp"
7 #include "base/io-engine.hpp"
8 #include "base/socket.hpp"
9 #include <boost/asio/ip/tcp.hpp>
10 #include <boost/asio/spawn.hpp>
11
12 namespace icinga
13 {
14
15 /**
16  * A TCP socket. DEPRECATED - Use Boost ASIO instead.
17  *
18  * @ingroup base
19  */
20 class TcpSocket final : public Socket
21 {
22 public:
23         DECLARE_PTR_TYPEDEFS(TcpSocket);
24
25         void Bind(const String& service, int family);
26         void Bind(const String& node, const String& service, int family);
27
28         void Connect(const String& node, const String& service);
29 };
30
31 /**
32  * TCP Connect based on Boost ASIO.
33  *
34  * @ingroup base
35  */
36 template<class Socket>
37 void Connect(Socket& socket, const String& node, const String& service)
38 {
39         using boost::asio::ip::tcp;
40
41         tcp::resolver resolver (IoEngine::Get().GetIoContext());
42         tcp::resolver::query query (node, service);
43         auto result (resolver.resolve(query));
44         auto current (result.begin());
45
46         for (;;) {
47                 try {
48                         socket.open(current->endpoint().protocol());
49                         socket.set_option(tcp::socket::keep_alive(true));
50                         socket.connect(current->endpoint());
51
52                         break;
53                 } catch (const std::exception&) {
54                         if (++current == result.end()) {
55                                 throw;
56                         }
57
58                         if (socket.is_open()) {
59                                 socket.close();
60                         }
61                 }
62         }
63 }
64
65 template<class Socket>
66 void Connect(Socket& socket, const String& node, const String& service, boost::asio::yield_context yc)
67 {
68         using boost::asio::ip::tcp;
69
70         tcp::resolver resolver (IoEngine::Get().GetIoContext());
71         tcp::resolver::query query (node, service);
72         auto result (resolver.async_resolve(query, yc));
73         auto current (result.begin());
74
75         for (;;) {
76                 try {
77                         socket.open(current->endpoint().protocol());
78                         socket.set_option(tcp::socket::keep_alive(true));
79                         socket.async_connect(current->endpoint(), yc);
80
81                         break;
82                 } catch (const std::exception&) {
83                         if (++current == result.end()) {
84                                 throw;
85                         }
86
87                         if (socket.is_open()) {
88                                 socket.close();
89                         }
90                 }
91         }
92 }
93
94 }
95
96 #endif /* TCPSOCKET_H */