rippled
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ripple::ConnectAttempt Class Reference

Manages outbound peer connection attempts with comprehensive timeout handling. More...

#include <ConnectAttempt.h>

Inheritance diagram for ripple::ConnectAttempt:
Inheritance graph
[legend]
Collaboration diagram for ripple::ConnectAttempt:
Collaboration graph
[legend]

Classes

struct  StepTimeouts
 Defines timeout values for each connection step. More...
 

Public Member Functions

 ConnectAttempt (Application &app, boost::asio::io_context &io_context, endpoint_type const &remote_endpoint, Resource::Consumer usage, shared_context const &context, Peer::id_t id, std::shared_ptr< PeerFinder::Slot > const &slot, beast::Journal journal, OverlayImpl &overlay)
 Construct a new ConnectAttempt object.
 
 ~ConnectAttempt ()
 
void stop () override
 Stop the connection attempt.
 
void run ()
 Begin the connection attempt.
 
shared_from_this (T... args)
 
weak_from_this (T... args)
 

Protected Attributes

OverlayImploverlay_
 

Private Types

enum class  ConnectionStep {
  Init , TcpConnect , TlsHandshake , HttpWrite ,
  HttpRead , Complete , ShutdownStarted
}
 Represents the current phase of the connection establishment process. More...
 
using error_code = boost::system::error_code
 
using endpoint_type = boost::asio::ip::tcp::endpoint
 
using request_type = boost::beast::http::request< boost::beast::http::empty_body >
 
using response_type = boost::beast::http::response< boost::beast::http::dynamic_body >
 
using socket_type = boost::asio::ip::tcp::socket
 
using middle_type = boost::beast::tcp_stream
 
using stream_type = boost::beast::ssl_stream< middle_type >
 
using shared_context = std::shared_ptr< boost::asio::ssl::context >
 

Private Member Functions

void setTimer (ConnectionStep step)
 Set timers for the specified connection step.
 
void cancelTimer ()
 Cancel both global and step timers.
 
void onTimer (error_code ec)
 Handle timer expiration events.
 
void onConnect (error_code ec)
 
void onHandshake (error_code ec)
 
void onWrite (error_code ec)
 
void onRead (error_code ec)
 
void fail (std::string const &reason)
 
void fail (std::string const &name, error_code ec)
 
void shutdown ()
 
void tryAsyncShutdown ()
 
void onShutdown (error_code ec)
 
void close ()
 
void processResponse ()
 Process the HTTP upgrade response from peer.
 

Static Private Member Functions

static std::string stepToString (ConnectionStep step)
 
template<class = void>
static boost::asio::ip::tcp::endpoint parse_endpoint (std::string const &s, boost::system::error_code &ec)
 

Private Attributes

Applicationapp_
 
Peer::id_t const id_
 
beast::WrappedSink sink_
 
beast::Journal const journal_
 
endpoint_type remote_endpoint_
 
Resource::Consumer usage_
 
boost::asio::strand< boost::asio::io_context::executor_type > strand_
 
boost::asio::basic_waitable_timer< std::chrono::steady_clocktimer_
 
boost::asio::basic_waitable_timer< std::chrono::steady_clockstepTimer_
 
std::unique_ptr< stream_typestream_ptr_
 
socket_typesocket_
 
stream_typestream_
 
boost::beast::multi_buffer read_buf_
 
response_type response_
 
std::shared_ptr< PeerFinder::Slotslot_
 
request_type req_
 
bool shutdown_ = false
 
bool ioPending_ = false
 
ConnectionStep currentStep_ = ConnectionStep::Init
 

Static Private Attributes

static constexpr std::chrono::seconds connectTimeout {25}
 

Detailed Description

Manages outbound peer connection attempts with comprehensive timeout handling.

The ConnectAttempt class handles the complete lifecycle of establishing an outbound connection to a peer in the XRPL network. It implements a sophisticated dual-timer system that provides both global timeout protection and per-step timeout diagnostics.

The connection establishment follows these steps:

  1. TCP Connect: Establish basic network connection
  2. TLS Handshake: Negotiate SSL/TLS encryption
  3. HTTP Write: Send peer handshake request
  4. HTTP Read: Receive and validate peer response
  5. Complete: Connection successfully established

Uses a hybrid timeout approach:

All operations are serialized using boost::asio::strand to ensure thread safety. The class is designed to be used exclusively within the ASIO event loop.

Note
This class should not be used directly. It is managed by OverlayImpl as part of the peer discovery and connection management system.

Definition at line 60 of file ConnectAttempt.h.

Member Typedef Documentation

◆ error_code

using ripple::ConnectAttempt::error_code = boost::system::error_code
private

Definition at line 64 of file ConnectAttempt.h.

◆ endpoint_type

using ripple::ConnectAttempt::endpoint_type = boost::asio::ip::tcp::endpoint
private

