ndmspc v1.2.0-0.1.rc5
Loading...
Searching...
No Matches
Ndmspc::NWsClient Class Reference

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.
 

Detailed Description

WebSocket client for asynchronous communication using libwebsockets.

Handles connection management, message sending/receiving, and threading.

Definition at line 49 of file NWsClient.h.

Member Typedef Documentation

◆ OnMessageCallback

using Ndmspc::NWsClient::OnMessageCallback = std::function<void(const std::string &)>

Callback type for received messages.

Definition at line 91 of file NWsClient.h.

Constructor & Destructor Documentation

◆ NWsClient()

Ndmspc::NWsClient::NWsClient ( int maxRetries = 5,
int retryDelayMs = 1000 )

Constructor.

Parameters
maxRetriesMaximum number of connection retries.
retryDelayMsDelay between retries in milliseconds.

Definition at line 133 of file NWsClient.cxx.

References fConnected, fConnectionAttemptComplete, fLwsContext, fMaxRetries, fRetryDelayMs, fShutdownRequested, and fWsi.

◆ ~NWsClient()

Ndmspc::NWsClient::~NWsClient ( )

Destructor. Cleans up resources and disconnects.

Definition at line 142 of file NWsClient.cxx.

References Disconnect().

Member Function Documentation

◆ Connect()

bool Ndmspc::NWsClient::Connect ( const std::string & uriString)

◆ Disconnect()

void Ndmspc::NWsClient::Disconnect ( )

◆ IsConnected()

bool Ndmspc::NWsClient::IsConnected ( ) const
inline

Check if the client is currently connected.

Returns
True if connected.

Definition at line 88 of file NWsClient.h.

References fConnected.

◆ LwsServiceLoop()

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().

◆ ParseUri()

WS_URI Ndmspc::NWsClient::ParseUri ( const std::string & uriString)
static

Parse a WebSocket URI string into its components.

Parameters
uriStringURI string to parse.
Returns
Parsed WS_URI structure.

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().

◆ Send()

bool Ndmspc::NWsClient::Send ( const std::string & message)

Send a message to the server.

Parameters
messageMessage string to send.
Returns
True if message was queued for sending.

Definition at line 300 of file NWsClient.cxx.

References fConnected, fOutgoingMessageQueue, fOutgoingMutex, and fWsi.

Referenced by Ndmspc::NGnNavigator::Export().

◆ SetOnMessageCallback()

void Ndmspc::NWsClient::SetOnMessageCallback ( OnMessageCallback callback)
inline

Set the callback to be invoked when a message is received.

Parameters
callbackFunction to call with received message.

Definition at line 97 of file NWsClient.h.

References fOnMessageCallback.

Member Data Documentation

◆ fConnectCv

std::condition_variable Ndmspc::NWsClient::fConnectCv

Condition variable for connection.

Definition at line 131 of file NWsClient.h.

Referenced by Connect(), Disconnect(), and LwsServiceLoop().

◆ fConnected

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().

◆ fConnectionAttemptComplete

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().

◆ fConnectMutex

std::mutex Ndmspc::NWsClient::fConnectMutex

Mutex for connection state.

Definition at line 130 of file NWsClient.h.

Referenced by Connect().

◆ fgProtocolName

const char* Ndmspc::NWsClient::fgProtocolName = "ndmspc-protocol"
staticconstexpr

Protocol name for websocket communication.

Definition at line 51 of file NWsClient.h.

Referenced by Connect().

◆ fHost

std::string Ndmspc::NWsClient::fHost

Hostname.

Definition at line 113 of file NWsClient.h.

Referenced by Connect().

◆ fLwsContext

struct lws_context* Ndmspc::NWsClient::fLwsContext

libwebsockets context

Definition at line 107 of file NWsClient.h.

Referenced by Connect(), Disconnect(), LwsServiceLoop(), and NWsClient().

◆ fLwsServiceThread

std::thread Ndmspc::NWsClient::fLwsServiceThread

Thread running the service loop.

Definition at line 109 of file NWsClient.h.

Referenced by Connect(), and Disconnect().

◆ fMaxRetries

int Ndmspc::NWsClient::fMaxRetries

Maximum connection retries.

Definition at line 110 of file NWsClient.h.

Referenced by Connect(), and NWsClient().

◆ fOnMessageCallback

OnMessageCallback Ndmspc::NWsClient::fOnMessageCallback

Callback for received messages.

Definition at line 126 of file NWsClient.h.

Referenced by SetOnMessageCallback().

◆ fOutgoingMessageQueue

std::queue<std::string> Ndmspc::NWsClient::fOutgoingMessageQueue

Queue of outgoing messages.

Definition at line 121 of file NWsClient.h.

Referenced by Disconnect(), and Send().

◆ fOutgoingMutex

std::mutex Ndmspc::NWsClient::fOutgoingMutex

Mutex for outgoing queue.

Definition at line 122 of file NWsClient.h.

Referenced by Disconnect(), and Send().

◆ fPath

std::string Ndmspc::NWsClient::fPath

Path.

Definition at line 115 of file NWsClient.h.

Referenced by Connect().

◆ fPort

int Ndmspc::NWsClient::fPort

Port number.

Definition at line 114 of file NWsClient.h.

Referenced by Connect().

◆ fProtocols

lws_protocols Ndmspc::NWsClient::fProtocols
static
Initial value:
= {
{Ndmspc::NWsClient::fgProtocolName, lws_callback_client_impl, sizeof(Ndmspc::NWsClient *), 4096, 0, nullptr, 0},
LWS_PROTOCOL_LIST_TERM
}
WebSocket client for asynchronous communication using libwebsockets.
Definition NWsClient.h:49
static constexpr const char * fgProtocolName
Protocol name for websocket communication.
Definition NWsClient.h:51

Protocols supported by libwebsockets.

Definition at line 129 of file NWsClient.h.

Referenced by Connect().

◆ fRetryDelayMs

int Ndmspc::NWsClient::fRetryDelayMs

Delay between retries (ms)

Definition at line 111 of file NWsClient.h.

Referenced by Connect(), and NWsClient().

◆ fSendBuffer

std::vector<unsigned char> Ndmspc::NWsClient::fSendBuffer

Buffer for sending messages.

Definition at line 123 of file NWsClient.h.

◆ fSendCv

std::condition_variable Ndmspc::NWsClient::fSendCv

Condition variable for sending messages.

Definition at line 124 of file NWsClient.h.

◆ fShutdownRequested

std::atomic<bool> Ndmspc::NWsClient::fShutdownRequested

Shutdown flag.

Definition at line 118 of file NWsClient.h.

Referenced by Connect(), Disconnect(), LwsServiceLoop(), and NWsClient().

◆ fWsi

struct lws* Ndmspc::NWsClient::fWsi

WebSocket instance.

Definition at line 108 of file NWsClient.h.

Referenced by Connect(), Disconnect(), NWsClient(), and Send().


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