Kagome
Polkadot Runtime Engine in C++17
kagome::api::WsSession Class Reference

#include <ws_session.hpp>

Inheritance diagram for kagome::api::WsSession:
Collaboration diagram for kagome::api::WsSession:

Classes

struct  Configuration
 

Public Member Functions

 ~WsSession () override=default
 
 WsSession (Context &context, Configuration config, SessionId id)
 constructor More...
 
Socketsocket () override
 
void start () override
 starts session More...
 
Session::SessionId id () const override
 method to get id of the session More...
 
SessionType type () const override
 method to get type of the session More...
 
void respond (std::string_view response) override
 sends response wrapped by websocket frame More...
 
void reject ()
 Closes the incoming connection with "try again later" response. More...
 
void connectOnWsSessionCloseHandler (OnWsSessionCloseHandler &&handler)
 connects on websocket close callback. Used to maintain the maximum number of simultaneous sessions More...
 
- Public Member Functions inherited from kagome::api::Session
virtual ~Session ()=default
 
void connectOnRequest (std::function< OnRequestSignature > callback)
 connects on request callback More...
 
void connectOnCloseHandler (OnCloseHandler &&handler)
 connects on close callback More...
 
void processRequest (std::string_view request, std::shared_ptr< Session > session)
 process request message More...
 
void notifyOnClose (SessionId id, SessionType type)
 makes on close notification to listener More...
 

Private Types

using WsError = boost::beast::websocket::error
 
using OnWsSessionCloseHandler = std::function< void()>
 

Private Member Functions

void stop ()
 stops session More...
 
void stop (boost::beast::websocket::close_code code)
 stops session specifying the reason More...
 
void handleRequest (std::string_view data)
 process received websocket frame, compose and execute response More...
 
void asyncRead ()
 asynchronously read More...
 
void asyncWrite ()
 asynchronously write More...
 
void onRun ()
 connected callback More...
 
void onAccept (boost::system::error_code ec)
 handshake completion callback More...
 
void onRead (boost::system::error_code ec, std::size_t size)
 read completion callback More...
 
void onWrite (boost::system::error_code ec, std::size_t bytes_transferred)
 write completion callback More...
 
void reportError (boost::system::error_code ec, std::string_view message)
 reports error code and message More...
 

Private Attributes

boost::asio::strand< boost::asio::io_context::executor_type > strand_
 Strand to ensure the connection's handlers are not called concurrently. More...
 
boost::asio::ip::tcp::socket socket_
 Socket for the connection. More...
 
Configuration config_
 session configuration More...
 
boost::beast::websocket::stream< boost::asio::ip::tcp::socket & > stream_
 
boost::beast::flat_buffer rbuffer_
 read buffer More...
 
boost::beast::flat_buffer wbuffer_
 write buffer More...
 
std::mutex cs_
 
std::queue< std::string > pending_responses_
 
std::atomic_bool writing_in_progress_ = false
 
std::atomic_bool stopped_ = false
 
SessionId const id_
 
OnWsSessionCloseHandler on_ws_close_
 
log::Logger logger_ = log::createLogger("WsSession", "rpc_transport")
 

Additional Inherited Members

- Public Types inherited from kagome::api::Session
using Context = RpcContext
 
using Socket = boost::asio::ip::tcp::socket
 
using ErrorCode = boost::system::error_code
 
using Streambuf = boost::asio::streambuf
 
using Timer = boost::asio::steady_timer
 
using Connection = boost::signals2::connection
 
using Duration = Timer::duration
 
using SessionId = uint64_t
 
using OnCloseHandler = std::function< void(SessionId, SessionType)>
 Event handlers. More...
 

Detailed Description

Definition at line 24 of file ws_session.hpp.

Member Typedef Documentation

using kagome::api::WsSession::OnWsSessionCloseHandler = std::function<void()>
private

Definition at line 27 of file ws_session.hpp.

using kagome::api::WsSession::WsError = boost::beast::websocket::error
private

Definition at line 26 of file ws_session.hpp.

Constructor & Destructor Documentation

kagome::api::WsSession::~WsSession ( )
overridedefault
kagome::api::WsSession::WsSession ( Context context,
Configuration  config,
SessionId  id 
)

constructor

Parameters
socketsocket instance
configsession configuration
idsession id

Definition at line 15 of file ws_session.cpp.

Member Function Documentation

void kagome::api::WsSession::asyncRead ( )
private

asynchronously read

Definition at line 69 of file ws_session.cpp.

void kagome::api::WsSession::asyncWrite ( )
private

asynchronously write

Definition at line 88 of file ws_session.cpp.

