79 {
80 std::shared_ptr<boost::asio::ip::tcp::socket> sock(new boost::asio::ip::tcp::socket(asio::default_io_service()));
81
82 bool connected = false;
84 {
85
86 try
87 {
88 asio::tcp::connect(*sock, *
endpoint);
89 connected = true;
90 }
91 catch (exception& er)
92 {
93 ilog(
"Failed to connect to GNTP service using an endpoint that previously worked: ${error_report}",
94 ("error_report", er.to_detail_string()));
95 sock->close();
96
97 endpoint = std::optional<boost::asio::ip::tcp::endpoint>();
98 }
99 catch (...)
100 {
101 ilog(
"Failed to connect to GNTP service using an endpoint that previously worked");
102 sock->close();
103
104 endpoint = std::optional<boost::asio::ip::tcp::endpoint>();
105 }
106 }
107 if (!connected)
108 {
109
110 auto eps = asio::tcp::resolve(
hostname, boost::lexical_cast<std::string>(
port));
111 if (eps.size() == 0)
113
114 for (
uint32_t i = 0; i < eps.size(); ++i)
115 {
116 try
117 {
118 boost::system::error_code ec;
119 ilog(
"Attempting to connect to GNTP srvice");
120 asio::tcp::connect(*sock, eps[i]);
122 connected = true;
123 break;
124 }
125 catch (const exception& er)
126 {
127 ilog(
"Failed to connect to GNTP service: ${error_reprot}",
128 ("error_report", er.to_detail_string()) );
129 sock->close();
130 }
131 catch (...)
132 {
133 ilog(
"Failed to connect to GNTP service");
134 sock->close();
135 }
136 }
137 }
138 if (!connected)
139 FC_THROW(
"Unable to connect to any resolved endpoint for ${host}:${port}",
141 try
142 {
143 asio::ostream<boost::asio::ip::tcp::socket> write_stream(sock);
144 write_stream.write(message.c_str(), message.size());
145 write_stream.flush();
146 write_stream.close();
147 }
148 catch (exception& er)
149 {
151 }
152 catch (...)
153 {
154 FC_THROW(
"Caught an exception while sending data to GNTP service");
155 }
156 }
std::optional< boost::asio::ip::tcp::endpoint > endpoint
#define FC_RETHROW_EXCEPTION(ER, LOG_LEVEL, FORMAT,...)
Appends a log_message to the exception ER and rethrows it.