10#ifndef PLOTLY_DETAILS_WEBSOCKETS_ENDPOINT_HPP
11#define PLOTLY_DETAILS_WEBSOCKETS_ENDPOINT_HPP
15#include <condition_variable>
22#include <unordered_map>
69 [[nodiscard]]
virtual auto
90 virtual auto
send(const std::string_view &message) ->
bool = 0;
96 using
callback_t = std::function<
void(const std::string_view &)>;
135 [[nodiscard]] virtual auto
getName() const -> std::
string = 0;
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