void kagome::api::WsSession::connectOnWsSessionCloseHandler ( WsSession::OnWsSessionCloseHandler &&  handler)

connects on websocket close callback. Used to maintain the maximum number of simultaneous sessions

Parameters
handleron close event handler

Definition at line 59 of file ws_session.cpp.

void kagome::api::WsSession::handleRequest ( std::string_view  data)
private

process received websocket frame, compose and execute response

Template Parameters
Bodyrequest body type
Parameters
requestrequest

Definition at line 64 of file ws_session.cpp.

kagome::api::Session::SessionId kagome::api::WsSession::id ( ) const
overridevirtual

method to get id of the session

Returns
id of the session

Implements kagome::api::Session.

Definition at line 75 of file ws_session.cpp.

void kagome::api::WsSession::onAccept ( boost::system::error_code  ec)
private

handshake completion callback

Definition at line 125 of file ws_session.cpp.

void kagome::api::WsSession::onRead ( boost::system::error_code  ec,
std::size_t  size 
)
private

read completion callback

Definition at line 137 of file ws_session.cpp.

void kagome::api::WsSession::onRun ( )
private

connected callback

Definition at line 108 of file ws_session.cpp.

void kagome::api::WsSession::onWrite ( boost::system::error_code  ec,
std::size_t  bytes_transferred 
)
private

write completion callback

Definition at line 155 of file ws_session.cpp.

void kagome::api::WsSession::reject ( )

Closes the incoming connection with "try again later" response.

Definition at line 29 of file ws_session.cpp.

void kagome::api::WsSession::reportError ( boost::system::error_code  ec,
std::string_view  message 
)
private

reports error code and message

Parameters
ecerror code
messageerror message

Definition at line 188 of file ws_session.cpp.

void kagome::api::WsSession::respond ( std::string_view  response)
overridevirtual

sends response wrapped by websocket frame

Parameters
responsemessage to send

Implements kagome::api::Session.

Definition at line 79 of file ws_session.cpp.

Socket& kagome::api::WsSession::socket ( )
inlineoverridevirtual

Implements kagome::api::Session.

Definition at line 48 of file ws_session.hpp.

void kagome::api::WsSession::start ( )
overridevirtual

starts session

Implements kagome::api::Session.

Definition at line 22 of file ws_session.cpp.

void kagome::api::WsSession::stop ( )
private

stops session

Definition at line 34 of file ws_session.cpp.

void kagome::api::WsSession::stop ( boost::beast::websocket::close_code  code)
private

stops session specifying the reason

Parameters
codefor close reason

Definition at line 39 of file ws_session.cpp.

SessionType kagome::api::WsSession::type ( ) const
inlineoverridevirtual

method to get type of the session

Returns
type of the session

Implements kagome::api::Session.

Definition at line 67 of file ws_session.hpp.

Member Data Documentation

Configuration kagome::api::WsSession::config_
private

session configuration

Definition at line 151 of file ws_session.hpp.

std::mutex kagome::api::WsSession::cs_
private

Definition at line 156 of file ws_session.hpp.

SessionId const kagome::api::WsSession::id_
private

Definition at line 162 of file ws_session.hpp.

log::Logger kagome::api::WsSession::logger_ = log::createLogger("WsSession", "rpc_transport")
private

Definition at line 164 of file ws_session.hpp.

OnWsSessionCloseHandler kagome::api::WsSession::on_ws_close_
private

Definition at line 163 of file ws_session.hpp.

std::queue<std::string> kagome::api::WsSession::pending_responses_
private

Definition at line 157 of file ws_session.hpp.

boost::beast::flat_buffer kagome::api::WsSession::rbuffer_
private

read buffer

Definition at line 153 of file ws_session.hpp.

boost::asio::ip::tcp::socket kagome::api::WsSession::socket_
private

Socket for the connection.

Definition at line 149 of file ws_session.hpp.

std::atomic_bool kagome::api::WsSession::stopped_ = false
private

Definition at line 160 of file ws_session.hpp.

boost::asio::strand<boost::asio::io_context::executor_type> kagome::api::WsSession::strand_
private

Strand to ensure the connection's handlers are not called concurrently.

Definition at line 146 of file ws_session.hpp.

boost::beast::websocket::stream<boost::asio::ip::tcp::socket &> kagome::api::WsSession::stream_
private

Definition at line 152 of file ws_session.hpp.

boost::beast::flat_buffer kagome::api::WsSession::wbuffer_
private

write buffer

Definition at line 154 of file ws_session.hpp.

std::atomic_bool kagome::api::WsSession::writing_in_progress_ = false
private

Definition at line 159 of file ws_session.hpp.


The documentation for this class was generated from the following files: