CppServer  1.0.0.0
C++ Server Library
websocket_session.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_ASIO_WEBSOCKET_SESSION_H
10 #define CPPSERVER_ASIO_WEBSOCKET_SESSION_H
11 
12 #include "service.h"
13 #include "websocket.h"
14 
15 #include "system/uuid.h"
16 
17 namespace CppServer {
18 namespace Asio {
19 
20 template <class TServer, class TSession>
21 class WebSocketServer;
22 
24 
29 template <class TServer, class TSession>
30 class WebSocketSession : public std::enable_shared_from_this<WebSocketSession<TServer, TSession>>
31 {
32  template <class TSomeServer, class TSomeSession>
33  friend class WebSocketServer;
34 
35 public:
37 
41  WebSocketSession(const WebSocketSession&) = delete;
43  virtual ~WebSocketSession() = default;
44 
47 
49  const CppCommon::UUID& id() const noexcept { return _id; }
50 
52  std::shared_ptr<Service>& service() noexcept { return _server->service(); }
54  std::shared_ptr<WebSocketServer<TServer, TSession>>& server() noexcept { return _server; }
56  websocketpp::connection_hdl& connection() noexcept { return _connection; }
57 
59  uint64_t messages_sent() const noexcept { return _messages_sent; }
61  uint64_t messages_received() const noexcept { return _messages_received; }
63  uint64_t bytes_sent() const noexcept { return _bytes_sent; }
65  uint64_t bytes_received() const noexcept { return _bytes_received; }
66 
68  bool IsConnected() const noexcept { return _connected; }
69 
71 
76  bool Disconnect(websocketpp::close::status::value code = websocketpp::close::status::normal, const std::string& reason = "") { return Disconnect(false, code, reason); }
77 
79 
85  size_t Send(const void* buffer, size_t size, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::binary);
87 
92  size_t Send(const std::string& text, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::text);
94 
98  size_t Send(const WebSocketMessage& message);
99 
100 protected:
102  virtual void onConnected() {}
104  virtual void onDisconnected() {}
105 
107 
110  virtual void onReceived(const WebSocketMessage& message) {}
111 
113 
118  virtual void onError(int error, const std::string& category, const std::string& message) {}
119 
120 private:
121  // Session Id
122  CppCommon::UUID _id;
123  // Session server & connection
124  std::shared_ptr<WebSocketServer<TServer, TSession>> _server;
125  websocketpp::connection_hdl _connection;
126  std::atomic<bool> _connected;
127  // Session statistic
128  uint64_t _messages_sent;
129  uint64_t _messages_received;
130  uint64_t _bytes_sent;
131  uint64_t _bytes_received;
132 
134 
137  void Connect(websocketpp::connection_hdl connection);
139 
145  bool Disconnect(bool dispatch, websocketpp::close::status::value code = websocketpp::close::status::normal, const std::string& reason = "");
146 
148  void Disconnected();
149 
151  void SendError(std::error_code ec);
152 };
153 
154 } // namespace Asio
155 } // namespace CppServer
156 
157 #endif // CPPSERVER_ASIO_WEBSOCKET_SESSION_H
WebSocketSession(std::shared_ptr< WebSocketServer< TServer, TSession >> server)
Initialize the session with a given server.
virtual void onConnected()
Handle session connected notification.
C++ Server project definitions.
Definition: asio.h:24
uint64_t bytes_received() const noexcept
Get the number of bytes received by this session.
std::shared_ptr< WebSocketServer< TServer, TSession > > & server() noexcept
Get the session server.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
size_t Send(const void *buffer, size_t size, websocketpp::frame::opcode::value opcode=websocketpp::frame::opcode::binary)
Send data into the session.
WebSocketSession & operator=(const WebSocketSession &)=delete
bool Disconnect(websocketpp::close::status::value code=websocketpp::close::status::normal, const std::string &reason="")
Disconnect the session.
uint64_t messages_received() const noexcept
Get the number messages received by this session.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
WebSocketConnection::message_ptr WebSocketMessage
WebSocket message.
Definition: websocket.h:31
Asio service definition.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by this session.
uint64_t messages_sent() const noexcept
Get the number messages sent by this session.
virtual ~WebSocketSession()=default
virtual void onReceived(const WebSocketMessage &message)
Handle message received notification.
websocketpp::connection_hdl & connection() noexcept
Get the session connection.
const CppCommon::UUID & id() const noexcept
Get the session Id.
bool IsConnected() const noexcept
Is the session connected?
virtual void onDisconnected()
Handle session disconnected notification.
WebSocket C++ Library definition.