|
ndmspc v1.2.0-0.1.rc5
|
WebSocket client for asynchronous communication using libwebsockets. More...
#include <NWsClient.h>
Public Types | |
| using | OnMessageCallback = std::function<void(const std::string &)> |
| Callback type for received messages. | |
Public Member Functions | |
| NWsClient (int maxRetries=5, int retryDelayMs=1000) | |
| Constructor. | |
| ~NWsClient () | |
| Destructor. Cleans up resources and disconnects. | |
| bool | Connect (const std::string &uriString) |
| Connect to a WebSocket server. | |
| void | Disconnect () |
| Disconnect from the WebSocket server. | |
| bool | Send (const std::string &message) |
| Send a message to the server. | |
| bool | IsConnected () const |
| Check if the client is currently connected. | |
| void | SetOnMessageCallback (OnMessageCallback callback) |
| Set the callback to be invoked when a message is received. | |
| void | LwsServiceLoop () |
| Service loop for libwebsockets running in a separate thread. | |
Static Public Member Functions | |
| static WS_URI | ParseUri (const std::string &uriString) |
| Parse a WebSocket URI string into its components. | |
Public Attributes | |
| struct lws_context * | fLwsContext |
| libwebsockets context | |
| struct lws * | fWsi |
| WebSocket instance. | |
| std::thread | fLwsServiceThread |
| Thread running the service loop. | |
| int | fMaxRetries |
| Maximum connection retries. | |
| int | fRetryDelayMs |
| Delay between retries (ms) | |
| std::string | fHost |
| Hostname. | |
| int | fPort |
| Port number. | |
| std::string | fPath |
| Path. | |
| std::atomic< bool > | fConnected |
| Connection status. | |
| std::atomic< bool > | fShutdownRequested |
| Shutdown flag. | |
| std::atomic< bool > | fConnectionAttemptComplete |
| Connection attempt completion flag. | |
| std::queue< std::string > | fOutgoingMessageQueue |
| Queue of outgoing messages. | |
| std::mutex | fOutgoingMutex |
| Mutex for outgoing queue. | |
| std::vector< unsigned char > | fSendBuffer |
| Buffer for sending messages. | |
| std::condition_variable | fSendCv |
| Condition variable for sending messages. | |
| OnMessageCallback | fOnMessageCallback |
| Callback for received messages. | |
| std::mutex | fConnectMutex |
| Mutex for connection state. | |
| std::condition_variable | fConnectCv |
| Condition variable for connection. | |
Static Public Attributes | |
| static constexpr const char * | fgProtocolName = "ndmspc-protocol" |
| Protocol name for websocket communication. | |
| static lws_protocols | fProtocols [] |
| Protocols supported by libwebsockets. | |
WebSocket client for asynchronous communication using libwebsockets.
Handles connection management, message sending/receiving, and threading.
Definition at line 49 of file NWsClient.h.
| using Ndmspc::NWsClient::OnMessageCallback = std::function<void(const std::string &)> |
Callback type for received messages.
Definition at line 91 of file NWsClient.h.
| Ndmspc::NWsClient::NWsClient | ( | int | maxRetries = 5, |
| int | retryDelayMs = 1000 ) |
Constructor.
| maxRetries | Maximum number of connection retries. |
| retryDelayMs | Delay between retries in milliseconds. |
Definition at line 133 of file NWsClient.cxx.
References fConnected, fConnectionAttemptComplete, fLwsContext, fMaxRetries, fRetryDelayMs, fShutdownRequested, and fWsi.
| Ndmspc::NWsClient::~NWsClient | ( | ) |
Destructor. Cleans up resources and disconnects.
Definition at line 142 of file NWsClient.cxx.
References Disconnect().
| bool Ndmspc::NWsClient::Connect | ( | const std::string & | uriString | ) |
Connect to a WebSocket server.
| uriString | URI string to connect to. |
Definition at line 148 of file NWsClient.cxx.
References Disconnect(), fConnectCv, fConnected, fConnectionAttemptComplete, fConnectMutex, fgProtocolName, fHost, Ndmspc::WS_URI::fHost, fLwsContext, fLwsServiceThread, fMaxRetries, fPath, Ndmspc::WS_URI::fPath, fPort, Ndmspc::WS_URI::fPort, fProtocols, fRetryDelayMs, Ndmspc::WS_URI::fScheme, fShutdownRequested, fWsi, LwsServiceLoop(), and ParseUri().
Referenced by Ndmspc::NGnNavigator::Export().
| void Ndmspc::NWsClient::Disconnect | ( | ) |
Disconnect from the WebSocket server.
Definition at line 256 of file NWsClient.cxx.
References fConnectCv, fConnected, fConnectionAttemptComplete, fLwsContext, fLwsServiceThread, fOutgoingMessageQueue, fOutgoingMutex, fShutdownRequested, and fWsi.
Referenced by Connect(), Ndmspc::NGnNavigator::Export(), and ~NWsClient().
|
inline |
Check if the client is currently connected.
Definition at line 88 of file NWsClient.h.
References fConnected.
| void Ndmspc::NWsClient::LwsServiceLoop | ( | ) |
Service loop for libwebsockets running in a separate thread.
Definition at line 330 of file NWsClient.cxx.
References fConnectCv, fConnected, fConnectionAttemptComplete, fLwsContext, and fShutdownRequested.
Referenced by Connect().
|
static |
Parse a WebSocket URI string into its components.
| uriString | URI string to parse. |
Definition at line 351 of file NWsClient.cxx.
References Ndmspc::WS_URI::fHost, Ndmspc::WS_URI::fPath, Ndmspc::WS_URI::fPort, and Ndmspc::WS_URI::fScheme.
Referenced by Connect().
| bool Ndmspc::NWsClient::Send | ( | const std::string & | message | ) |
Send a message to the server.
| message | Message string to send. |
Definition at line 300 of file NWsClient.cxx.
References fConnected, fOutgoingMessageQueue, fOutgoingMutex, and fWsi.
Referenced by Ndmspc::NGnNavigator::Export().
|
inline |
Set the callback to be invoked when a message is received.
| callback | Function to call with received message. |
Definition at line 97 of file NWsClient.h.
References fOnMessageCallback.
| std::condition_variable Ndmspc::NWsClient::fConnectCv |
Condition variable for connection.
Definition at line 131 of file NWsClient.h.
Referenced by Connect(), Disconnect(), and LwsServiceLoop().
| std::atomic<bool> Ndmspc::NWsClient::fConnected |
Connection status.
Definition at line 117 of file NWsClient.h.
Referenced by Connect(), Disconnect(), IsConnected(), LwsServiceLoop(), NWsClient(), and Send().
| std::atomic<bool> Ndmspc::NWsClient::fConnectionAttemptComplete |
Connection attempt completion flag.
Definition at line 119 of file NWsClient.h.
Referenced by Connect(), Disconnect(), LwsServiceLoop(), and NWsClient().
| std::mutex Ndmspc::NWsClient::fConnectMutex |
|
staticconstexpr |
Protocol name for websocket communication.
Definition at line 51 of file NWsClient.h.
Referenced by Connect().
| std::string Ndmspc::NWsClient::fHost |
| struct lws_context* Ndmspc::NWsClient::fLwsContext |
libwebsockets context
Definition at line 107 of file NWsClient.h.
Referenced by Connect(), Disconnect(), LwsServiceLoop(), and NWsClient().
| std::thread Ndmspc::NWsClient::fLwsServiceThread |
Thread running the service loop.
Definition at line 109 of file NWsClient.h.
Referenced by Connect(), and Disconnect().
| int Ndmspc::NWsClient::fMaxRetries |
Maximum connection retries.
Definition at line 110 of file NWsClient.h.
Referenced by Connect(), and NWsClient().
| OnMessageCallback Ndmspc::NWsClient::fOnMessageCallback |
Callback for received messages.
Definition at line 126 of file NWsClient.h.
Referenced by SetOnMessageCallback().
| std::queue<std::string> Ndmspc::NWsClient::fOutgoingMessageQueue |
Queue of outgoing messages.
Definition at line 121 of file NWsClient.h.
Referenced by Disconnect(), and Send().
| std::mutex Ndmspc::NWsClient::fOutgoingMutex |
Mutex for outgoing queue.
Definition at line 122 of file NWsClient.h.
Referenced by Disconnect(), and Send().
| std::string Ndmspc::NWsClient::fPath |
| int Ndmspc::NWsClient::fPort |
|
static |
Protocols supported by libwebsockets.
Definition at line 129 of file NWsClient.h.
Referenced by Connect().
| int Ndmspc::NWsClient::fRetryDelayMs |
Delay between retries (ms)
Definition at line 111 of file NWsClient.h.
Referenced by Connect(), and NWsClient().
| std::vector<unsigned char> Ndmspc::NWsClient::fSendBuffer |
Buffer for sending messages.
Definition at line 123 of file NWsClient.h.
| std::condition_variable Ndmspc::NWsClient::fSendCv |
Condition variable for sending messages.
Definition at line 124 of file NWsClient.h.
| std::atomic<bool> Ndmspc::NWsClient::fShutdownRequested |
Shutdown flag.
Definition at line 118 of file NWsClient.h.
Referenced by Connect(), Disconnect(), LwsServiceLoop(), and NWsClient().
| struct lws* Ndmspc::NWsClient::fWsi |
WebSocket instance.
Definition at line 108 of file NWsClient.h.
Referenced by Connect(), Disconnect(), NWsClient(), and Send().