rippled
Loading...
Searching...
No Matches
ConnectAttempt.h
1#pragma once
2
3#include <xrpld/overlay/detail/OverlayImpl.h>
4
5#include <chrono>
6
7namespace xrpl {
8
41 public std::enable_shared_from_this<ConnectAttempt>
42{
43private:
44 using error_code = boost::system::error_code;
45 using endpoint_type = boost::asio::ip::tcp::endpoint;
46 using request_type = boost::beast::http::request<boost::beast::http::empty_body>;
47 using response_type = boost::beast::http::response<boost::beast::http::dynamic_body>;
48 using socket_type = boost::asio::ip::tcp::socket;
49 using middle_type = boost::beast::tcp_stream;
50 using stream_type = boost::beast::ssl_stream<middle_type>;
52
61 enum class ConnectionStep {
62 Init, // Initial state, nothing started
63 TcpConnect, // Establishing TCP connection to remote peer
64 TlsHandshake, // Performing SSL/TLS handshake
65 HttpWrite, // Sending HTTP upgrade request
66 HttpRead, // Reading HTTP upgrade response
67 Complete, // Connection successfully established
68 ShutdownStarted // Connection shutdown has started
69 };
70
71 // A timeout for connection process, greater than all step timeouts
73
82 {
83 // TCP connection timeout
84 static constexpr std::chrono::seconds tcpConnect{8};
85 // SSL handshake timeout
87 // HTTP write timeout
88 static constexpr std::chrono::seconds httpWrite{3};
89 // HTTP read timeout
90 static constexpr std::chrono::seconds httpRead{3};
91 // SSL shutdown timeout
92 static constexpr std::chrono::seconds tlsShutdown{2};
93 };
94
95 // Core application and networking components
102
103 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
104 boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
105 boost::asio::basic_waitable_timer<std::chrono::steady_clock> stepTimer_;
106
110 boost::beast::multi_buffer read_buf_;
111
115
116 bool shutdown_ = false; // Shutdown has been initiated
117 bool ioPending_ = false; // Async I/O operation in progress
119
120public:
138 Application& app,
139 boost::asio::io_context& io_context,
140 endpoint_type const& remote_endpoint,
141 Resource::Consumer usage,
142 shared_context const& context,
143 Peer::id_t id,
145 beast::Journal journal,
146 OverlayImpl& overlay);
147
148 virtual ~ConnectAttempt();
149
155 void
156 stop() override;
157
163 void
164 run();
165
166private:
175 void
177
184 void
185 cancelTimer();
186
195 void
197
198 // Connection phase handlers
199 void
200 onConnect(error_code ec); // TCP connection completion handler
201 void
202 onHandshake(error_code ec); // TLS handshake completion handler
203 void
204 onWrite(error_code ec); // HTTP write completion handler
205 void
206 onRead(error_code ec); // HTTP read completion handler
207
208 // Error and cleanup handlers
209 void
210 fail(std::string const& reason); // Fail with custom reason
211 void
212 fail(std::string const& name, error_code ec); // Fail with system error
213 void
214 shutdown(); // Initiate graceful shutdown
215 void
216 tryAsyncShutdown(); // Attempt async SSL shutdown
217 void
218 onShutdown(error_code ec); // SSL shutdown completion handler
219 void
220 close(); // Force close socket
221
229 void
231
232 static std::string
234 {
235 switch (step)
236 {
238 return "Init";
240 return "TcpConnect";
242 return "TlsHandshake";
244 return "HttpWrite";
246 return "HttpRead";
248 return "Complete";
250 return "ShutdownStarted";
251 }
252 return "Unknown";
253 };
254
255 template <class = void>
256 static boost::asio::ip::tcp::endpoint
257 parse_endpoint(std::string const& s, boost::system::error_code& ec)
258 {
260 std::istringstream is(s);
261 is >> bep;
262 if (is.fail())
263 {
264 ec = boost::system::errc::make_error_code(boost::system::errc::invalid_argument);
265 return boost::asio::ip::tcp::endpoint{};
266 }
267
269 }
270};
271
272} // namespace xrpl
A version-independent IP address and port combination.
Definition IPEndpoint.h:18
A generic endpoint for log messages.
Definition Journal.h:40
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:14
Manages outbound peer connection attempts with comprehensive timeout handling.
boost::system::error_code error_code
boost::beast::ssl_stream< middle_type > stream_type
void fail(std::string const &reason)
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
boost::beast::http::request< boost::beast::http::empty_body > request_type
void setTimer(ConnectionStep step)
Set timers for the specified connection step.
void processResponse()
Process the HTTP upgrade response from peer.
boost::asio::strand< boost::asio::io_context::executor_type > strand_
void stop() override
Stop the connection attempt.
boost::beast::tcp_stream middle_type
ConnectionStep currentStep_
void onRead(error_code ec)
static std::string stepToString(ConnectionStep step)
std::unique_ptr< stream_type > stream_ptr_
void onConnect(error_code ec)
void onShutdown(error_code ec)
std::shared_ptr< PeerFinder::Slot > slot_
ConnectionStep
Represents the current phase of the connection establishment process.
response_type response_
boost::asio::ip::tcp::socket socket_type
boost::beast::multi_buffer read_buf_
void run()
Begin the connection attempt.
Peer::id_t const id_
boost::asio::ip::tcp::endpoint endpoint_type
endpoint_type remote_endpoint_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > stepTimer_
void onHandshake(error_code ec)
static boost::asio::ip::tcp::endpoint parse_endpoint(std::string const &s, boost::system::error_code &ec)
static constexpr std::chrono::seconds connectTimeout
Resource::Consumer usage_
boost::beast::http::response< boost::beast::http::dynamic_body > response_type
beast::WrappedSink sink_
beast::Journal const journal_
void onWrite(error_code ec)
void onTimer(error_code ec)
Handle timer expiration events.
void cancelTimer()
Cancel both global and step timers.
An endpoint that consumes resources.
Definition Consumer.h:16
T fail(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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 tlsHandshake
static constexpr std::chrono::seconds httpWrite
static constexpr std::chrono::seconds httpRead
static constexpr std::chrono::seconds tlsShutdown