Kagome
Polkadot Runtime Engine in C++17
session.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_API_TRANSPORT_SESSION_HPP
7 #define KAGOME_CORE_API_TRANSPORT_SESSION_HPP
8 
9 #include <boost/asio/buffer.hpp>
10 #include <boost/asio/ip/tcp.hpp>
11 #include <boost/asio/read_until.hpp>
12 #include <boost/asio/streambuf.hpp>
13 #include <boost/asio/write.hpp>
14 #include <boost/signals2/signal.hpp>
15 
17 
18 namespace kagome::api {
19  enum struct SessionType {
20  kHttp = 1,
21  kWs,
22  };
23 
27  class Session {
28  template <class T>
29  using Signal = boost::signals2::signal<T>;
30 
31  using OnRequestSignature = void(std::string_view,
32  std::shared_ptr<Session> session);
34 
35  public:
37  using Socket = boost::asio::ip::tcp::socket;
38  using ErrorCode = boost::system::error_code;
39  using Streambuf = boost::asio::streambuf;
40  using Timer = boost::asio::steady_timer;
41  using Connection = boost::signals2::connection;
42  using Duration = Timer::duration;
43  using SessionId = uint64_t;
44 
46  using OnCloseHandler = std::function<void(SessionId, SessionType)>;
47 
48  virtual ~Session() = default;
49 
53  virtual void start() = 0;
54 
55  virtual Socket &socket() = 0;
56 
61  void connectOnRequest(std::function<OnRequestSignature> callback) {
62  on_request_ = std::move(callback);
63  }
64 
70  on_close_ = std::move(handler);
71  }
72 
77  void processRequest(std::string_view request,
78  std::shared_ptr<Session> session) {
79  on_request_(request, std::move(session));
80  }
81 
86  virtual void respond(std::string_view message) = 0;
87 
94  if (nullptr != on_close_) on_close_(id, type);
95  }
96 
101  virtual SessionId id() const = 0;
102 
107  virtual SessionType type() const = 0;
108 
109  private:
110  std::function<OnRequestSignature> on_request_;
112  };
113 
114 } // namespace kagome::api
115 
116 #endif // KAGOME_CORE_API_TRANSPORT_SESSION_HPP
std::function< OnRequestSignature > on_request_
on request callback
Definition: session.hpp:110
Timer::duration Duration
Definition: session.hpp:42
boost::asio::steady_timer Timer
Definition: session.hpp:40
Signal< OnRequestSignature > OnRequest
Definition: session.hpp:33
void connectOnCloseHandler(OnCloseHandler &&handler)
connects on close callback
Definition: session.hpp:69
boost::asio::ip::tcp::socket Socket
Definition: session.hpp:37
boost::signals2::signal< T > Signal
Definition: session.hpp:29
void processRequest(std::string_view request, std::shared_ptr< Session > session)
process request message
Definition: session.hpp:77
rpc session
Definition: session.hpp:27
void(std::string_view, std::shared_ptr< Session > session) OnRequestSignature
Definition: session.hpp:32
std::function< void(SessionId, SessionType)> OnCloseHandler
Event handlers.
Definition: session.hpp:46
boost::signals2::connection Connection
Definition: session.hpp:41
boost::system::error_code ErrorCode
Definition: session.hpp:38
uint64_t SessionId
Definition: session.hpp:43
boost::asio::streambuf Streambuf
Definition: session.hpp:39
OnCloseHandler on_close_
Definition: session.hpp:111
void connectOnRequest(std::function< OnRequestSignature > callback)
connects on request callback
Definition: session.hpp:61
void notifyOnClose(SessionId id, SessionType type)
makes on close notification to listener
Definition: session.hpp:93