6 #ifndef KAGOME_CORE_API_TRANSPORT_IMPL_WS_SESSION_HPP 7 #define KAGOME_CORE_API_TRANSPORT_IMPL_WS_SESSION_HPP 14 #include <boost/asio/strand.hpp> 15 #include <boost/beast/core/multi_buffer.hpp> 16 #include <boost/beast/core/tcp_stream.hpp> 17 #include <boost/beast/websocket.hpp> 25 public std::enable_shared_from_this<WsSession> {
26 using WsError = boost::beast::websocket::error;
55 void start()
override;
75 void respond(std::string_view response)
override;
99 void stop(boost::beast::websocket::close_code code);
126 void onAccept(boost::system::error_code ec);
131 void onRead(boost::system::error_code ec, std::size_t size);
136 void onWrite(boost::system::error_code ec, std::size_t bytes_transferred);
143 void reportError(boost::system::error_code ec, std::string_view message);
146 boost::asio::strand<boost::asio::io_context::executor_type>
strand_;
152 boost::beast::websocket::stream<boost::asio::ip::tcp::socket &>
stream_;
169 #endif // KAGOME_CORE_API_TRANSPORT_BEAST_HTTP_SESSION_HPP 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
Configuration config_
session configuration
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_
Socket & socket() override
boost::asio::ip::tcp::socket Socket
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.
~WsSession() override=default
void connectOnWsSessionCloseHandler(OnWsSessionCloseHandler &&handler)
connects on websocket close callback. Used to maintain the maximum number of simultaneous sessions ...
std::shared_ptr< soralog::Logger > Logger
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
static constexpr Duration kDefaultTimeout
boost::beast::websocket::error WsError
static constexpr size_t kDefaultRequestSize
boost::beast::flat_buffer rbuffer_
read buffer
void asyncWrite()
asynchronously write
Duration operation_timeout
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Strand to ensure the connection's handlers are not called concurrently.
Logger createLogger(const std::string &tag)
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 reject()
Closes the incoming connection with "try again later" response.