rippled
Loading...
Searching...
No Matches
ConnectAttempt.h
1#ifndef XRPL_OVERLAY_CONNECTATTEMPT_H_INCLUDED
2#define XRPL_OVERLAY_CONNECTATTEMPT_H_INCLUDED
3
4#include <xrpld/overlay/detail/OverlayImpl.h>
5
6#include <chrono>
7
8namespace ripple {
9
42 public std::enable_shared_from_this<ConnectAttempt>
43{
44private:
45 using error_code = boost::system::error_code;
46 using endpoint_type = boost::asio::ip::tcp::endpoint;
48 boost::beast::http::request<boost::beast::http::empty_body>;
50 boost::beast::http::response<boost::beast::http::dynamic_body>;
51 using socket_type = boost::asio::ip::tcp::socket;
52 using middle_type = boost::beast::tcp_stream;
53 using stream_type = boost::beast::ssl_stream<middle_type>;
55
64 enum class ConnectionStep {
65 Init, // Initial state, nothing started
66 TcpConnect, // Establishing TCP connection to remote peer
67 TlsHandshake, // Performing SSL/TLS handshake
68 HttpWrite, // Sending HTTP upgrade request
69 HttpRead, // Reading HTTP upgrade response
70 Complete, // Connection successfully established
71 ShutdownStarted // Connection shutdown has started
72 };
73
74 // A timeout for connection process, greater than all step timeouts
76
85 {
86 // TCP connection timeout
87 static constexpr std::chrono::seconds tcpConnect{8};
88 // SSL handshake timeout
90 // HTTP write timeout
91 static constexpr std::chrono::seconds httpWrite{3};
92 // HTTP read timeout
93 static constexpr std::chrono::seconds httpRead{3};
94 // SSL shutdown timeout
95 static constexpr std::chrono::seconds tlsShutdown{2};
96 };
97
98 // Core application and networking components
105
106 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
107 boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
108 boost::asio::basic_waitable_timer<std::chrono::steady_clock> stepTimer_;
109
113 boost::beast::multi_buffer read_buf_;
114
118
119 bool shutdown_ = false; // Shutdown has been initiated
120 bool ioPending_ = false; // Async I/O operation in progress
122
123public:
141 Application& app,
142 boost::asio::io_context& io_context,
143 endpoint_type const& remote_endpoint,
144 Resource::Consumer usage,
145 shared_context const& context,
146 Peer::id_t id,
148 beast::Journal journal,
149 OverlayImpl& overlay);
150
152
158 void
159 stop() override;
160
166 void
167 run();
168
169private:
178 void
180
187 void
188 cancelTimer();
189
198 void
200
201 // Connection phase handlers
202 void
203 onConnect(error_code ec); // TCP connection completion handler
204 void
205 onHandshake(error_code ec); // TLS handshake completion handler
206 void
207 onWrite(error_code ec); // HTTP write completion handler
208 void
209 onRead(error_code ec); // HTTP read completion handler
210
211 // Error and cleanup handlers
212 void
213 fail(std::string const& reason); // Fail with custom reason
214 void
215 fail(std::string const& name, error_code ec); // Fail with system error
216 void
217 shutdown(); // Initiate graceful shutdown
218 void
219 tryAsyncShutdown(); // Attempt async SSL shutdown
220 void
221 onShutdown(error_code ec); // SSL shutdown completion handler
222 void
223 close(); // Force close socket
224
232 void
234
235 static std::string
237 {
238 switch (step)
239 {
241 return "Init";
243 return "TcpConnect";
245 return "TlsHandshake";
247 return "HttpWrite";
249 return "HttpRead";
251 return "Complete";
253 return "ShutdownStarted";
254 }
255 return "Unknown";
256 };
257
258 template <class = void>
259 static boost::asio::ip::tcp::endpoint
260 parse_endpoint(std::string const& s, boost::system::error_code& ec)
261 {
263 std::istringstream is(s);
264 is >> bep;
265 if (is.fail())
266 {
267 ec = boost::system::errc::make_error_code(
268 boost::system::errc::invalid_argument);
269 return boost::asio::ip::tcp::endpoint{};
270 }
271
273 }
274};
275
276} // namespace ripple
277
278#endif
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
A generic endpoint for log messages.
Definition Journal.h:41
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:15
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:17
T fail(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
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