Plotly.cpp 0.1.0
A C++ plotting library for expressive, interactive, real-time & streaming data visualization
Loading...
Searching...
No Matches
plotly::detail::WebsocketEndpointImpl Class Referenceabstract

Base implementation class for WebSocket endpoints. More...

#include <websockets_endpoint.hpp>

Inheritance diagram for plotly::detail::WebsocketEndpointImpl:
Collaboration diagram for plotly::detail::WebsocketEndpointImpl:

Public Member Functions

void stopCallbackExecutor ()
 Stop the callback executor thread and clean up resources.
void registerCallback (const std::string_view &eventName, callback_t callback) override
 Register a callback for a specific event (override)
void unregisterCallback (const std::string_view &eventName) override
 Unregister a callback for a specific event (override)
Public Member Functions inherited from plotly::detail::WebsocketEndpointInterface
 WebsocketEndpointInterface ()
 Default constructor.
virtual ~WebsocketEndpointInterface ()
 Virtual destructor.
 WebsocketEndpointInterface (const WebsocketEndpointInterface &)=delete
 Copy constructor (deleted)
auto operator= (const WebsocketEndpointInterface &) -> WebsocketEndpointInterface &=delete
 Copy assignment operator (deleted)
virtual auto waitConnection (std::chrono::milliseconds timeout) const -> bool=0
 Wait for a connection to be established.
virtual auto isConnected () const -> bool=0
 Check if the endpoint is currently connected.
virtual auto send (const std::string_view &message) -> bool=0
 Send a message through the WebSocket connection.
virtual void stop ()=0
 Stop the endpoint and clean up resources.
virtual auto getName () const -> std::string=0
 Get the name/identifier of this endpoint.

Protected Member Functions

virtual void serviceLoop ()=0
 Pure virtual method for the main service loop.
void callbackExecutorLoop ()
 Main loop for the callback executor thread.
void handleMessage (const std::string &message)
 Handle an incoming message by queuing it for callback processing.
void startCallbackExecutor ()
 Start the callback executor thread.

Protected Attributes

std::atomic< bool > running {false}
 Atomic flag indicating if the endpoint is running.
std::thread serviceThread
 Service thread for handling WebSocket operations.
std::mutex recvMessagesMutex
 Mutex protecting the received messages queue.
std::condition_variable recvMessagesCv
 Condition variable for notifying about new received messages.
std::queue< std::string > recvMessages
 Queue of received messages waiting to be processed.
std::mutex callbackMutex
 Mutex protecting the callbacks map.
std::unordered_map< std::string, callback_tcallbacks
 Map of event names to their associated callback functions.
std::thread callbackExecutorThread
 Thread for executing callbacks.

Additional Inherited Members

Public Types inherited from plotly::detail::WebsocketEndpointInterface
using callback_t = std::function<void(const std::string_view &)>
 Callback function type for handling incoming messages.

Detailed Description

Base implementation class for WebSocket endpoints.

This class provides a concrete implementation of the WebsocketEndpointInterface with common functionality shared between client and server endpoints. It handles message queuing, callback management, and threading infrastructure.

The implementation uses a multi-threaded design:

  • A service thread for handling WebSocket operations (derived classes implement serviceLoop)
  • A callback executor thread for processing incoming messages and invoking callbacks
  • Thread-safe message queuing between the service and callback threads

Derived classes need to implement the pure virtual serviceLoop() method to provide specific client or server behavior.

Member Function Documentation

◆ callbackExecutorLoop()

void plotly::detail::WebsocketEndpointImpl::callbackExecutorLoop ( )
protected

Main loop for the callback executor thread.

This method runs in the callback executor thread and processes received messages by invoking the appropriate registered callbacks.

◆ handleMessage()

void plotly::detail::WebsocketEndpointImpl::handleMessage ( const std::string & message)
protected

Handle an incoming message by queuing it for callback processing.

Parameters
messageThe received message to handle

This method is called by the service thread to queue received messages for processing by the callback executor thread.

◆ registerCallback()

void plotly::detail::WebsocketEndpointImpl::registerCallback ( const std::string_view & eventName,
callback_t callback )
overridevirtual

Register a callback for a specific event (override)

Parameters
eventNameThe name of the event to listen for
callbackThe callback function to invoke when the event occurs

This implementation stores the callback in the callbacks map in a thread-safe manner using the callback mutex.

Implements plotly::detail::WebsocketEndpointInterface.

◆ serviceLoop()

virtual void plotly::detail::WebsocketEndpointImpl::serviceLoop ( )
protectedpure virtual

Pure virtual method for the main service loop.

Derived classes must implement this method to provide specific WebSocket client or server behavior. This method runs in the service thread and should handle connection management and message reception.

Implemented in plotly::detail::WebsocketClient, and plotly::detail::WebsocketServer.

◆ startCallbackExecutor()

void plotly::detail::WebsocketEndpointImpl::startCallbackExecutor ( )
protected

Start the callback executor thread.

This method initializes and starts the callback executor thread that processes incoming messages and invokes registered callbacks.

◆ stopCallbackExecutor()

void plotly::detail::WebsocketEndpointImpl::stopCallbackExecutor ( )

Stop the callback executor thread and clean up resources.

This method stops the callback executor thread and performs necessary cleanup. It's called automatically by the destructor but can also be called explicitly if needed.

◆ unregisterCallback()

void plotly::detail::WebsocketEndpointImpl::unregisterCallback ( const std::string_view & eventName)
overridevirtual

Unregister a callback for a specific event (override)

Parameters
eventNameThe name of the event to stop listening for

This implementation removes the callback from the callbacks map in a thread-safe manner using the callback mutex.

Implements plotly::detail::WebsocketEndpointInterface.

Member Data Documentation

◆ callbackExecutorThread

std::thread plotly::detail::WebsocketEndpointImpl::callbackExecutorThread
protected

Thread for executing callbacks.

◆ callbackMutex

std::mutex plotly::detail::WebsocketEndpointImpl::callbackMutex
protected

Mutex protecting the callbacks map.

◆ callbacks

std::unordered_map<std::string, callback_t> plotly::detail::WebsocketEndpointImpl::callbacks
protected

Map of event names to their associated callback functions.

◆ recvMessages

std::queue<std::string> plotly::detail::WebsocketEndpointImpl::recvMessages
protected

Queue of received messages waiting to be processed.

◆ recvMessagesCv

std::condition_variable plotly::detail::WebsocketEndpointImpl::recvMessagesCv
protected

Condition variable for notifying about new received messages.

◆ recvMessagesMutex

std::mutex plotly::detail::WebsocketEndpointImpl::recvMessagesMutex
protected

Mutex protecting the received messages queue.

◆ running

std::atomic<bool> plotly::detail::WebsocketEndpointImpl::running {false}
protected

Atomic flag indicating if the endpoint is running.

◆ serviceThread

std::thread plotly::detail::WebsocketEndpointImpl::serviceThread
protected

Service thread for handling WebSocket operations.


The documentation for this class was generated from the following files: