Plotly.cpp 0.1.0
A C++ plotting library for expressive, interactive, real-time & streaming data visualization
Loading...
Searching...
No Matches
websockets_endpoint.hpp
Go to the documentation of this file.
1
9
10#ifndef PLOTLY_DETAILS_WEBSOCKETS_ENDPOINT_HPP
11#define PLOTLY_DETAILS_WEBSOCKETS_ENDPOINT_HPP
12
13#include <atomic>
14#include <chrono>
15#include <condition_variable>
16#include <functional>
17#include <mutex>
18#include <queue>
19#include <string>
20#include <string_view>
21#include <thread>
22#include <unordered_map>
23
24namespace plotly::detail {
25
38public:
43
48
53
58 -> WebsocketEndpointInterface & = delete;
59
69 [[nodiscard]] virtual auto
70 waitConnection(std::chrono::milliseconds timeout) const -> bool = 0;
71
79 [[nodiscard]] virtual auto isConnected() const -> bool = 0;
80
90 virtual auto send(const std::string_view &message) -> bool = 0;
91
96 using callback_t = std::function<void(const std::string_view &)>;
97
107 virtual void registerCallback(const std::string_view &eventName,
108 callback_t callback) = 0;
109
117 virtual void unregisterCallback(const std::string_view &eventName) = 0;
118
126 virtual void stop() = 0;
127
135 [[nodiscard]] virtual auto getName() const -> std::string = 0;
136};
137
158protected:
162 std::atomic<bool> running{false};
163
167 std::thread serviceThread;
168
173
177 std::condition_variable recvMessagesCv;
178
182 std::queue<std::string> recvMessages;
183
187 std::mutex callbackMutex;
188
192 std::unordered_map<std::string, callback_t> callbacks;
193
198
206 virtual void serviceLoop() = 0;
207
215
223 void handleMessage(const std::string &message);
224
232
233public:
242
251 void registerCallback(const std::string_view &eventName,
252 callback_t callback) override;
253
261 void unregisterCallback(const std::string_view &eventName) override;
262};
263} // namespace plotly::detail
264#endif
Base implementation class for WebSocket endpoints.
Definition websockets_endpoint.hpp:157
std::atomic< bool > running
Atomic flag indicating if the endpoint is running.
Definition websockets_endpoint.hpp:162
std::thread callbackExecutorThread
Thread for executing callbacks.
Definition websockets_endpoint.hpp:197
void callbackExecutorLoop()
Main loop for the callback executor thread.
Definition websockets_endpoint.cpp:14
virtual void serviceLoop()=0
Pure virtual method for the main service loop.
void startCallbackExecutor()
Start the callback executor thread.
Definition websockets_endpoint.cpp:59
std::queue< std::string > recvMessages
Queue of received messages waiting to be processed.
Definition websockets_endpoint.hpp:182
std::thread serviceThread
Service thread for handling WebSocket operations.
Definition websockets_endpoint.hpp:167
std::condition_variable recvMessagesCv
Condition variable for notifying about new received messages.
Definition websockets_endpoint.hpp:177
void stopCallbackExecutor()
Stop the callback executor thread and clean up resources.
Definition websockets_endpoint.cpp:65
std::mutex recvMessagesMutex
Mutex protecting the received messages queue.
Definition websockets_endpoint.hpp:172
std::mutex callbackMutex
Mutex protecting the callbacks map.
Definition websockets_endpoint.hpp:187
std::unordered_map< std::string, callback_t > callbacks
Map of event names to their associated callback functions.
Definition websockets_endpoint.hpp:192
void handleMessage(const std::string &message)
Handle an incoming message by queuing it for callback processing.
Definition websockets_endpoint.cpp:53
WebsocketEndpointInterface(const WebsocketEndpointInterface &)=delete
Copy constructor (deleted)
virtual auto getName() const -> std::string=0
Get the name/identifier of this endpoint.
auto operator=(const WebsocketEndpointInterface &) -> WebsocketEndpointInterface &=delete
Copy assignment operator (deleted)
virtual void stop()=0
Stop the endpoint and clean up resources.
virtual auto send(const std::string_view &message) -> bool=0
Send a message through the WebSocket connection.
virtual void registerCallback(const std::string_view &eventName, callback_t callback)=0
Register a callback for a specific event.
WebsocketEndpointInterface()
Default constructor.
virtual auto isConnected() const -> bool=0
Check if the endpoint is currently connected.
std::function< void(const std::string_view &)> callback_t
Callback function type for handling incoming messages.
Definition websockets_endpoint.hpp:96
virtual auto waitConnection(std::chrono::milliseconds timeout) const -> bool=0
Wait for a connection to be established.
virtual ~WebsocketEndpointInterface()
Virtual destructor.
virtual void unregisterCallback(const std::string_view &eventName)=0
Unregister a callback for a specific event.
Definition browser.cpp:27