JayBeams  0.1
Another project to have fun coding.
connection.cpp
Go to the documentation of this file.
1 #include "jb/ehs/connection.hpp"
2 
3 #include <jb/log.hpp>
4 
5 namespace jb {
6 namespace ehs {
7 
9  socket_type&& sock, std::shared_ptr<request_dispatcher> dispatcher)
10  : sock_(std::move(sock))
11  , dispatcher_(dispatcher)
12  , strand_(sock_.get_io_service())
13  , id_(++idgen) {
14  JB_LOG(info) << "#" << id_ << " created, peer=" << sock_.remote_endpoint()
15  << ", handle=" << sock_.native_handle();
16  dispatcher_->count_open_connection();
17 }
18 
20  JB_LOG(info) << "#" << id_ << " ~connection()";
21  dispatcher_->count_close_connection();
22 }
23 
25  beast::http::async_read(
26  sock_, sb_, req_,
27  strand_.wrap([self = shared_from_this()](
28  boost::system::error_code const& ec) { self->on_read(ec); }));
29 }
30 
31 void connection::on_read(boost::system::error_code const& ec) {
32  if (ec) {
33  JB_LOG(info) << "#" << id_ << " on_read: " << ec.message() << " ["
34  << ec.category().name() << "/" << ec.value() << "]";
35  dispatcher_->count_read_error();
36  return;
37  }
38  dispatcher_->count_read_ok();
39  // Prepare a response ...
40  response_type res = dispatcher_->process(req_);
41  res.set(beast::http::field::content_length, res.body.size());
42  // ... create a copy that will survive until the lambda wrapping
43  // on_write() is called ...
44  auto ptr = std::make_shared<response_type>(std::move(res));
45  // ... and send that copy back ...
46  beast::http::async_write(sock_, *ptr, strand_.wrap([
47  ptr, self = shared_from_this()
48  ](boost::system::error_code const& ec) { self->on_write(ec); }));
49 }
50 
51 void connection::on_write(boost::system::error_code const& ec) {
52  if (ec) {
53  JB_LOG(info) << "#" << id_ << " on_write: " << ec.message() << " ["
54  << ec.category().name() << "/" << ec.value() << "]";
55  dispatcher_->count_write_error();
56  }
57  // Start a new asynchronous read for the next message ...
58  run();
59 }
60 
61 // Define the generator for connection ids ...
62 std::atomic<int> connection::idgen(0);
63 
64 } // namespace ehs
65 } // namespace jb
boost::asio::io_service::strand strand_
Definition: connection.hpp:69
socket_type sock_
Definition: connection.hpp:67
STL namespace.
void on_read(boost::system::error_code const &ec)
Handle a completed HTTP request read.
Definition: connection.cpp:31
void on_write(boost::system::error_code const &ec)
Handle a completed response write.
Definition: connection.cpp:51
void run()
Asynchronously read a HTTP request for this connection.
Definition: connection.cpp:24
static std::atomic< int > idgen
Definition: connection.hpp:74
~connection()
Destructor.
Definition: connection.cpp:19
beast::http::response< beast::http::string_body > response_type
The response type used for JayBeams Embedded HTTP Servers.
Definition: base_types.hpp:20
beast::flat_buffer sb_
Definition: connection.hpp:71
boost::asio::ip::tcp::socket socket_type
The type of socket used for the connection.
Definition: connection.hpp:20
connection(socket_type &&sock, std::shared_ptr< request_dispatcher > dispatcher)
Create a new connection.
Definition: connection.cpp:8
request_type req_
Definition: connection.hpp:72
std::shared_ptr< request_dispatcher > dispatcher_
Definition: connection.hpp:68
#define JB_LOG(lvl)
Definition: log.hpp:70
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7