VR-Engage  2.2
Loading...
Searching...
No Matches
networkForwarder.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file networkForwarder.h
7//! \brief Defines base class for VREngage message network forwarding
8//!
9//! This file contains the DtNetworkForwarder abstract base class and DtVreMessageBundle
10//! utility class for VREngage message forwarding across networks. The network forwarder
11//! handles bundling of messages for efficient transmission and provides mechanisms for
12//! forwarding specific message types. Derived classes implement the actual network
13//! transport specifics.
14
15#pragma once
16
20
21#include <string>
22
23namespace makVre
24{
26
27//! \brief Utility class for bundling multiple messages into a single network packet
28//!
29//! This class provides a buffer for bundling multiple VREngage messages together
30//! for efficient network transmission. It manages a resizable buffer and provides
31//! methods for adding messages, checking capacity, and tracking buffer usage.
33{
34 //! \brief Pointer to the buffer containing bundled messages
35 char* myBuffer;
36
37 //! \brief Current write position in the buffer
39
40 //! \brief Maximum size of the buffer in bytes
42
43 //! \brief Constructor
44 //! \param maxSize Maximum size of the buffer in bytes
45 //!
46 //! Allocates a new buffer of the specified size for bundling messages.
47 DtVreMessageBundle(int maxSize);
48
49 //! \brief Destructor
50 //!
51 //! Deallocates the buffer memory.
53
54 //! \brief Resets the write position to the beginning of the buffer
55 //!
56 //! Allows the buffer to be reused after its contents have been sent.
57 void reset();
58
59 //! \brief Gets the number of bytes currently used in the buffer
60 //! \return Number of bytes used
61 int used();
62
63 //! \brief Gets the number of bytes remaining in the buffer
64 //! \return Number of bytes remaining
66
67 //! \brief Checks if a message of the specified size can be added to the buffer
68 //! \param size Size of the message in bytes
69 //! \return True if the message can be added, false otherwise
70 bool canAdd(int size);
71
72 //! \brief Adds a message to the buffer
73 //! \param data Pointer to the message data
74 //! \param size Size of the message in bytes
75 //!
76 //! Copies the message data into the buffer and advances the write position.
77 void add(char* data, int size);
78};
79
80//! \brief Abstract base class for network forwarding of VREngage messages
81//!
82//! This class provides the foundation for forwarding VREngage messages across
83//! networks. It handles message bundling for efficient transmission and provides
84//! mechanisms for forwarding specific message types. Derived classes implement
85//! the actual network transport specifics by overriding the pure virtual flush()
86//! method and optionally the netSendMessage() method.
88{
89public:
90 //! \brief Constructor
91 //! \param factory Pointer to the message factory for creating messages from raw data
92 //!
93 //! Initializes a new network forwarder with the specified message factory.
94 //! The message factory is used to create messages from incoming raw data.
95 //! This constructor also initializes the first message bundle for outgoing messages.
97
98 //! \brief Virtual destructor
99 //!
100 //! Calls disable() to unregister message handlers and performs cleanup.
102
103 //! \brief Flushes bundled messages to the network
104 //!
105 //! This pure virtual method must be implemented by derived classes to send
106 //! accumulated message bundles over the network. Because messages are bundled
107 //! for efficiency, this method should be called once per frame to ensure
108 //! timely delivery of messages.
109 //!
110 //! \note This must be called once per frame to ensure messages are transmitted
111 virtual void flush() = 0;
112
113 //! \brief Enables the network forwarder
114 //! \return True if the forwarder was successfully enabled
115 //!
116 //! This method enables the network forwarder by registering message handlers
117 //! for forwarding control messages. When enabled, the forwarder will process
118 //! incoming control messages and forward outgoing messages of registered types.
119 //! It also initializes the unique ID for this forwarder instance.
120 virtual bool enable();
121
122 //! \brief Disables the network forwarder
123 //!
124 //! This method disables the network forwarder by unregistering all message
125 //! handlers. When disabled, the forwarder will not process incoming control
126 //! messages or forward outgoing messages.
127 virtual void disable();
128
129 //! \brief Starts forwarding messages of the specified type
130 //! \param messageType The type of messages to forward
131 //!
132 //! This method registers a handler for messages of the specified type
133 //! to forward them over the network. Messages of this type will be
134 //! bundled and sent when flush() is called.
135 virtual void forwardEvents(const std::string& messageType);
136
137 //! \brief Stops forwarding messages of the specified type
138 //! \param messageType The type of messages to stop forwarding
139 //!
140 //! This method unregisters the handler for messages of the specified type,
141 //! stopping their forwarding over the network.
142 virtual void stopForwardingEvents(const std::string& messageType);
143
144 //! \brief Sets the application name for this network forwarder
145 //! \param applicationName The name to set for this application instance
146 //!
147 //! This method sets the application name used to filter incoming forwarding
148 //! request messages. Only requests targeting this application name will be processed.
149 virtual void setApplicationName(const std::string& applicationName);
150
151 //! \brief Gets the current application name
152 //! \return The current application name
153 //!
154 //! Returns the application name previously set with setApplicationName().
155 virtual std::string applicationName();
156
157 //! \brief Gets the maximum size of a message bundle
158 //! \return Maximum bundle size in bytes
159 //!
160 //! This static method returns the maximum size of a message bundle,
161 //! which is calculated to fit within a single UDP packet including headers.
162 static int maxBundleSize();
163
164protected:
165 //! \brief Sends a message through the network
166 //! \param msg Pointer to the message to send
167 //! \return Message processing result (HANDLED or NOT_HANDLED)
168 //!
169 //! This method bundles the message for efficient network transmission. If the
170 //! message was not previously forwarded (originatingApplicationId is 0), it is
171 //! added to an appropriate message bundle and tagged with this forwarder's ID.
172 //! The actual transmission occurs when flush() is called.
173 //!
174 //! Derived classes may override this method to implement custom message handling.
176
177 //! \brief Handles ForwardMessage control messages
178 //! \param msg Pointer to the received message
179 //! \return Message processing result (HANDLED)
180 //!
181 //! This method processes ForwardMessage control messages, which request
182 //! forwarding of a specific message type. It calls forwardEvents() with
183 //! the specified message type.
185
186 //! \brief Handles StopForwardMessage control messages
187 //! \param msg Pointer to the received message
188 //! \return Message processing result (HANDLED)
189 //!
190 //! This method processes StopForwardMessage control messages, which request
191 //! stopping forwarding of a specific message type. It calls stopForwardingEvents()
192 //! with the specified message type.
194
195 //! \brief Handles RequestForwardMessage control messages
196 //! \param msg Pointer to the received message
197 //! \return Message processing result (HANDLED)
198 //!
199 //! This method processes RequestForwardMessage control messages, which request
200 //! forwarding of a specific message type from a specific host. If the request
201 //! is for this host (matching applicationName), it calls forwardEvents().
203
204 //! \brief Handles RequestStopForwardMessage control messages
205 //! \param msg Pointer to the received message
206 //! \return Message processing result (HANDLED)
207 //!
208 //! This method processes RequestStopForwardMessage control messages, which request
209 //! stopping forwarding of a specific message type from a specific host. If the request
210 //! is for this host (matching applicationName), it calls stopForwardingEvents().
212
213 //! \brief Pointer to the message factory for creating messages from raw data
215
216 //! \brief Unique identifier for this network forwarder instance
217 //!
218 //! This ID is used to tag outgoing messages to prevent processing loops
219 //! when messages are received from the network.
221
222 //! \brief Vector of message bundles for outgoing messages
223 //!
224 //! Messages are accumulated in these bundles until flush() is called.
225 //! New bundles are created as needed when existing ones are full.
226 std::vector<DtVreMessageBundle*> myMessageBundles;
227
228 //! \brief Application name for filtering forwarding request messages
229 //!
230 //! Only forwarding requests targeting this application name will be processed.
231 std::string myApplicationName;
232};
233
234} // namespace makVre
virtual void flush()=0
Flushes bundled messages to the network.
virtual makVre::DtVreMessageResult handleRequestForwardMessage(makVre::DtVreMessage *msg)
Handles RequestForwardMessage control messages.
virtual void setApplicationName(const std::string &applicationName)
Sets the application name for this network forwarder.
virtual void disable()
Disables the network forwarder.
virtual bool enable()
Enables the network forwarder.
virtual ~DtNetworkForwarder()
Virtual destructor.
virtual makVre::DtVreMessageResult handleRequestStopForwardMessage(makVre::DtVreMessage *msg)
Handles RequestStopForwardMessage control messages.
std::string myApplicationName
Application name for filtering forwarding request messages.
Definition networkForwarder.h:231
virtual std::string applicationName()
Gets the current application name.
virtual void forwardEvents(const std::string &messageType)
Starts forwarding messages of the specified type.
DtNetworkForwarder(DtVreMessageFactory *factory)
Constructor.
DtVreMessageFactory * myMessageFactory
Pointer to the message factory for creating messages from raw data.
Definition networkForwarder.h:214
virtual makVre::DtVreMessageResult netSendMessage(makVre::DtVreMessage *msg)
Sends a message through the network.
virtual makVre::DtVreMessageResult handleForwardMessage(makVre::DtVreMessage *msg)
Handles ForwardMessage control messages.
static int maxBundleSize()
Gets the maximum size of a message bundle.
UInt64 myId
Unique identifier for this network forwarder instance.
Definition networkForwarder.h:220
std::vector< DtVreMessageBundle * > myMessageBundles
Vector of message bundles for outgoing messages.
Definition networkForwarder.h:226
virtual void stopForwardingEvents(const std::string &messageType)
Stops forwarding messages of the specified type.
virtual makVre::DtVreMessageResult handleStopForwardMessage(makVre::DtVreMessage *msg)
Handles StopForwardMessage control messages.
Factory for creating VREngage messages from serialized data.
Definition vreMessageFactory.h:48
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
Defines export macros for the VREngage Message Manager library.
#define MESSAGEMANAGER_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
unsigned long long UInt64
Unsigned 64-bit integer type.
Definition utilFunctions.h:42
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
char * myWriteHead
Current write position in the buffer.
Definition networkForwarder.h:38
void reset()
Resets the write position to the beginning of the buffer.
int myMaxSize
Maximum size of the buffer in bytes.
Definition networkForwarder.h:41
char * myBuffer
Pointer to the buffer containing bundled messages.
Definition networkForwarder.h:35
~DtVreMessageBundle()
Destructor.
bool canAdd(int size)
Checks if a message of the specified size can be added to the buffer.
int bufferRemaining()
Gets the number of bytes remaining in the buffer.
DtVreMessageBundle(int maxSize)
Constructor.
void add(char *data, int size)
Adds a message to the buffer.
int used()
Gets the number of bytes currently used in the buffer.
Defines the base class for all VREngage messages.
Defines hierarchical message routing tree for the VREngage messaging system.