rippled
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 <ripple/overlay/impl/OverlayImpl.h>
24 #include <ripple/overlay/impl/Tuning.h>
25 
26 namespace ripple {
27 
30  : public OverlayImpl::Child
31  , public std::enable_shared_from_this<ConnectAttempt>
32 {
33 private:
34  using error_code = boost::system::error_code;
35 
36  using endpoint_type = boost::asio::ip::tcp::endpoint;
37 
38  using request_type =
39  boost::beast::http::request<boost::beast::http::empty_body>;
40 
41  using response_type =
42  boost::beast::http::response<boost::beast::http::dynamic_body>;
43 
44  using socket_type = boost::asio::ip::tcp::socket;
45  using middle_type = boost::beast::tcp_stream;
46  using stream_type = boost::beast::ssl_stream <middle_type>;
48 
55  boost::asio::io_service::strand strand_;
56  boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
60  boost::beast::multi_buffer read_buf_;
64 
65 public:
66  ConnectAttempt (Application& app, boost::asio::io_service& io_service,
67  endpoint_type const& remote_endpoint, Resource::Consumer usage,
68  shared_context const& context,
70  beast::Journal journal, OverlayImpl& overlay);
71 
73 
74  void
75  stop() override;
76 
77  void
78  run();
79 
80 private:
81 
82  void close();
83  void fail (std::string const& reason);
84  void fail (std::string const& name, error_code ec);
85  void setTimer();
86  void cancelTimer();
87  void onTimer (error_code ec);
88  void onConnect (error_code ec);
89  void onHandshake (error_code ec);
90  void onWrite (error_code ec);
91  void onRead (error_code ec);
92  void onShutdown (error_code ec);
93 
94  static
96  makeRequest (bool crawl, bool compressionEnabled);
97 
98  void processResponse();
99 
100  template <class = void>
101  static
102  boost::asio::ip::tcp::endpoint
103  parse_endpoint (std::string const& s, boost::system::error_code& ec)
104  {
106  std::istringstream is(s);
107  is >> bep;
108  if (is.fail())
109  {
110  ec = boost::system::errc::make_error_code(
111  boost::system::errc::invalid_argument);
112  return boost::asio::ip::tcp::endpoint{};
113  }
114 
116  }
117 };
118 
119 }
120 
121 #endif
ripple::ConnectAttempt::onHandshake
void onHandshake(error_code ec)
Definition: ConnectAttempt.cpp:177
ripple::Application
Definition: Application.h:85
ripple::ConnectAttempt::cancelTimer
void cancelTimer()
Definition: ConnectAttempt.cpp:129
ripple::ConnectAttempt::remote_endpoint_
endpoint_type remote_endpoint_
Definition: ConnectAttempt.h:53
ripple::ConnectAttempt::sink_
beast::WrappedSink sink_
Definition: ConnectAttempt.h:51
std::string
STL class.
std::shared_ptr< boost::asio::ssl::context >
ripple::ConnectAttempt::strand_
boost::asio::io_service::strand strand_
Definition: ConnectAttempt.h:55
ripple::ConnectAttempt::response_type
boost::beast::http::response< boost::beast::http::dynamic_body > response_type
Definition: ConnectAttempt.h:42
ripple::ConnectAttempt::journal_
const beast::Journal journal_
Definition: ConnectAttempt.h:52
std::istringstream
STL class.
ripple::ConnectAttempt::stream_type
boost::beast::ssl_stream< middle_type > stream_type
Definition: ConnectAttempt.h:46
ripple::ConnectAttempt::request_type
boost::beast::http::request< boost::beast::http::empty_body > request_type
Definition: ConnectAttempt.h:39
ripple::ConnectAttempt::processResponse
void processResponse()
Definition: ConnectAttempt.cpp:284
ripple::ConnectAttempt::socket_type
boost::asio::ip::tcp::socket socket_type
Definition: ConnectAttempt.h:44
ripple::ConnectAttempt::response_
response_type response_
Definition: ConnectAttempt.h:61
ripple::ConnectAttempt::onRead
void onRead(error_code ec)
Definition: ConnectAttempt.cpp:227
ripple::ConnectAttempt::setTimer
void setTimer()
Definition: ConnectAttempt.cpp:112
ripple::ConnectAttempt::slot_
std::shared_ptr< PeerFinder::Slot > slot_
Definition: ConnectAttempt.h:62
ripple::ConnectAttempt::makeRequest
static request_type makeRequest(bool crawl, bool compressionEnabled)
Definition: ConnectAttempt.cpp:267
ripple::ConnectAttempt::id_
const std::uint32_t id_
Definition: ConnectAttempt.h:50
ripple::ConnectAttempt::socket_
socket_type & socket_
Definition: ConnectAttempt.h:58
ripple::ConnectAttempt::error_code
boost::system::error_code error_code
Definition: ConnectAttempt.h:34
std::enable_shared_from_this
ripple::ConnectAttempt::onShutdown
void onShutdown(error_code ec)
Definition: ConnectAttempt.cpp:250
ripple::ConnectAttempt::middle_type
boost::beast::tcp_stream middle_type
Definition: ConnectAttempt.h:45
ripple::ConnectAttempt::usage_
Resource::Consumer usage_
Definition: ConnectAttempt.h:54
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
std::uint32_t
ripple::ConnectAttempt::stop
void stop() override
Definition: ConnectAttempt.cpp:61
ripple::ConnectAttempt
Manages an outbound connection attempt.
Definition: ConnectAttempt.h:29
ripple::ConnectAttempt::endpoint_type
boost::asio::ip::tcp::endpoint endpoint_type
Definition: ConnectAttempt.h:36
ripple::ConnectAttempt::app_
Application & app_
Definition: ConnectAttempt.h:49
ripple::ConnectAttempt::fail
void fail(std::string const &reason)
Definition: ConnectAttempt.cpp:98
std::istringstream::fail
T fail(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ConnectAttempt::stream_ptr_
std::unique_ptr< stream_type > stream_ptr_
Definition: ConnectAttempt.h:57
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition: Consumer.h:33
ripple::ConnectAttempt::run
void run()
Definition: ConnectAttempt.cpp:75
ripple::ConnectAttempt::timer_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
Definition: ConnectAttempt.h:56
ripple::ConnectAttempt::onConnect
void onConnect(error_code ec)
Definition: ConnectAttempt.cpp:153
beast::WrappedSink
Wraps a Journal::Sink to prefix its output with a string.
Definition: WrappedSink.h:33
ripple::ConnectAttempt::stream_
stream_type & stream_
Definition: ConnectAttempt.h:59
ripple::ConnectAttempt::req_
request_type req_
Definition: ConnectAttempt.h:63
ripple::ConnectAttempt::close
void close()
Definition: ConnectAttempt.cpp:85
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
beast::IPAddressConversion::to_asio_endpoint
static boost::asio::ip::tcp::endpoint to_asio_endpoint(IP::Endpoint const &address)
Definition: IPAddressConversion.h:64
ripple::OverlayImpl
Definition: OverlayImpl.h:57
ripple::ConnectAttempt::ConnectAttempt
ConnectAttempt(Application &app, boost::asio::io_service &io_service, endpoint_type const &remote_endpoint, Resource::Consumer usage, shared_context const &context, std::uint32_t id, std::shared_ptr< PeerFinder::Slot > const &slot, beast::Journal journal, OverlayImpl &overlay)
Definition: ConnectAttempt.cpp:28
ripple::ConnectAttempt::~ConnectAttempt
~ConnectAttempt()
Definition: ConnectAttempt.cpp:52
ripple::OverlayImpl::Child
Definition: OverlayImpl.h:60
std::unique_ptr< stream_type >
ripple::ConnectAttempt::parse_endpoint
static boost::asio::ip::tcp::endpoint parse_endpoint(std::string const &s, boost::system::error_code &ec)
Definition: ConnectAttempt.h:103
ripple::ConnectAttempt::read_buf_
boost::beast::multi_buffer read_buf_
Definition: ConnectAttempt.h:60
ripple::ConnectAttempt::onTimer
void onTimer(error_code ec)
Definition: ConnectAttempt.cpp:136
ripple::ConnectAttempt::onWrite
void onWrite(error_code ec)
Definition: ConnectAttempt.cpp:212