Kagome
Polkadot Runtime Engine in C++17
session_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <boost/system/error_code.hpp>
9 
10 namespace kagome::metrics {
11 
13  : strand_(boost::asio::make_strand(context)),
14  config_{config},
15  stream_(boost::asio::ip::tcp::socket(strand_)),
16  logger_{log::createLogger("OpenMetricsSession", "metrics")} {}
17 
19  asyncRead();
20  }
21 
23  boost::system::error_code ec;
24  stream_.socket().shutdown(Socket::shutdown_both, ec);
25  boost::ignore_unused(ec);
26  }
27 
29  processRequest(req, shared_from_this());
30  }
31 
33  parser_ = std::make_unique<Parser>();
34  parser_->body_limit(config_.max_request_size);
35  stream_.expires_after(config_.operation_timeout);
36 
37  boost::beast::http::async_read(
38  stream_,
39  buffer_,
40  parser_->get(),
41  boost::beast::bind_front_handler(&SessionImpl::onRead,
42  shared_from_this()));
43  }
44 
46  auto m = std::make_shared<Response>(std::forward<Response>(message));
47 
48  res_ = m;
49 
50  // write response
51  boost::beast::http::async_write(
52  stream_,
53  *m,
54  boost::beast::bind_front_handler(
55  &SessionImpl::onWrite, shared_from_this(), m->need_eof()));
56  }
57 
58  void SessionImpl::respond(Response response) {
59  return asyncWrite(response);
60  }
61 
62  void SessionImpl::onRead(boost::system::error_code ec, std::size_t) {
63  if (ec) {
64  if (HttpError::end_of_stream != ec) {
65  reportError(ec, "unknown error occurred");
66  }
67 
68  stop();
69  }
70 
71  handleRequest(parser_->release());
72  }
73 
74  void SessionImpl::onWrite(bool close,
75  boost::system::error_code ec,
76  std::size_t) {
77  if (ec) {
78  reportError(ec, "failed to write message");
79  return stop();
80  }
81 
82  if (close) {
83  return stop();
84  }
85 
86  res_ = nullptr;
87 
88  // read next request
89  asyncRead();
90  }
91 
92  void SessionImpl::reportError(boost::system::error_code ec,
93  std::string_view message) {
94  logger_->error("error occured: {}, code: {}, message: {}",
95  message,
96  ec.value(),
97  ec.message());
98  }
99 
100 } // namespace kagome::metrics
SessionImpl(Context &context, Configuration config)
constructor
void processRequest(Request request, std::shared_ptr< Session > session)
process request message
Definition: session.hpp:62
boost::beast::flat_buffer buffer_
read buffer
void reportError(boost::system::error_code ec, std::string_view message)
reports error code and message
void asyncRead()
asynchronously read http message
void start() override
starts session
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Strand to ensure the connection&#39;s handlers are not called concurrently.
boost::beast::tcp_stream stream_
stream
boost::asio::io_context Context
Definition: session.hpp:24
void respond(Response response) override
sends response wrapped by http message
boost::beast::http::response< Body > Response
void asyncWrite(Response message)
sends http message
std::unique_ptr< Parser > parser_
request parser type
void stop()
stops session
std::shared_ptr< void > res_
boost::beast::http::request< Body > Request
void onWrite(bool close, boost::system::error_code ec, std::size_t)
write completion callback
void onRead(boost::system::error_code ec, std::size_t size)
read completion callback
Configuration config_
session configuration
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
void handleRequest(Request &&request)
process http request, compose and execute response