VR-Engage  2.2
Loading...
Searching...
No Matches
vreMessage.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6#pragma once
7
8//! \file vreMessage.h
9//! \ingroup managedInterface
10//! \brief Defines the base class for all VREngage messages
11//!
12//! This file contains the DtVreMessage abstract base class which serves as the foundation
13//! for all messages in the VREngage messaging system. It provides core functionality for
14//! message serialization/deserialization, identification, and lifecycle management.
15//! Messages are used for communication between components both within a single
16//! application instance and across the network.
20#include "vreUtil/byteStream.h"
21#include "vreutil/delegate.h"
22
23#include <string>
24#include <memory>
25
26namespace makVre
27{
28//! \brief Enumeration of possible message handling results
29//!
30//! These values are returned by message handlers to indicate how a message was processed
31//! and whether further processing should occur.
33{
34 IGNORED, //!< The handler did not process the message; continue processing
35 HANDLED, //!< The handler processed the message; continue processing
36 EATEN //!< The handler processed the message; stop further processing
37};
38
39//! \brief Abstract base class for all VREngage messages
40//!
41//! This class serves as the foundation for all messages in the VREngage messaging system.
42//! It provides core functionality for message serialization/deserialization, identification,
43//! and lifecycle management. All concrete message types must inherit from this class and
44//! implement the pure virtual methods.
45//!
46//! Messages contain a type identifier, serialization/deserialization mechanisms, versioning,
47//! and features to support network distribution including originating application ID tracking
48//! to prevent message loops.
50{
51public:
52 //! \brief Default constructor
53 //!
54 //! Initializes a new message with zero delay, zero application ID,
55 //! and version 1.
57
58 //! \brief Virtual destructor
59 //!
60 //! Ensures proper cleanup of derived message classes.
61 virtual ~DtVreMessage();
62
63 //! \brief Gets the string name of the message type
64 //! \return Reference to the message type string
65 //!
66 //! This pure virtual method must be implemented by derived classes to
67 //! return a unique string identifier for the message type. This is used
68 //! for message routing and identification.
69 virtual const std::string& type() const = 0;
70
71 //! \brief Gets the message ID object
72 //! \return Reference to the message ID object
73 //!
74 //! This pure virtual method must be implemented by derived classes to
75 //! return a unique identifier object for the message. The message ID
76 //! contains both the type name and a hash value for efficient routing.
77 virtual const DtVreMessageId& messageId() const = 0;
78
79 //! \brief Encodes the message as an array of bytes and returns the bytes
80 //! \return Newly allocated buffer containing the serialized message
81 //!
82 //! This method serializes the message into a newly allocated buffer and
83 //! returns a pointer to that buffer. The caller is responsible for deleting
84 //! the returned buffer when it is no longer needed.
85 //!
86 //! \note This method allocates memory that must be freed by the caller
87 virtual char* encode();
88
89 //! \brief Encodes the message into the given buffer of bytes
90 //! \param buffer Pointer to the buffer to write into
91 //! \param bufferSize Size of the buffer in bytes
92 //!
93 //! This method serializes the message into the provided buffer. The buffer
94 //! must be at least as large as messageSize() bytes.
95 //!
96 //! \note This method assumes bufferSize >= messageSize()
97 virtual void encode(char* buffer, int bufferSize);
98
99 //! \brief Static decode function pattern for derived classes
100 //! \note Each derived class must implement a static decode function with the signature:
101 //! \code
102 //! static DtVreMessage* decode(unsigned char* buffer, int length);
103 //! \endcode
104 //! This function is used by the message factory to create messages from raw data.
105
106 //! \brief Gets the total size of the serialized message in bytes
107 //! \return Size of the message in bytes
108 //!
109 //! This method calculates the total size of the message when serialized,
110 //! including the base message header (message size, type hash, application ID,
111 //! version, and delay) and any data added by derived classes.
112 //!
113 //! Derived classes should override this method to include their own data size
114 //! while calling the base class implementation.
115 virtual int messageSize() const;
116
117 //! \brief Gets the ID of the application that created this message
118 //! \return Application ID value
119 //!
120 //! This method returns the unique identifier of the application instance
121 //! that created this message. It is used by network forwarders to prevent
122 //! message loops when distributing messages across networks.
123 //!
124 //! The ID defaults to 0 when a new message is created and is set by the
125 //! network forwarder when sending a message.
126 virtual const UInt64 originatingApplicationId() const;
127
128 //! \brief Sets the ID of the application that created this message
129 //! \param appId The application ID to set
130 //!
131 //! This method sets the unique identifier of the application instance
132 //! that created this message. It is primarily used by network forwarders
133 //! to tag outgoing messages with the local application ID.
135
136 //! \brief Sets a delay in seconds before the message is dispatched
137 //! \param delay Delay time in seconds
138 //!
139 //! This method sets a delay time that must elapse before the message
140 //! is processed by the message manager. It allows for scheduled message
141 //! delivery.
142 //!
143 //! \note This is an internal API, not to be called by users directly
144 virtual void setDelay(double delay);
145
146 //! \brief Checks if the message has remaining delay time
147 //! \return True if there is any delay remaining, false otherwise
148 //!
149 //! This method checks if the message still has remaining delay time
150 //! before it should be processed.
151 //!
152 //! \note This is an internal API, not to be called by users directly
153 virtual bool isDelayed();
154
155 //! \brief Reduces the remaining delay time
156 //! \param dt Time in seconds to subtract from the delay
157 //!
158 //! This method reduces the remaining delay time by the specified amount.
159 //! It is called by the message manager during each update cycle to track
160 //! delayed messages.
161 //!
162 //! \note This is an internal API, not to be called by users directly
163 virtual void reduceDelay(double dt);
164
165 //! \brief Gets the version number of this message
166 //! \return Version number
167 //!
168 //! This method returns the version number of the message, which can be
169 //! used for backward compatibility and migration between different versions
170 //! of the messaging system. The version number may be a bitfield containing
171 //! various pieces of information.
172 virtual const UInt64 versionNumber() const;
173
174protected:
175 //! \brief Serializes the message to a stream writer
176 //! \param writer Reference to the stream writer
177 //!
178 //! This method writes the message header fields to the stream writer.
179 //! Derived classes should override this method to add their own fields
180 //! while calling the base class implementation.
181 virtual void serialize(DtStreamWriter& writer) const;
182
183 //! \brief Deserializes the message from a stream reader
184 //! \param reader Reference to the stream reader
185 //!
186 //! This method reads the message header fields from the stream reader.
187 //! Derived classes should override this method to read their own fields
188 //! while calling the base class implementation.
189 virtual void deserialize(DtStreamReader& reader);
190
191 //! \brief Delay time in seconds before the message should be processed
192 double myDelay;
193
194 //! \brief ID of the application that created this message
196
197 //! \brief Version number of this message
199};
200
201//! \brief Shared pointer type for DtVreMessage objects
202//!
203//! This type defines a shared pointer to a DtVreMessage object, providing
204//! automatic memory management for messages.
205using DtVreMessagePtr = std::shared_ptr<DtVreMessage>;
206
207//! \brief Delegate type for message handler callbacks
208//!
209//! This type defines a delegate that takes a DtVreMessage pointer and
210//! returns a DtVreMessageResult value. It is used to register message
211//! handlers with the message manager.
213
214} // namespace makVre
Modern delegate class that can bind and invoke callables with any number of parameters.
Definition delegate.h:31
Reader class for decoding values from a byte array.
Definition byteStream.h:42
Writer class for encoding values into a byte array.
Definition byteStream.h:161
virtual void deserialize(DtStreamReader &reader)
Deserializes the message from a stream reader.
virtual const DtVreMessageId & messageId() const =0
Gets the message ID object.
double myDelay
Delay time in seconds before the message should be processed.
Definition vreMessage.h:192
virtual char * encode()
Encodes the message as an array of bytes and returns the bytes.
virtual bool isDelayed()
Checks if the message has remaining delay time.
virtual void setOriginatingApplicationId(UInt64 appId)
Sets the ID of the application that created this message.
UInt64 myApplicationId
ID of the application that created this message.
Definition vreMessage.h:195
UInt64 myVersion
Version number of this message.
Definition vreMessage.h:198
virtual const std::string & type() const =0
Gets the string name of the message type.
virtual void encode(char *buffer, int bufferSize)
Encodes the message into the given buffer of bytes.
virtual void setDelay(double delay)
Sets a delay in seconds before the message is dispatched.
virtual const UInt64 versionNumber() const
Gets the version number of this message.
virtual const UInt64 originatingApplicationId() const
Gets the ID of the application that created this message.
virtual ~DtVreMessage()
Virtual destructor.
virtual void serialize(DtStreamWriter &writer) const
Serializes the message to a stream writer.
virtual void reduceDelay(double dt)
Reduces the remaining delay time.
DtVreMessage()
Default constructor.
virtual int messageSize() const
Static decode function pattern for derived classes.
Message identifier class for the VREngage messaging system.
Definition vreMessageId.h:35
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
DtDelegate< DtVreMessageResult, DtVreMessage * > DtVreMessageDelegate
Delegate type for message handler callbacks.
Definition vreMessage.h:212
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
@ HANDLED
The handler processed the message; continue processing.
Definition vreMessage.h:35
@ EATEN
The handler processed the message; stop further processing.
Definition vreMessage.h:36
@ IGNORED
The handler did not process the message; continue processing.
Definition vreMessage.h:34
std::shared_ptr< DtVreMessage > DtVreMessagePtr
Shared pointer type for DtVreMessage objects.
Definition vreMessage.h:205
Provides common utility functions for the VREngage system.
Provides binary serialization and deserialization capabilities.
Defines message identifier class for the VREngage messaging system.