10 #include <boost/asio/dispatch.hpp> 11 #include <boost/config.hpp> 16 : strand_(boost::asio::make_strand(context)),
23 boost::asio::dispatch(
stream_.get_executor(),
30 stop(boost::beast::websocket::close_code::try_again_later);
36 stop(boost::beast::websocket::close_code::none);
40 bool already_stopped =
false;
41 if (
stopped_.compare_exchange_strong(already_stopped,
true)) {
42 boost::system::error_code ec;
43 stream_.close(boost::beast::websocket::close_reason(code), ec);
44 boost::ignore_unused(ec);
49 SL_TRACE(
logger_,
"Session id = {} terminated, reason = {} ",
id_, code);
52 "Session id = {} was already terminated. Doing nothing. Called " 65 SL_DEBUG(
logger_,
"Session#{} IN: {}",
id_, data);
80 SL_DEBUG(
logger_,
"Session#{} OUT: {}",
id_, response);
82 std::lock_guard lg{
cs_};
91 std::lock_guard lg{
cs_};
93 boost::asio::buffer_copy(
102 boost::beast::bind_front_handler(
110 stream_.set_option(boost::beast::websocket::stream_base::timeout::suggested(
111 boost::beast::role_type::server));
114 stream_.set_option(boost::beast::websocket::stream_base::decorator(
115 [](boost::beast::websocket::response_type &res) {
116 res.set(boost::beast::http::field::server,
117 std::string(BOOST_BEAST_VERSION_STRING)
118 +
" websocket-server-async");
122 shared_from_this()));
128 (ec == WsError::closed) ?
"connection was closed" : ec.message();
138 std::size_t bytes_transferred) {
141 (ec == WsError::closed) ?
"connection was closed" : ec.message();
148 {
static_cast<char *
>(
rbuffer_.data().data()), bytes_transferred});
150 rbuffer_.consume(bytes_transferred);
156 std::size_t bytes_transferred) {
162 wbuffer_.consume(bytes_transferred);
167 shared_from_this()));
169 std::lock_guard lg{
cs_};
171 boost::asio::buffer_copy(
180 boost::beast::bind_front_handler(
189 std::string_view message) {
191 "error occurred: {}, code: {}, message: {}",
std::queue< std::string > pending_responses_
boost::beast::websocket::stream< boost::asio::ip::tcp::socket & > stream_
std::atomic_bool writing_in_progress_
boost::beast::flat_buffer wbuffer_
write buffer
OnWsSessionCloseHandler on_ws_close_
void respond(std::string_view response) override
sends response wrapped by websocket frame
void onRead(boost::system::error_code ec, std::size_t size)
read completion callback
std::atomic_bool stopped_
Session::SessionId id() const override
method to get id of the session
SessionType type() const override
method to get type of the session
WsSession(Context &context, Configuration config, SessionId id)
constructor
boost::asio::ip::tcp::socket socket_
Socket for the connection.
void processRequest(std::string_view request, std::shared_ptr< Session > session)
process request message
void connectOnWsSessionCloseHandler(OnWsSessionCloseHandler &&handler)
connects on websocket close callback. Used to maintain the maximum number of simultaneous sessions ...
void onWrite(boost::system::error_code ec, std::size_t bytes_transferred)
write completion callback
void onAccept(boost::system::error_code ec)
handshake completion callback
void onRun()
connected callback
void asyncRead()
asynchronously read
void start() override
starts session
boost::beast::flat_buffer rbuffer_
read buffer
void asyncWrite()
asynchronously write
void handleRequest(std::string_view data)
process received websocket frame, compose and execute response
void reportError(boost::system::error_code ec, std::string_view message)
reports error code and message
std::function< void()> OnWsSessionCloseHandler
void notifyOnClose(SessionId id, SessionType type)
makes on close notification to listener
void reject()
Closes the incoming connection with "try again later" response.