11 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__WS_SESSION 12 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__WS_SESSION 25 #pragma clang diagnostic push 26 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" 35 class WsSession final :
public std::enable_shared_from_this<WsSession> {
46 public:
WsSession(std::shared_ptr<HttpServerConfiguration> serverConfiguration_,
47 TCP::socket && socket_,
49 : serverConfiguration(
std::move(serverConfiguration_))
50 , strand(socket_.get_executor())
51 , socket(
std::move(socket_))
52 , path(
std::move(path_)) {}
60 return *serverConfiguration;
63 public:
template <
typename Body,
typename AllocatorT>
64 void doAccept(HTTP::request<Body, HTTP::basic_fields<AllocatorT>> req) {
77 boost::asio::bind_executor(
82 std::placeholders::_1)));
89 boost::asio::bind_executor(
95 public:
void onControl(
WsFrame frameType, boost::beast::string_view payload) {
96 boost::ignore_unused(payload);
99 case WsFrame::close: {
100 serverConfiguration->wsHandler->handleClose(*
this, path);
104 case WsFrame::ping: {
105 serverConfiguration->wsHandler->handlePing(*
this, path);
109 case WsFrame::pong: {
110 serverConfiguration->wsHandler->handlePong(*
this, path);
119 public:
void onAccept(boost::system::error_code ec) {
124 public:
void doRead() {
127 boost::asio::bind_executor(
132 std::placeholders::_1,
133 std::placeholders::_2)));
139 public:
void onRead(boost::system::error_code ec, std::size_t bytes_transferred) {
140 boost::ignore_unused(bytes_transferred);
142 if (ec == WS::error::closed) {
149 socket.text(socket.got_text());
153 , boost::asio::bind_executor(
154 strand, std::bind(&
WsSession::onWrite, shared_from_this(), std::placeholders::_1, std::placeholders::_2)
162 public:
void onWrite(boost::system::error_code ec, std::size_t bytes_transferred) {
163 boost::ignore_unused(bytes_transferred);
166 buffer.consume(buffer.size());
172 private:
void checkError(
const boost::system::error_code & errorCode) {
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;
187 #pragma clang diagnostic pop 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
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
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