rippled
Loading...
Searching...
No Matches
ConnectAttempt.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_OVERLAY_CONNECTATTEMPT_H_INCLUDED
21#define RIPPLE_OVERLAY_CONNECTATTEMPT_H_INCLUDED
22
23#include <xrpld/overlay/detail/OverlayImpl.h>
24
25#include <chrono>
26
27namespace ripple {
28
61 public std::enable_shared_from_this<ConnectAttempt>
62{
63private:
64 using error_code = boost::system::error_code;
65 using endpoint_type = boost::asio::ip::tcp::endpoint;
67 boost::beast::http::request<boost::beast::http::empty_body>;
69 boost::beast::http::response<boost::beast::http::dynamic_body>;
70 using socket_type = boost::asio::ip::tcp::socket;
71 using middle_type = boost::beast::tcp_stream;
72 using stream_type = boost::beast::ssl_stream<middle_type>;
74
83 enum class ConnectionStep {
84 Init, // Initial state, nothing started
85 TcpConnect, // Establishing TCP connection to remote peer
86 TlsHandshake, // Performing SSL/TLS handshake
87 HttpWrite, // Sending HTTP upgrade request
88 HttpRead, // Reading HTTP upgrade response
89 Complete, // Connection successfully established
90 ShutdownStarted // Connection shutdown has started
91 };
92
93 // A timeout for connection process, greater than all step timeouts
95
104 {
105 // TCP connection timeout
106 static constexpr std::chrono::seconds tcpConnect{8};
107 // SSL handshake timeout
109 // HTTP write timeout
110 static constexpr std::chrono::seconds httpWrite{3};
111 // HTTP read timeout
112 static constexpr std::chrono::seconds httpRead{3};
113 // SSL shutdown timeout
115 };
116
117 // Core application and networking components
124
125 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
126 boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
127 boost::asio::basic_waitable_timer<std::chrono::steady_clock> stepTimer_;
128
132 boost::beast::multi_buffer read_buf_;
133
137
138 bool shutdown_ = false; // Shutdown has been initiated
139 bool ioPending_ = false; // Async I/O operation in progress
141
142public:
160 Application& app,
161 boost::asio::io_context& io_context,
162 endpoint_type const& remote_endpoint,
163 Resource::Consumer usage,
164 shared_context const& context,
165 Peer::id_t id,
167 beast::Journal journal,
168 OverlayImpl& overlay);
169
171
177 void
178 stop() override;
179
185 void
186 run();
187
188private:
197 void
199
206 void
207 cancelTimer();
208
217 void
219
220 // Connection phase handlers
221 void
222 onConnect(error_code ec); // TCP connection completion handler
223 void
224 onHandshake(error_code ec); // TLS handshake completion handler
225 void
226 onWrite(error_code ec); // HTTP write completion handler
227 void
228 onRead(error_code ec); // HTTP read completion handler
229
230 // Error and cleanup handlers
231 void
232 fail(std::string const& reason); // Fail with custom reason
233 void
234 fail(std::string const& name, error_code ec); // Fail with system error
235 void
236 shutdown(); // Initiate graceful shutdown
237 void
238 tryAsyncShutdown(); // Attempt async SSL shutdown
239 void
240 onShutdown(error_code ec); // SSL shutdown completion handler
241 void
242 close(); // Force close socket
243
251 void
253
254 static std::string
256 {
257 switch (step)
258 {
260 return "Init";
262 return "TcpConnect";
264 return "TlsHandshake";
266 return "HttpWrite";
268 return "HttpRead";
270 return "Complete";
272 return "ShutdownStarted";
273 }
274 return "Unknown";
275 };
276
277 template <class = void>
278 static boost::asio::ip::tcp::endpoint
279 parse_endpoint(std::string const& s, boost::system::error_code& ec)
280 {
282 std::istringstream is(s);
283 is >> bep;
284 if (is.fail())
285 {
286 ec = boost::system::errc::make_error_code(
287 boost::system::errc::invalid_argument);
288 return boost::asio::ip::tcp::endpoint{};
289 }
290
292 }
293};
294
295} // namespace ripple
296
297#endif
A version-independent IP address and port combination.
Definition IPEndpoint.h:38
A generic endpoint for log messages.
Definition Journal.h:60
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:34
Manages outbound peer connection attempts with comprehensive timeout handling.
static constexpr std::chrono::seconds connectTimeout
boost::beast::tcp_stream middle_type
void stop() override
Stop the connection attempt.
boost::asio::ip::tcp::socket socket_type
void cancelTimer()
Cancel both global and step timers.
boost::system::error_code error_code
std::unique_ptr< stream_type > stream_ptr_
std::shared_ptr< PeerFinder::Slot > slot_
ConnectionStep currentStep_
Resource::Consumer usage_
boost::asio::strand< boost::asio::io_context::executor_type > strand_
static std::string stepToString(ConnectionStep step)
void run()
Begin the connection attempt.
void onHandshake(error_code ec)
boost::asio::ip::tcp::endpoint endpoint_type
void onWrite(error_code ec)
void processResponse()
Process the HTTP upgrade response from peer.
void onTimer(error_code ec)
Handle timer expiration events.
void setTimer(ConnectionStep step)
Set timers for the specified connection step.
boost::beast::http::response< boost::beast::http::dynamic_body > response_type
void onShutdown(error_code ec)
boost::beast::ssl_stream< middle_type > stream_type
boost::beast::multi_buffer read_buf_
void onConnect(error_code ec)
beast::WrappedSink sink_
void fail(std::string const &reason)
void onRead(error_code ec)
static boost::asio::ip::tcp::endpoint parse_endpoint(std::string const &s, boost::system::error_code &ec)
beast::Journal const journal_
boost::beast::http::request< boost::beast::http::empty_body > request_type
ConnectionStep
Represents the current phase of the connection establishment process.
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
endpoint_type remote_endpoint_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > stepTimer_
An endpoint that consumes resources.
Definition Consumer.h:35
T fail(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
static boost::asio::ip::tcp::endpoint to_asio_endpoint(IP::Endpoint const &address)
Defines timeout values for each connection step.
static constexpr std::chrono::seconds tcpConnect
static constexpr std::chrono::seconds tlsShutdown
static constexpr std::chrono::seconds tlsHandshake
static constexpr std::chrono::seconds httpWrite
static constexpr std::chrono::seconds httpRead