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_EMSCRIPTEN
12#endif
13
14#ifdef KURLYK_USE_SIMPLEWEB
15#include "client/SimpleWeb.hpp"
16#endif
17
18namespace kurlyk {
19
20#ifdef KURLYK_USE_EMSCRIPTEN
22 using selected_backend_client_t = EmscriptenWebSocketClientAdapter;
23#elif defined(KURLYK_USE_SIMPLEWEB)
25 using selected_backend_client_t = SimpleWebSocketClientAdapter;
26#endif
27
29 using selected_backend_client_ptr = std::shared_ptr<selected_backend_client_t>;
31 using selected_backend_client_weak_ptr = std::weak_ptr<selected_backend_client_t>;
32
36 public:
37
41 static WebSocketManager* instance = new WebSocketManager();
42 return *instance;
43 }
44
49 void process() override {
50 std::lock_guard<std::mutex> lock(m_client_list_mutex);
51 for (auto &&client_weak_ptr : m_client_list) {
52 if (auto client_ptr = client_weak_ptr.lock()) {
53 client_ptr->process();
54 }
55 }
56 // Remove expired clients from the list
57 m_client_list.remove_if([](const selected_backend_client_weak_ptr& client_weak_ptr) {
58 return client_weak_ptr.expired();
59 });
60 }
61
65 void shutdown() override {
66 std::lock_guard<std::mutex> lock(m_client_list_mutex);
67 for (auto &&client_weak_ptr : m_client_list) {
68 if (auto client_ptr = client_weak_ptr.lock()) {
69 client_ptr->shutdown();
70 }
71 }
72 }
73
76 const bool is_loaded() const override {
77 std::lock_guard<std::mutex> lock(m_client_list_mutex);
78 for (auto &&client_weak_ptr : m_client_list) {
79 if (auto client_ptr = client_weak_ptr.lock()) {
80 if (client_ptr->is_running()) return true;
81 }
82 }
83 return false;
84 }
85
90
91# ifdef KURLYK_USE_EMSCRIPTEN
92 // Client for the Emscripten platform
93# if __cplusplus >= 201402L
94 client = std::make_shared<selected_backend_client_t>();
95# else
96 client = selected_backend_client_ptr(new selected_backend_client_t());
97# endif
98# endif
99
100# ifdef KURLYK_USE_SIMPLEWEB
101 // Client for other platforms
102# if __cplusplus >= 201402L
103 client = std::make_shared<selected_backend_client_t>();
104# else
105 client = selected_backend_client_ptr(new selected_backend_client_t());
106# endif
107# endif
108
109 std::lock_guard<std::mutex> lock(m_client_list_mutex);
110 m_client_list.push_back(client);
111 return client;
112 }
113
114 private:
115 mutable std::mutex m_client_list_mutex;
116 std::list<selected_backend_client_weak_ptr> m_client_list;
117
119 WebSocketManager() = default;
120
122 virtual ~WebSocketManager() = default;
123
126
129
130 }; // WebSocketManager
131
132}
133
134#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...
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 backend-specific WebSocket clients managed by this instance.
std::mutex m_client_list_mutex
Mutex for synchronizing access to the client list.
selected_backend_client_ptr create_client()
Creates and returns a new backend-specific WebSocket client selected by compilation flags.
std::list< selected_backend_client_weak_ptr > m_client_list
List of managed backend-specific WebSocket clients.
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.
const bool is_loaded() const override
Checks if any managed backend-specific WebSocket client is currently running.
void process() override
Processes all active backend-specific 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,...
std::shared_ptr< selected_backend_client_t > selected_backend_client_ptr
Shared pointer to the compile-time selected backend client type.
std::weak_ptr< selected_backend_client_t > selected_backend_client_weak_ptr
Weak pointer to the compile-time selected backend client type.