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 std::enable_shared_from_this<ConnectAttempt>
31 {
32 private:
33  using error_code = boost::system::error_code;
34 
35  using endpoint_type = boost::asio::ip::tcp::endpoint;
36 
37  using request_type =
38  boost::beast::http::request<boost::beast::http::empty_body>;
39 
40  using response_type =
41  boost::beast::http::response<boost::beast::http::dynamic_body>;
42 
43  using socket_type = boost::asio::ip::tcp::socket;
44  using middle_type = boost::beast::tcp_stream;
45  using stream_type = boost::beast::ssl_stream<middle_type>;
47 
54  boost::asio::io_service::strand strand_;
55  boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
59  boost::beast::multi_buffer read_buf_;
63 
64 public:
66  Application& app,
67  boost::asio::io_service& io_service,
68  endpoint_type const& remote_endpoint,
69  Resource::Consumer usage,
70  shared_context const& context,
71  std::uint32_t id,
73  beast::Journal journal,
74  OverlayImpl& overlay);
75 
77 
78  void
79  stop() override;
80 
81  void
82  run();
83 
84 private:
85  void
86  close();
87  void
88  fail(std::string const& reason);
89  void
90  fail(std::string const& name, error_code ec);
91  void
92  setTimer();
93  void
94  cancelTimer();
95  void
96  onTimer(error_code ec);
97  void
99  void
101  void
102  onWrite(error_code ec);
103  void
104  onRead(error_code ec);
105  void
107  void
108  processResponse();
109 
110  template <class = void>
111  static boost::asio::ip::tcp::endpoint
112  parse_endpoint(std::string const& s, boost::system::error_code& ec)
113  {
115  std::istringstream is(s);
116  is >> bep;
117  if (is.fail())
118  {
119  ec = boost::system::errc::make_error_code(
120  boost::system::errc::invalid_argument);
121  return boost::asio::ip::tcp::endpoint{};
122  }
123 
125  }
126 };
127 
128 } // namespace ripple
129 
130 #endif
ripple::ConnectAttempt::onHandshake
void onHandshake(error_code ec)
Definition: ConnectAttempt.cpp:182
ripple::Application
Definition: Application.h:115
ripple::ConnectAttempt::cancelTimer
void cancelTimer()
Definition: ConnectAttempt.cpp:133
ripple::ConnectAttempt::remote_endpoint_
endpoint_type remote_endpoint_
Definition: ConnectAttempt.h:52
ripple::ConnectAttempt::sink_
beast::WrappedSink sink_
Definition: ConnectAttempt.h:50
std::string
STL class.
std::shared_ptr< boost::asio::ssl::context >
ripple::ConnectAttempt::strand_
boost::asio::io_service::strand strand_
Definition: ConnectAttempt.h:54
ripple::ConnectAttempt::response_type
boost::beast::http::response< boost::beast::http::dynamic_body > response_type
Definition: ConnectAttempt.h:41
ripple::ConnectAttempt::journal_
const beast::Journal journal_
Definition: ConnectAttempt.h:51
std::istringstream
STL class.
ripple::ConnectAttempt::request_type
boost::beast::http::request< boost::beast::http::empty_body > request_type
Definition: ConnectAttempt.h:38
ripple::ConnectAttempt::processResponse
void processResponse()
Definition: ConnectAttempt.cpp:289
ripple::ConnectAttempt::socket_type
boost::asio::ip::tcp::socket socket_type
Definition: ConnectAttempt.h:43
ripple::ConnectAttempt::response_
response_type response_
Definition: ConnectAttempt.h:60
ripple::ConnectAttempt::onRead
void onRead(error_code ec)
Definition: ConnectAttempt.cpp:250
ripple::ConnectAttempt::setTimer
void setTimer()
Definition: ConnectAttempt.cpp:118
ripple::ConnectAttempt::slot_
std::shared_ptr< PeerFinder::Slot > slot_
Definition: ConnectAttempt.h:61
ripple::ConnectAttempt::id_
const std::uint32_t id_
Definition: ConnectAttempt.h:49
ripple::ConnectAttempt::socket_
socket_type & socket_
Definition: ConnectAttempt.h:57
ripple::ConnectAttempt::error_code
boost::system::error_code error_code
Definition: ConnectAttempt.h:33
std::enable_shared_from_this
ripple::ConnectAttempt::onShutdown
void onShutdown(error_code ec)
Definition: ConnectAttempt.cpp:273
ripple::ConnectAttempt::middle_type
boost::beast::tcp_stream middle_type
Definition: ConnectAttempt.h:44
ripple::ConnectAttempt::usage_
Resource::Consumer usage_
Definition: ConnectAttempt.h:53
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::ConnectAttempt::stop
void stop() override
Definition: ConnectAttempt.cpp:65
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:35
ripple::ConnectAttempt::app_
Application & app_
Definition: ConnectAttempt.h:48
ripple::ConnectAttempt::fail
void fail(std::string const &reason)
Definition: ConnectAttempt.cpp:104
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_type
boost::beast::ssl_stream< middle_type > stream_type
Definition: ConnectAttempt.h:45
ripple::ConnectAttempt::stream_ptr_
std::unique_ptr< stream_type > stream_ptr_
Definition: ConnectAttempt.h:56
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition: Consumer.h:34
ripple::ConnectAttempt::run
void run()
Definition: ConnectAttempt.cpp:78
ripple::ConnectAttempt::timer_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
Definition: ConnectAttempt.h:55
ripple::ConnectAttempt::onConnect
void onConnect(error_code ec)
Definition: ConnectAttempt.cpp:156
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:58
ripple::ConnectAttempt::req_
request_type req_
Definition: ConnectAttempt.h:62
ripple::ConnectAttempt::close
void close()
Definition: ConnectAttempt.cpp:91
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
beast::IPAddressConversion::to_asio_endpoint
static boost::asio::ip::tcp::endpoint to_asio_endpoint(IP::Endpoint const &address)
Definition: IPAddressConversion.h:78
ripple::OverlayImpl
Definition: OverlayImpl.h:58
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:57
ripple::OverlayImpl::Child
Definition: OverlayImpl.h:61
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:112
ripple::ConnectAttempt::read_buf_
boost::beast::multi_buffer read_buf_
Definition: ConnectAttempt.h:59
ripple::ConnectAttempt::onTimer
void onTimer(error_code ec)
Definition: ConnectAttempt.cpp:140
ripple::ConnectAttempt::onWrite
void onWrite(error_code ec)
Definition: ConnectAttempt.cpp:230