VR-Engage  2.2
Loading...
Searching...
No Matches
radarFxConnector.h
Go to the documentation of this file.
1// ******************************************************************************
2// ** Copyright (c) 2025 MAK Technologies
3// ** All rights reserved.
4// ******************************************************************************
5
6//! \file radarFxConnector.h
7//! \brief Defines the connector class for communicating with a RadarFX server
8//! \ingroup vreRadarFxConnector
9
10
11#pragma once
12
14
18
19#include <vlutil/vlInetTcpSocket.h>
20
21#include <list>
22#include <string>
23
24class DtExerciseConnection;
25class DtThread;
26
27namespace makRadarFx
28{
29//! \brief The default port that the RadarFX server listens on
30//!
31//! This constant defines the standard network port used for connecting
32//! to a RadarFX server when no custom port is specified
33const int DtRadarFXPort = 4567;
34
35//! \brief Forward declaration of the base message class
36class DtBaseMessage;
37
38//! \brief Forward declaration of the delegate template class
39//! \tparam Ret Return type of the delegate function
40//! \tparam Param0 Parameter type for the delegate function
41template <typename Ret, typename Param0>
42class DtDelegate;
43
44//! \brief Manages connection to a RadarFX server and handles message exchange
45//!
46//! The DtRadarFxConnector class provides a client interface for connecting to a
47//! RadarFX server. It manages the connection lifecycle, sends and receives messages,
48//! and provides callbacks for connection events. The class includes functionality
49//! for automatic reconnection and message queuing.
50//!
51//! This connector operates with a worker thread that handles the actual network
52//! communication while presenting a thread-safe interface to client code.
54{
55public:
56 //! \brief Type definition for connection state change callback delegates
57 //!
58 //! This delegate type is used for callbacks that are triggered when the
59 //! connection state changes (connected or disconnected)
61
62 //! \brief Default constructor
63 //!
64 //! Initializes a new RadarFx connector with default settings
66
67 //! \brief Virtual destructor
68 //!
69 //! Cleans up resources and stops the worker thread
71
72 //! \brief Establishes a connection to a remote RadarFX server
73 //! \param address IP address or hostname of the RadarFX server
74 //! \param port Network port the server is listening on (defaults to DtRadarFXPort)
75 //! \param attemptRetry Whether to automatically retry connection if it fails or disconnects
76 //!
77 //! Initiates a connection to the specified RadarFX server. When the connection
78 //! is established, all registered connection callbacks will be executed.
79 //! If attemptRetry is true, the connector will automatically attempt to
80 //! reconnect if the initial connection fails or if the connection is lost.
81 virtual void connect(const std::string& address, int port = DtRadarFXPort, bool attemptRetry = true);
82
83 //! \brief Disconnects from the RadarFX server
84 //!
85 //! Closes the connection to the server and stops any automatic reconnection
86 //! attempts. All registered disconnection callbacks will be executed.
87 virtual void disconnect();
88
89 //! \brief Checks if the connection is valid
90 //! \return True if the connection has been established and is currently valid
91 //!
92 //! Determines whether there is an active, valid connection to the RadarFX server
93 virtual bool isOk();
94
95 //! \brief Processes queued messages and updates internal state
96 //!
97 //! This method must be called periodically (typically once per frame) to
98 //! process incoming messages and execute associated callbacks. It also
99 //! handles connection retries if enabled.
100 virtual void tick();
101
102 //! \brief Sends a message to the RadarFX server
103 //! \param request Reference to the message to send
104 //!
105 //! Queues a message to be sent to the RadarFX server. A copy of the message
106 //! is made internally, so the original message can safely go out of scope
107 //! immediately after sendMessage is called. If the message has no ID or
108 //! requester name set, these will be automatically assigned.
109 virtual void sendMessage(DtBaseMessage& request);
110
111 //! \brief Gets the message handler for incoming message processing
112 //! \return Reference to the message handler used by this connector
113 //!
114 //! Provides access to the message handler that dispatches incoming messages.
115 //! Users should register their delegates with this handler to process
116 //! specific message types when they are received from the server.
118
119 //! \brief Registers a callback for successful connections
120 //! \param delegate The callback delegate to register
121 //!
122 //! Adds a delegate that will be called when a connection to the
123 //! RadarFX server is successfully established
124 virtual void addConnectedCallback(const DtConnectionDelegate& delegate);
125
126 //! \brief Unregisters a previously registered connection callback
127 //! \param delegate The callback delegate to unregister
128 //!
129 //! Removes a delegate from the list of callbacks that are called when
130 //! a connection is established
131 virtual void removeConnectedCallback(const DtConnectionDelegate& delegate);
132
133 //! \brief Registers a callback for disconnections
134 //! \param delegate The callback delegate to register
135 //!
136 //! Adds a delegate that will be called when the connection to the
137 //! RadarFX server is lost
138 virtual void addDisconnectedCallback(const DtConnectionDelegate& delegate);
139
140 //! \brief Unregisters a previously registered disconnection callback
141 //! \param delegate The callback delegate to unregister
142 //!
143 //! Removes a delegate from the list of callbacks that are called when
144 //! a disconnection occurs
146
147 //! \brief Gets the message factory used for message creation
148 //! \return Reference to the message factory
149 //!
150 //! Provides access to the message factory that creates and decodes messages.
151 //! Users can add custom message decoders to this factory to handle
152 //! application-specific message types.
154
155 //! \brief Generates a unique message ID
156 //! \return A unique identifier for a message
157 //!
158 //! Provides a unique ID for tracking message responses. This function is
159 //! automatically used to assign IDs to messages that don't have one set
160 //! (id field is 0) when sendMessage is called.
161 virtual unsigned int nextId();
162
163 //! \brief Gets a unique name for this client application
164 //! \return String containing a unique requester name
165 //!
166 //! Provides a unique name for the client derived from the IP address and
167 //! process ID of the running application. This name is automatically
168 //! assigned to messages that don't have a requester name set when
169 //! sendMessage is called.
170 virtual std::string& requesterName();
171
172 //! \brief Gets the default timeout for connection retry attempts
173 //! \return Default retry timeout in seconds
174 //!
175 //! Returns the time in seconds between reconnection attempts when
176 //! automatic retry is enabled. The default value is 5 seconds.
177 static double defaultRetryTimeout();
178
179 //! \brief Sets the default timeout for connection retry attempts
180 //! \param val New timeout value in seconds
181 //!
182 //! Sets the time in seconds between reconnection attempts when
183 //! automatic retry is enabled.
184 static void setDefaultRetryTimeout(double val);
185
186 //! \brief Gets the default heartbeat timeout
187 //! \return Default heartbeat timeout in seconds
188 //!
189 //! Returns the timeout for heartbeat messages used to detect
190 //! connection loss. The default value is 2.5 seconds.
191 static double defaultHeartbeatTimeout();
192
193 //! \brief Sets the default heartbeat timeout
194 //! \param val New timeout value in seconds
195 //!
196 //! Sets the timeout for heartbeat messages used to detect
197 //! connection loss.
198 static void setDefaultHeatbeatTimeout(double val);
199
200protected:
201 //! \brief Finds the IP address of the first available network device
202 //! \param found Set to true if an address was found, false otherwise
203 //! \return Network address of the first available device
204 //!
205 //! Searches for and returns the IP address of the first available
206 //! network device on the system
207 DtInetAddr findFirstNetworkDeviceAddress(bool& found) const;
208
209 //! \brief Message handler for processing incoming messages
211
212 //! \brief List of callbacks to invoke when connection is established
213 std::list<DtConnectionDelegate> myConnectionCallbacks;
214
215 //! \brief List of callbacks to invoke when connection is lost
216 std::list<DtConnectionDelegate> myDisconnectionCallbacks;
217
218 //! \brief Factory for creating and decoding messages
220
221 //! \brief Counter for generating unique message IDs
222 unsigned int myNextId;
223
224 //! \brief Unique name for this client instance
225 std::string myRequestorName;
226
227 //! \brief Thread-safe queue for outgoing messages
229
230 //! \brief Thread-safe queue for incoming messages
232
233 //! \brief Worker thread that handles network communication
234 DtThread* myWorkerThread;
235
236 //! \brief Socket for TCP communication with the server
237 DtInetTcpSocket* mySocket;
238
239 //! \brief Flag indicating whether to automatically retry connections
241
242 //! \brief Time in seconds between connection retry attempts
244
245 //! \brief Server address to connect to
246 std::string myAddress;
247
248 //! \brief Server port to connect to
250
251 //! \brief Worker thread function that runs network communication
252 //! \param data Pointer to the DtRadarFxConnector instance
253 //! \param stop Pointer to a flag indicating if the thread should stop
254 //!
255 //! This static function is executed by the worker thread to handle
256 //! network communication asynchronously
257 static void workerFunction(void* data, volatile bool* stop);
258
259 //! \brief Flag indicating if the worker thread should terminate
261
262 //! \brief Static storage for default heartbeat timeout
264
265 //! \brief Static storage for default retry timeout
267};
268} // namespace makRadarFx
Base class for all RadarFX messages.
Definition rfxMessage.h:35
Forward declaration of the delegate template class.
Definition delegate.h:186
Thread-safe queue for single reader/single writer communication.
Definition locklessQueue.h:26
static void setDefaultHeatbeatTimeout(double val)
Sets the default heartbeat timeout.
virtual unsigned int nextId()
Generates a unique message ID.
DtLocklessQueue< DtBaseMessage * > myOutgoingQueue
Thread-safe queue for outgoing messages.
Definition radarFxConnector.h:228
unsigned int myNextId
Counter for generating unique message IDs.
Definition radarFxConnector.h:222
DtInetTcpSocket * mySocket
Socket for TCP communication with the server.
Definition radarFxConnector.h:237
static void workerFunction(void *data, volatile bool *stop)
Worker thread function that runs network communication.
virtual void removeDisconnectedCallback(const DtConnectionDelegate &delegate)
Unregisters a previously registered disconnection callback.
virtual std::string & requesterName()
Gets a unique name for this client application.
virtual bool isOk()
Checks if the connection is valid.
virtual DtVreMessageFactory & messageFactory()
Gets the message factory used for message creation.
Definition radarFxConnector.h:153
virtual void sendMessage(DtBaseMessage &request)
Sends a message to the RadarFX server.
static double defaultHeartbeatTimeout()
Gets the default heartbeat timeout.
DtRadarFxConnector()
Default constructor.
virtual void addDisconnectedCallback(const DtConnectionDelegate &delegate)
Registers a callback for disconnections.
std::string myAddress
Server address to connect to.
Definition radarFxConnector.h:246
static double theDefaultHeatbeatTimeout
Static storage for default heartbeat timeout.
Definition radarFxConnector.h:263
virtual void tick()
Processes queued messages and updates internal state.
virtual void addConnectedCallback(const DtConnectionDelegate &delegate)
Registers a callback for successful connections.
virtual ~DtRadarFxConnector()
Virtual destructor.
double myRetryTimeout
Time in seconds between connection retry attempts.
Definition radarFxConnector.h:243
static double theDefaultRetryTimeout
Static storage for default retry timeout.
Definition radarFxConnector.h:266
std::string myRequestorName
Unique name for this client instance.
Definition radarFxConnector.h:225
bool myShouldQuit
Flag indicating if the worker thread should terminate.
Definition radarFxConnector.h:260
DtThread * myWorkerThread
Worker thread that handles network communication.
Definition radarFxConnector.h:234
DtVreMessageFactory myMessageFactory
Factory for creating and decoding messages.
Definition radarFxConnector.h:219
virtual void disconnect()
Disconnects from the RadarFX server.
std::list< DtConnectionDelegate > myConnectionCallbacks
List of callbacks to invoke when connection is established.
Definition radarFxConnector.h:213
static void setDefaultRetryTimeout(double val)
Sets the default timeout for connection retry attempts.
DtVreMessageHandler myMessageHandler
Message handler for processing incoming messages.
Definition radarFxConnector.h:210
virtual void removeConnectedCallback(const DtConnectionDelegate &delegate)
Unregisters a previously registered connection callback.
virtual void connect(const std::string &address, int port=DtRadarFXPort, bool attemptRetry=true)
Establishes a connection to a remote RadarFX server.
DtInetAddr findFirstNetworkDeviceAddress(bool &found) const
Finds the IP address of the first available network device.
DtLocklessQueue< DtBaseMessage * > myIncomingQueue
Thread-safe queue for incoming messages.
Definition radarFxConnector.h:231
virtual DtVreMessageHandler & messageHandler()
Gets the message handler for incoming message processing.
Definition radarFxConnector.h:117
bool myShouldRetryConnect
Flag indicating whether to automatically retry connections.
Definition radarFxConnector.h:240
static double defaultRetryTimeout()
Gets the default timeout for connection retry attempts.
DtDelegate< void, DtRadarFxConnector * > DtConnectionDelegate
Type definition for connection state change callback delegates.
Definition radarFxConnector.h:60
std::list< DtConnectionDelegate > myDisconnectionCallbacks
List of callbacks to invoke when connection is lost.
Definition radarFxConnector.h:216
int myPort
Server port to connect to.
Definition radarFxConnector.h:249
Factory for creating RadarFX message objects from encoded data.
Definition rfxMessageFactory.h:42
Manages message callbacks for different message types.
Definition rfxMessageHandler.h:41
Defines export macros for the RadarFx Connector library.
#define RFX_CONNECTOR_DLL
Export/import macro for non-Windows platforms (empty)
Definition export.h:25
Defines a thread-safe queue for single reader/single writer scenarios.
Definition radarFxConnectorDriver.h:21
const int DtRadarFXPort
The default port that the RadarFX server listens on.
Definition radarFxConnector.h:33
Defines a factory for creating and decoding RadarFX messages.
Defines a handler for RadarFX message callbacks.