CppServer  1.0.0.0
C++ Server Library
websocket_server.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_ASIO_WEBSOCKET_SERVER_H
10 #define CPPSERVER_ASIO_WEBSOCKET_SERVER_H
11 
12 #include "websocket_session.h"
13 
14 #include <map>
15 #include <mutex>
16 #include <tuple>
17 #include <vector>
18 
19 namespace CppServer {
20 namespace Asio {
21 
22 template <class TServer, class TSession>
24 
26 
31 template <class TServer, class TSession>
32 class WebSocketServer : public std::enable_shared_from_this<WebSocketServer<TServer, TSession>>
33 {
34  template <class TSomeServer, class TSomeSession>
35  friend class WebSocketSession;
36 
37 public:
39 
44  explicit WebSocketServer(std::shared_ptr<Service> service, InternetProtocol protocol, int port);
46 
51  explicit WebSocketServer(std::shared_ptr<Service> service, const std::string& address, int port);
53 
57  explicit WebSocketServer(std::shared_ptr<Service> service, const asio::ip::tcp::endpoint& endpoint);
58  WebSocketServer(const WebSocketServer&) = delete;
59  WebSocketServer(WebSocketServer&&) = default;
60  virtual ~WebSocketServer() = default;
61 
62  WebSocketServer& operator=(const WebSocketServer&) = delete;
64 
66  std::shared_ptr<Service>& service() noexcept { return _service; }
68  asio::ip::tcp::endpoint& endpoint() noexcept { return _endpoint; }
70  WebSocketServerCore& core() noexcept { return _core; }
71 
73  uint64_t current_sessions() const noexcept { return _sessions.size(); }
75  uint64_t messages_sent() const noexcept { return _messages_sent; }
77  uint64_t messages_received() const noexcept { return _messages_received; }
79  uint64_t bytes_sent() const noexcept { return _bytes_sent; }
81  uint64_t bytes_received() const noexcept { return _bytes_received; }
82 
84  bool IsStarted() const noexcept { return _started; }
85 
87 
90  bool Start();
92 
95  bool Stop();
97 
100  bool Restart();
101 
103 
109  bool Multicast(const void* buffer, size_t size, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::binary);
111 
116  bool Multicast(const std::string& text, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::text);
118 
122  bool Multicast(const WebSocketMessage& message);
123 
125 
128  bool DisconnectAll();
129 
130 protected:
132  virtual void onStarted() {}
134  virtual void onStopped() {}
135 
137 
140  virtual void onConnected(std::shared_ptr<TSession>& session) {}
142 
145  virtual void onDisconnected(std::shared_ptr<TSession>& session) {}
146 
148 
153  virtual void onError(int error, const std::string& category, const std::string& message) {}
154 
155 private:
156  // Asio service
157  std::shared_ptr<Service> _service;
158  // Server endpoint & core
159  asio::ip::tcp::endpoint _endpoint;
160  WebSocketServerCore _core;
161  std::atomic<bool> _initialized;
162  std::atomic<bool> _started;
163  // Server statistic
164  uint64_t _messages_sent;
165  uint64_t _messages_received;
166  uint64_t _bytes_sent;
167  uint64_t _bytes_received;
168  // Server sessions
169  std::map<websocketpp::connection_hdl, std::shared_ptr<TSession>, std::owner_less<websocketpp::connection_hdl>> _connections;
170  std::map<CppCommon::UUID, std::shared_ptr<TSession>> _sessions;
171  // Multicast buffer
172  std::mutex _multicast_lock;
173  std::vector<std::tuple<std::vector<uint8_t>, websocketpp::frame::opcode::value>> _multicast_buffer;
174  std::vector<std::tuple<std::string, websocketpp::frame::opcode::value>> _multicast_text;
175  std::vector<WebSocketMessage> _multicast_messages;
176 
178  void InitAsio();
179 
181  /*
182  \param connection - WebSocket connection
183  */
184  std::shared_ptr<TSession> RegisterSession(websocketpp::connection_hdl connection);
186 
189  void UnregisterSession(websocketpp::connection_hdl connection);
191 
194  void UnregisterSession(const CppCommon::UUID& id);
195 
197  void MulticastAll();
198 
200  void ClearBuffers();
201 
203  void SendError(std::error_code ec);
204 };
205 
208 } // namespace Asio
209 } // namespace CppServer
210 
211 #include "websocket_session.inl"
212 #include "websocket_server.inl"
213 
214 #endif // CPPSERVER_ASIO_WEBSOCKET_SERVER_H
bool Restart()
Restart the server.
WebSocket session inline implementation.
bool DisconnectAll()
Disconnect all connected sessions.
bool Multicast(const void *buffer, size_t size, websocketpp::frame::opcode::value opcode=websocketpp::frame::opcode::binary)
Multicast data to all connected sessions.
WebSocketServer & operator=(const WebSocketServer &)=delete
WebSocketServerCore & core() noexcept
Get the WebSocket server core.
WebSocket session definition.
virtual ~WebSocketServer()=default
InternetProtocol
Internet protocol.
Definition: asio.h:36
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
C++ Server project definitions.
Definition: asio.h:24
virtual void onConnected(std::shared_ptr< TSession > &session)
Handle new session connected notification.
bool IsStarted() const noexcept
Is the server started?
uint64_t current_sessions() const noexcept
Get the number of sessions currently connected to this server.
virtual void onDisconnected(std::shared_ptr< TSession > &session)
Handle session disconnected notification.
WebSocket server inline implementation.
bool Start()
Start the server.
websocketpp::server< websocketpp::config::asio > WebSocketServerCore
WebSocket server core.
Definition: websocket.h:27
WebSocketServer(std::shared_ptr< Service > service, InternetProtocol protocol, int port)
Initialize WebSocket server with a given Asio service, protocol and port number.
WebSocketConnection::message_ptr WebSocketMessage
WebSocket message.
Definition: websocket.h:31
virtual void onStarted()
Handle server started notification.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the server endpoint.
uint64_t messages_sent() const noexcept
Get the number messages sent by this server.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
uint64_t bytes_received() const noexcept
Get the number of bytes received by this server.
virtual void onStopped()
Handle server stopped notification.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by this server.
uint64_t messages_received() const noexcept
Get the number messages received by this server.