Kurlyk
Loading...
Searching...
No Matches
WebSocketManager.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_WEBSOCKET_MANAGER_HPP_INCLUDED
3#define _KURLYK_WEBSOCKET_MANAGER_HPP_INCLUDED
4
7
9
10#ifdef KURLYK_USE_SIMPLEWEB
11#include "client/SimpleWeb.hpp"
12#endif
13
14namespace kurlyk {
15
19 public:
20
24 static WebSocketManager* instance = new WebSocketManager();
25 return *instance;
26 }
27
32 void process() override {
33 std::lock_guard<std::mutex> lock(m_client_list_mutex);
34 for (auto &&client_weak_ptr : m_client_list) {
35 if (auto client_ptr = client_weak_ptr.lock()) {
36 client_ptr->process();
37 }
38 }
39 // Remove expired clients from the list
40 m_client_list.remove_if([](const std::weak_ptr<IWebSocketClient>& client_weak_ptr) {
41 return client_weak_ptr.expired();
42 });
43 }
44
48 void shutdown() override {
49 std::lock_guard<std::mutex> lock(m_client_list_mutex);
50 for (auto &&client_weak_ptr : m_client_list) {
51 if (auto client_ptr = client_weak_ptr.lock()) {
52 client_ptr->shutdown();
53 }
54 }
55 }
56
59 const bool is_loaded() const override {
60 std::lock_guard<std::mutex> lock(m_client_list_mutex);
61 for (auto &&client_weak_ptr : m_client_list) {
62 if (auto client_ptr = client_weak_ptr.lock()) {
63 if (client_ptr->is_running()) return true;
64 }
65 }
66 return false;
67 }
68
71 std::shared_ptr<IWebSocketClient> create_client() {
72 std::shared_ptr<IWebSocketClient> client;
73
74# ifdef KURLYK_USE_EMSCRIPTEN
75 // Client for the Emscripten platform
76# if __cplusplus >= 201402L
77 client = std::make_shared<EmscriptenWebSocketClientAdapter>();
78# else
79 client = std::shared_ptr<EmscriptenWebSocketClientAdapter>(new EmscriptenWebSocketClientAdapter());
80# endif
81# endif
82
83# ifdef KURLYK_USE_SIMPLEWEB
84 // Client for other platforms
85# if __cplusplus >= 201402L
86 client = std::make_shared<SimpleWebSocketClientAdapter>();
87# else
88 client = std::shared_ptr<SimpleWebSocketClientAdapter>(new SimpleWebSocketClientAdapter());
89# endif
90# endif
91
92 std::lock_guard<std::mutex> lock(m_client_list_mutex);
93 m_client_list.push_back(client);
94 return client;
95 }
96
97 private:
98 mutable std::mutex m_client_list_mutex;
99 std::list<std::weak_ptr<IWebSocketClient>> m_client_list;
100
102 WebSocketManager() = default;
103
105 virtual ~WebSocketManager() = default;
106
109
112
113 }; // WebSocketManager
114
115}
116
117#endif // _KURLYK_WEBSOCKET_MANAGER_HPP_INCLUDED
Contains the definition of the BaseWebSocketClient class, which provides the base functionality for W...
Declares the SimpleWebSocketClientAdapter implementation for WebSocket communication using Simple-Web...
A WebSocket client implementation using the Emscripten API.
A WebSocket client adapter that leverages the Simple WebSocket Server library for managing WebSocket ...
WebSocketManager()=default
Private constructor to enforce singleton pattern.
static WebSocketManager & get_instance()
Get the singleton instance of WebSocketManager.
void shutdown() override
Shuts down all active WebSocket clients managed by this instance.
std::mutex m_client_list_mutex
Mutex for synchronizing access to the client list.
std::list< std::weak_ptr< IWebSocketClient > > m_client_list
List of WebSocket clients managed by the WebSocketManager.
virtual ~WebSocketManager()=default
Private destructor to enforce singleton pattern.
WebSocketManager(const WebSocketManager &)=delete
Deleted copy constructor to enforce the singleton pattern.
WebSocketManager & operator=(const WebSocketManager &)=delete
Deleted copy assignment operator to enforce the singleton pattern.
std::shared_ptr< IWebSocketClient > create_client()
Creates and returns a new WebSocket client instance based on the platform defined by compilation flag...
const bool is_loaded() const override
Checks if any WebSocket client is currently running.
void process() override
Processes all active WebSocket clients managed by this instance.
Interface for modules managed by NetworkWorker (e.g., HTTP, WebSocket).
Primary namespace for the Kurlyk library, encompassing initialization, request management,...