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{
42private:
43 using error_code = boost::system::error_code;
44 using endpoint_type = boost::asio::ip::tcp::endpoint;
45 using request_type = boost::beast::http::request<boost::beast::http::empty_body>;
46 using response_type = boost::beast::http::response<boost::beast::http::dynamic_body>;
47 using socket_type = boost::asio::ip::tcp::socket;
48 using middle_type = boost::beast::tcp_stream;
49 using stream_type = boost::beast::ssl_stream<middle_type>;
51
60 enum class ConnectionStep {
61 Init, // Initial state, nothing started
62 TcpConnect, // Establishing TCP connection to remote peer
63 TlsHandshake, // Performing SSL/TLS handshake
64 HttpWrite, // Sending HTTP upgrade request
65 HttpRead, // Reading HTTP upgrade response
66 Complete, // Connection successfully established
67 ShutdownStarted // Connection shutdown has started
68 };
69
70 // A timeout for connection process, greater than all step timeouts
72
81 {
82 // TCP connection timeout
83 static constexpr std::chrono::seconds tcpConnect{8};
84 // SSL handshake timeout
86 // HTTP write timeout
87 static constexpr std::chrono::seconds httpWrite{3};
88 // HTTP read timeout
89 static constexpr std::chrono::seconds httpRead{3};
90 // SSL shutdown timeout
91 static constexpr std::chrono::seconds tlsShutdown{2};
92 };
93
94 // Core application and networking components
101
102 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
103 boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
104 boost::asio::basic_waitable_timer<std::chrono::steady_clock> stepTimer_;
105
109 boost::beast::multi_buffer read_buf_;
110
114
115 bool shutdown_ = false; // Shutdown has been initiated
116 bool ioPending_ = false; // Async I/O operation in progress
118
119public:
137 Application& app,
138 boost::asio::io_context& io_context,
139 endpoint_type const& remote_endpoint,
140 Resource::Consumer usage,
141 shared_context const& context,
142 Peer::id_t id,
144 beast::Journal journal,
145 OverlayImpl& overlay);
146
148
154 void
155 stop() override;
156
162 void
163 run();
164
165private:
174 void
176
183 void
184 cancelTimer();
185
194 void
196
197 // Connection phase handlers
198 void
199 onConnect(error_code ec); // TCP connection completion handler
200 void
201 onHandshake(error_code ec); // TLS handshake completion handler
202 void
203 onWrite(error_code ec); // HTTP write completion handler
204 void
205 onRead(error_code ec); // HTTP read completion handler
206
207 // Error and cleanup handlers
208 void
209 fail(std::string const& reason); // Fail with custom reason
210 void
211 fail(std::string const& name, error_code ec); // Fail with system error
212 void
213 shutdown(); // Initiate graceful shutdown
214 void
215 tryAsyncShutdown(); // Attempt async SSL shutdown
216 void
217 onShutdown(error_code ec); // SSL shutdown completion handler
218 void
219 close(); // Force close socket
220
228 void
230
231 static std::string
233 {
234 switch (step)
235 {
237 return "Init";
239 return "TcpConnect";
241 return "TlsHandshake";
243 return "HttpWrite";
245 return "HttpRead";
247 return "Complete";
249 return "ShutdownStarted";
250 }
251 return "Unknown";
252 };
253
254 template <class = void>
255 static boost::asio::ip::tcp::endpoint
256 parse_endpoint(std::string const& s, boost::system::error_code& ec)
257 {
259 std::istringstream is(s);
260 is >> bep;
261 if (is.fail())
262 {
263 ec = boost::system::errc::make_error_code(boost::system::errc::invalid_argument);
264 return boost::asio::ip::tcp::endpoint{};
265 }
266
268 }
269};
270
271} // 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