WsSession.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
11 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__WS_SESSION
12 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__WS_SESSION
13 
19 
22 #include <Balau/Util/DateTime.hpp>
23 
24 // Avoid false positive (due to std::make_shared).
25 #pragma clang diagnostic push
26 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
27 
28 namespace Balau::Network::Http {
29 
35 class WsSession final : public std::enable_shared_from_this<WsSession> {
46  public: WsSession(std::shared_ptr<HttpServerConfiguration> serverConfiguration_,
47  TCP::socket && socket_,
48  std::string path_)
49  : serverConfiguration(std::move(serverConfiguration_))
50  , strand(socket_.get_executor())
51  , socket(std::move(socket_))
52  , path(std::move(path_)) {}
53 
60  return *serverConfiguration;
61  }
62 
63  public: template <typename Body, typename AllocatorT>
64  void doAccept(HTTP::request<Body, HTTP::basic_fields<AllocatorT>> req) {
65 
66 
67  // TODO
68 // ws.control_callback(
69 // std::bind(
70 // &WsSession::onControl,
71 // this,
72 // std::placeholders::_1,
73 // std::placeholders::_2));
74 
75  socket.async_accept(
76  req,
77  boost::asio::bind_executor(
78  strand,
79  std::bind(
81  shared_from_this(),
82  std::placeholders::_1)));
83 
84  // TODO
85  }
86 
87  public: void run() {
88  socket.async_accept(
89  boost::asio::bind_executor(
90  strand, std::bind(&WsSession::onAccept, shared_from_this(), std::placeholders::_1)
91  )
92  );
93  }
94 
95  public: void onControl(WsFrame frameType, boost::beast::string_view payload) {
96  boost::ignore_unused(payload);
97 
98  switch (frameType) {
99  case WsFrame::close: {
100  serverConfiguration->wsHandler->handleClose(*this, path); // TODO
101  break;
102  }
103 
104  case WsFrame::ping: {
105  serverConfiguration->wsHandler->handlePing(*this, path); // TODO
106  break;
107  }
108 
109  case WsFrame::pong: {
110  serverConfiguration->wsHandler->handlePong(*this, path); // TODO
111  break;
112  }
113  }
114  }
115 
119  public: void onAccept(boost::system::error_code ec) {
120  checkError(ec);
121  doRead();
122  }
123 
124  public: void doRead() {
125  socket.async_read(
126  buffer,
127  boost::asio::bind_executor(
128  strand,
129  std::bind(
131  shared_from_this(),
132  std::placeholders::_1,
133  std::placeholders::_2)));
134  }
135 
139  public: void onRead(boost::system::error_code ec, std::size_t bytes_transferred) {
140  boost::ignore_unused(bytes_transferred);
141 
142  if (ec == WS::error::closed) {
143  return;
144  }
145 
146  checkError(ec);
147 
148  // Echo the message
149  socket.text(socket.got_text());
150 
151  socket.async_write(
152  buffer.data()
153  , boost::asio::bind_executor(
154  strand, std::bind(&WsSession::onWrite, shared_from_this(), std::placeholders::_1, std::placeholders::_2)
155  )
156  );
157  }
158 
162  public: void onWrite(boost::system::error_code ec, std::size_t bytes_transferred) {
163  boost::ignore_unused(bytes_transferred);
164 
165  checkError(ec);
166  buffer.consume(buffer.size());
167  doRead();
168  }
169 
171 
172  private: void checkError(const boost::system::error_code & errorCode) {
173  if (errorCode) {
175  }
176  }
177 
178  private: std::shared_ptr<HttpServerConfiguration> serverConfiguration;
179  private: boost::asio::strand<boost::asio::io_context::executor_type> strand;
180  private: WS::stream<TCP::socket> socket;
181  private: const std::string path;
182  private: boost::beast::multi_buffer buffer;
183 };
184 
185 } // namespace Balau::Network::Http
186 
187 #pragma clang diagnostic pop
188 
189 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__WS_SESSION
Components and utilities working on HTTP data transmission.
Definition: HttpClient.hpp:26
Abstract base class of WebSocket web application handlers.
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
Base class of network exceptions.
Definition: NetworkExceptions.hpp:28
Shared state between HTTP sessions.
Definition: HttpServerConfiguration.hpp:44
STL namespace.
HttpServerConfiguration & configuration() const
Get the shared state of the http server.
Definition: WsSession.hpp:59
void onRead(boost::system::error_code ec, std::size_t bytes_transferred)
Definition: WsSession.hpp:139
const std::string message
The message.
Definition: BalauException.hpp:59
WsSession(std::shared_ptr< HttpServerConfiguration > serverConfiguration_, TCP::socket &&socket_, std::string path_)
Create a WebSocket session object with the supplied data.
Definition: WsSession.hpp:46
WS::frame_type WsFrame
The Boost HTTP namespace.
Definition: NetworkTypes.hpp:358
Date and time utilities.
Shared state between HTTP sessions.
void onAccept(boost::system::error_code ec)
Definition: WsSession.hpp:119
void onWrite(boost::system::error_code ec, std::size_t bytes_transferred)
Definition: WsSession.hpp:162
Manages the handling of WebSocket messages in a WebSocket connection.
Definition: WsSession.hpp:35