Definition at line 65 of file ConnectAttempt.h.

◆ request_type

using ripple::ConnectAttempt::request_type = boost::beast::http::request<boost::beast::http::empty_body>
private

Definition at line 66 of file ConnectAttempt.h.

◆ response_type

using ripple::ConnectAttempt::response_type = boost::beast::http::response<boost::beast::http::dynamic_body>
private

Definition at line 68 of file ConnectAttempt.h.

◆ socket_type

using ripple::ConnectAttempt::socket_type = boost::asio::ip::tcp::socket
private

Definition at line 70 of file ConnectAttempt.h.

◆ middle_type

using ripple::ConnectAttempt::middle_type = boost::beast::tcp_stream
private

Definition at line 71 of file ConnectAttempt.h.

◆ stream_type

using ripple::ConnectAttempt::stream_type = boost::beast::ssl_stream<middle_type>
private

Definition at line 72 of file ConnectAttempt.h.

◆ shared_context

using ripple::ConnectAttempt::shared_context = std::shared_ptr<boost::asio::ssl::context>
private

Definition at line 73 of file ConnectAttempt.h.

Member Enumeration Documentation

◆ ConnectionStep

enum class ripple::ConnectAttempt::ConnectionStep
strongprivate

Represents the current phase of the connection establishment process.

Used for tracking progress and providing detailed timeout diagnostics. Each step has its own timeout value defined in StepTimeouts.

Enumerator
Init 
TcpConnect 
TlsHandshake 
HttpWrite 
HttpRead 
Complete 
ShutdownStarted 

Definition at line 83 of file ConnectAttempt.h.

Constructor & Destructor Documentation

◆ ConnectAttempt()

ripple::ConnectAttempt::ConnectAttempt ( Application app,
boost::asio::io_context &  io_context,
endpoint_type const &  remote_endpoint,
Resource::Consumer  usage,
shared_context const &  context,
Peer::id_t  id,
std::shared_ptr< PeerFinder::Slot > const &  slot,
beast::Journal  journal,
OverlayImpl overlay 
)

Construct a new ConnectAttempt object.

Parameters
appApplication context providing configuration and services
io_contextASIO I/O context for async operations
remote_endpointTarget peer endpoint to connect to
usageResource usage tracker for rate limiting
contextShared SSL context for encryption
idUnique peer identifier for this connection attempt
slotPeerFinder slot representing this connection
journalLogging interface for diagnostics
overlayParent overlay manager
Note
The constructor only initializes the object. Call run() to begin the actual connection attempt.

Definition at line 31 of file ConnectAttempt.cpp.

◆ ~ConnectAttempt()

ripple::ConnectAttempt::~ConnectAttempt ( )

Definition at line 60 of file ConnectAttempt.cpp.

Member Function Documentation

◆ stop()

void ripple::ConnectAttempt::stop ( )
overridevirtual

Stop the connection attempt.

This method is thread-safe and can be called from any thread.

Implements ripple::OverlayImpl::Child.

Definition at line 69 of file ConnectAttempt.cpp.

◆ run()

void ripple::ConnectAttempt::run ( )

Begin the connection attempt.

This method is thread-safe and posts to the strand if needed.

Definition at line 84 of file ConnectAttempt.cpp.

◆ setTimer()

void ripple::ConnectAttempt::setTimer ( ConnectionStep  step)
private

Set timers for the specified connection step.

Parameters
stepThe connection step to set timers for

Sets both the step-specific timer and the global timer (if not already set).

Definition at line 212 of file ConnectAttempt.cpp.

◆ cancelTimer()

void ripple::ConnectAttempt::cancelTimer ( )
private

Cancel both global and step timers.

Used during cleanup and when connection completes successfully. Exceptions from timer cancellation are safely ignored.

Definition at line 283 of file ConnectAttempt.cpp.

◆ onTimer()

void ripple::ConnectAttempt::onTimer ( error_code  ec)
private

Handle timer expiration events.

Parameters
ecError code from timer operation

Determines which timer expired (global vs step) and logs appropriate diagnostic information before terminating the connection.

Definition at line 297 of file ConnectAttempt.cpp.

◆ onConnect()

void ripple::ConnectAttempt::onConnect ( error_code  ec)
private

Definition at line 337 of file ConnectAttempt.cpp.

◆ onHandshake()

void ripple::ConnectAttempt::onHandshake ( error_code  ec)
private

Definition at line 376 of file ConnectAttempt.cpp.

◆ onWrite()

void ripple::ConnectAttempt::onWrite ( error_code  ec)
private

Definition at line 435 of file ConnectAttempt.cpp.

◆ onRead()

void ripple::ConnectAttempt::onRead ( error_code  ec)
private

Definition at line 467 of file ConnectAttempt.cpp.

◆ fail() [1/2]

void ripple::ConnectAttempt::fail ( std::string const &  reason)
private

Definition at line 198 of file ConnectAttempt.cpp.

◆ fail() [2/2]

void ripple::ConnectAttempt::fail ( std::string const &  name,
error_code  ec 
)
private

Definition at line 205 of file ConnectAttempt.cpp.

◆ shutdown()

void ripple::ConnectAttempt::shutdown ( )
private

Definition at line 110 of file ConnectAttempt.cpp.

◆ tryAsyncShutdown()

void ripple::ConnectAttempt::tryAsyncShutdown ( )
private

Definition at line 126 of file ConnectAttempt.cpp.

◆ onShutdown()

void ripple::ConnectAttempt::onShutdown ( error_code  ec)
private

Definition at line 155 of file ConnectAttempt.cpp.

◆ close()

void ripple::ConnectAttempt::close ( )
private

Definition at line 183 of file ConnectAttempt.cpp.

◆ processResponse()

void ripple::ConnectAttempt::processResponse ( )
private

Process the HTTP upgrade response from peer.

Validates the peer's response, extracts protocol information, verifies handshake, and either creates a PeerImp or handles redirect responses.

Definition at line 496 of file ConnectAttempt.cpp.

◆ stepToString()

static std::string ripple::ConnectAttempt::stepToString ( ConnectionStep  step)
staticprivate

Definition at line 255 of file ConnectAttempt.h.

◆ parse_endpoint()

template<class = void>
static boost::asio::ip::tcp::endpoint ripple::ConnectAttempt::parse_endpoint ( std::string const &  s,
boost::system::error_code &  ec 
)
staticprivate

Definition at line 279 of file ConnectAttempt.h.

Member Data Documentation

◆ connectTimeout

constexpr std::chrono::seconds ripple::ConnectAttempt::connectTimeout {25}
staticconstexprprivate

Definition at line 94 of file ConnectAttempt.h.

◆ app_

Application& ripple::ConnectAttempt::app_
private

Definition at line 118 of file ConnectAttempt.h.

◆ id_

Peer::id_t const ripple::ConnectAttempt::id_
private

Definition at line 119 of file ConnectAttempt.h.

◆ sink_

beast::WrappedSink ripple::ConnectAttempt::sink_
private

Definition at line 120 of file ConnectAttempt.h.

◆ journal_

beast::Journal const ripple::ConnectAttempt::journal_
private

Definition at line 121 of file ConnectAttempt.h.

◆ remote_endpoint_

endpoint_type ripple::ConnectAttempt::remote_endpoint_
private

Definition at line 122 of file ConnectAttempt.h.

◆ usage_

Resource::Consumer ripple::ConnectAttempt::usage_
private

Definition at line 123 of file ConnectAttempt.h.

◆ strand_

boost::asio::strand<boost::asio::io_context::executor_type> ripple::ConnectAttempt::strand_
private

Definition at line 125 of file ConnectAttempt.h.

◆ timer_

boost::asio::basic_waitable_timer<std::chrono::steady_clock> ripple::ConnectAttempt::timer_
private

Definition at line 126 of file ConnectAttempt.h.

◆ stepTimer_

boost::asio::basic_waitable_timer<std::chrono::steady_clock> ripple::ConnectAttempt::stepTimer_
private

Definition at line 127 of file ConnectAttempt.h.

◆ stream_ptr_

std::unique_ptr<stream_type> ripple::ConnectAttempt::stream_ptr_
private

Definition at line 129 of file ConnectAttempt.h.

◆ socket_

socket_type& ripple::ConnectAttempt::socket_
private

Definition at line 130 of file ConnectAttempt.h.

◆ stream_

stream_type& ripple::ConnectAttempt::stream_
private

Definition at line 131 of file ConnectAttempt.h.

◆ read_buf_

boost::beast::multi_buffer ripple::ConnectAttempt::read_buf_
private

Definition at line 132 of file ConnectAttempt.h.

◆ response_

response_type ripple::ConnectAttempt::response_
private

Definition at line 134 of file ConnectAttempt.h.

◆ slot_

std::shared_ptr<PeerFinder::Slot> ripple::ConnectAttempt::slot_
private

Definition at line 135 of file ConnectAttempt.h.

◆ req_

request_type ripple::ConnectAttempt::req_
private

Definition at line 136 of file ConnectAttempt.h.

◆ shutdown_

bool ripple::ConnectAttempt::shutdown_ = false
private

Definition at line 138 of file ConnectAttempt.h.

◆ ioPending_

bool ripple::ConnectAttempt::ioPending_ = false
private

Definition at line 139 of file ConnectAttempt.h.

◆ currentStep_

ConnectionStep ripple::ConnectAttempt::currentStep_ = ConnectionStep::Init
private

Definition at line 140 of file ConnectAttempt.h.

◆ overlay_

OverlayImpl& ripple::OverlayImpl::Child::overlay_
protectedinherited

Definition at line 68 of file OverlayImpl.h.