src/api/transport/impl/ws/ws_session.hpp
Namespaces
Classes
Source code
#ifndef SUPERGENIUS_SRC_API_TRANSPORT_IMPL_WS_SESSION_HPP
#define SUPERGENIUS_SRC_API_TRANSPORT_IMPL_WS_SESSION_HPP
#include <boost/asio/strand.hpp>
#include <boost/beast/core/tcp_stream.hpp>
#include <boost/beast/websocket.hpp>
#include <chrono>
#include <cstdlib>
#include <memory>
#include "api/transport/session.hpp"
#include "base/logger.hpp"
namespace sgns::api {
class WsSession : public Session,
public std::enable_shared_from_this<WsSession> {
using WsError = boost::beast::websocket::error;
public:
struct Configuration {
static constexpr size_t kDefaultRequestSize = 10000u;
static constexpr Duration kDefaultTimeout = std::chrono::seconds(30);
size_t max_request_size{kDefaultRequestSize};
Duration operation_timeout{kDefaultTimeout};
};
~WsSession() override = default;
WsSession(Context &context, Configuration config, SessionId id);
Socket &socket() override {
return socket_;
}
void start() override;
Session::SessionId id() const override;
SessionType type() const override {
return SessionType::kWs;
}
void respond(std::string_view response) override;
private:
void stop();
void handleRequest(std::string_view data);
void asyncRead();
void asyncWrite();
void onRun();
void onAccept(boost::system::error_code ec);
void onRead(boost::system::error_code ec, std::size_t size);
void onWrite(boost::system::error_code ec, std::size_t bytes_transferred);
void reportError(boost::system::error_code ec, std::string_view message);
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
boost::asio::ip::tcp::socket socket_;
Configuration config_;
boost::beast::websocket::stream<boost::asio::ip::tcp::socket &>
stream_;
boost::beast::flat_buffer rbuffer_;
boost::beast::flat_buffer wbuffer_;
SessionId const id_;
base::Logger logger_ =
base::createLogger("websocket session");
};
} // namespace sgns::api
#endif // SUPERGENIUS_SRC_API_TRANSPORT_BEAST_HTTP_SESSION_HPP
Updated on 2026-03-04 at 13:10:44 -0800