9 #ifndef CPPSERVER_ASIO_WEBSOCKET_SERVER_H 10 #define CPPSERVER_ASIO_WEBSOCKET_SERVER_H 22 template <
class TServer,
class TSession>
31 template <
class TServer,
class TSession>
32 class WebSocketServer :
public std::enable_shared_from_this<WebSocketServer<TServer, TSession>>
34 template <
class TSomeServer,
class TSomeSession>
66 std::shared_ptr<Service>&
service() noexcept {
return _service; }
68 asio::ip::tcp::endpoint&
endpoint() noexcept {
return _endpoint; }
79 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
84 bool IsStarted() const noexcept {
return _started; }
109 bool Multicast(
const void* buffer,
size_t size, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::binary);
116 bool Multicast(
const std::string& text, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::text);
153 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
157 std::shared_ptr<Service> _service;
159 asio::ip::tcp::endpoint _endpoint;
161 std::atomic<bool> _initialized;
162 std::atomic<bool> _started;
164 uint64_t _messages_sent;
165 uint64_t _messages_received;
166 uint64_t _bytes_sent;
167 uint64_t _bytes_received;
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;
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;
184 std::shared_ptr<TSession> RegisterSession(websocketpp::connection_hdl connection);
189 void UnregisterSession(websocketpp::connection_hdl connection);
194 void UnregisterSession(
const CppCommon::UUID&
id);
203 void SendError(std::error_code ec);
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.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
C++ Server project definitions.
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.
bool Stop()
Stop the server.
websocketpp::server< websocketpp::config::asio > WebSocketServerCore
WebSocket server core.
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.
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.