JayBeams  0.1
Another project to have fun coding.
connection.hpp
Go to the documentation of this file.
1 #ifndef jb_ehs_connection_hpp
2 #define jb_ehs_connection_hpp
3 
5 
6 #include <beast/core/flat_buffer.hpp>
7 #include <boost/asio/ip/tcp.hpp>
8 #include <boost/asio/strand.hpp>
9 #include <atomic>
10 
11 namespace jb {
12 namespace ehs {
13 
14 /**
15  * Handle one connection to the control server.
16  */
17 class connection : public std::enable_shared_from_this<connection> {
18 public:
19  /// The type of socket used for the connection
20  using socket_type = boost::asio::ip::tcp::socket;
21 
22  /**
23  * Create a new connection.
24  *
25  * @param sock the underlying socket for this connection.
26  * @param dispatcher the handler for the connections.
27  */
28  connection(
29  socket_type&& sock, std::shared_ptr<request_dispatcher> dispatcher);
30 
31  /// Destructor
32  ~connection();
33 
34  /// Asynchronously read a HTTP request for this connection.
35  void run();
36 
37  //@{
38  /**
39  * Control copy and assignment.
40  */
41  connection(connection&&) = default;
42  connection(connection const&) = default;
43  connection& operator=(connection&&) = delete;
44  connection& operator=(connection const&) = delete;
45  //@}
46 
47 private:
48  /**
49  * Handle a completed HTTP request read.
50  *
51  * Once a HTTP request has been received it needs to be parsed and a
52  * response sent back. This function is called by the Beast
53  * framework when the read completes.
54  *
55  * @param ec the error code
56  */
57  void on_read(boost::system::error_code const& ec);
58 
59  /**
60  * Handle a completed response write.
61  *
62  * @param ec indicate if writing the response resulted in an error.
63  */
64  void on_write(boost::system::error_code const& ec);
65 
66 private:
68  std::shared_ptr<request_dispatcher> dispatcher_;
69  boost::asio::io_service::strand strand_;
70  int id_;
71  beast::flat_buffer sb_;
73 
74  static std::atomic<int> idgen;
75 };
76 
77 } // namespace ehs
78 } // namespace jb
79 
80 #endif // jb_ehs_connection_hpp
Handle one connection to the control server.
Definition: connection.hpp:17
boost::asio::io_service::strand strand_
Definition: connection.hpp:69
socket_type sock_
Definition: connection.hpp:67
beast::http::request< beast::http::string_body > request_type
The request type used for JayBeams Embedded HTTP Servers.
Definition: base_types.hpp:17
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::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
connection & operator=(connection &&)=delete
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7