VR-Engage  2.2
Loading...
Searching...
No Matches
udpNetworkForwarder.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file udpNetworkForwarder.h
7//! \brief Defines UDP-based network forwarder for VREngage messages
8//!
9//! This file contains the DtUdpNetworkForwarder class which implements message
10//! forwarding through UDP sockets. It extends the DtNetworkForwarder base class
11//! to provide UDP-specific network communication for distributing VREngage messages
12//! across networks, handling both message sending and receiving.
13
14#pragma once
15
19
20#include <vlutil/vlInetUdpSocket.h>
21
22#include <string>
23
24namespace makVre
25{
26
28
29//! \brief UDP-based implementation of network forwarder for VREngage messages
30//!
31//! This class implements a network forwarder that uses UDP sockets to send and
32//! receive VREngage messages over the network. It manages a UDP socket bound to
33//! a specific device and port, sending messages to a configured destination address.
34//! The forwarder receives incoming messages, processes them, and forwards them to
35//! the message manager for handling.
37{
38public:
39 //! \brief Constructor
40 //! \param factory Pointer to the message factory for creating messages from raw data
41 //! \param deviceAddress Network interface address to bind to (e.g., "0.0.0.0" for all interfaces)
42 //! \param destinationAddress Destination address for outgoing messages (can be multicast/broadcast)
43 //! \param port UDP port number for both sending and receiving
44 //!
45 //! Initializes a new UDP network forwarder with the specified message factory and
46 //! network parameters. The forwarder binds to the specified device address and port
47 //! for receiving messages and sends messages to the specified destination address.
49 DtVreMessageFactory* factory, std::string deviceAddress, std::string destinationAddress, int port);
50
51 //! \brief Virtual destructor
52 //!
53 //! Cleans up resources, including the UDP socket if it exists.
54 virtual ~DtUdpNetworkForwarder() override;
55
56 //! \brief Flushes bundled messages to the network via UDP
57 //!
58 //! This method sends all accumulated message bundles to the network through
59 //! the UDP socket. It checks that the socket is valid and operational before
60 //! sending. Each bundle with content is sent as a single UDP packet to the
61 //! configured destination address and port.
62 //!
63 //! Implements the pure virtual method from DtNetworkForwarder.
64 virtual void flush() override;
65
66 //! \brief Enables the UDP network forwarder
67 //! \return True if the forwarder was successfully enabled, false otherwise
68 //!
69 //! This method enables the UDP network forwarder by creating and configuring
70 //! the UDP socket. It configures the socket with the specified device address,
71 //! destination address, and port. The socket is set to non-blocking mode and
72 //! opened for communication. If the socket cannot be opened, the method returns false.
73 //!
74 //! Overrides the base class implementation to add UDP-specific initialization.
75 virtual bool enable() override;
76
77 //! \brief Disables the UDP network forwarder
78 //!
79 //! This method disables the UDP network forwarder by closing and deleting
80 //! the UDP socket. After this method is called, no more messages will be
81 //! sent or received until enable() is called again.
82 //!
83 //! Overrides the base class implementation to add UDP-specific cleanup.
84 virtual void disable() override;
85
86 //! \brief Processes incoming UDP messages
87 //! \param dt Delta time in seconds since the last tick (not used)
88 //!
89 //! This method polls the UDP socket for incoming messages, processes them, and
90 //! forwards them to the message manager. It continues reading from the socket
91 //! until there are no more messages available or an error occurs. If the socket
92 //! becomes invalid, it is closed and deleted.
93 //!
94 //! This method should be called regularly (e.g., once per frame) to ensure
95 //! timely processing of incoming messages.
96 virtual void tick(double dt);
97
98protected:
99 //! \brief Processes a received UDP packet containing VREngage messages
100 //! \param buffer Pointer to the buffer containing the received data
101 //! \param length Length of the received data in bytes
102 //! \param from Endpoint information about the sender
103 //!
104 //! This method extracts VREngage messages from a received UDP packet and
105 //! queues them for processing by the message manager. It iterates through
106 //! the buffer, creating messages using the message factory, and filtering out
107 //! messages that originated from this forwarder to prevent loops.
108 virtual void handleBuffer(char* buffer, int length, DtInetEndpoint& from);
109
110 //! \brief Network interface address to bind to
111 //!
112 //! This is the address of the network interface on which the UDP socket
113 //! will listen for incoming messages (e.g., "0.0.0.0" for all interfaces).
114 std::string myDeviceAddress;
115
116 //! \brief Destination address for outgoing messages
117 //!
118 //! This is the address to which outgoing messages will be sent.
119 //! It can be a multicast or broadcast address to reach multiple receivers.
121
122 //! \brief UDP port number for both sending and receiving
124
125 //! \brief Pointer to the UDP socket used for communication
126 //!
127 //! This socket is created in enable() and destroyed in disable().
128 //! It is used for both sending outgoing messages and receiving incoming messages.
129 DtInetUdpSocket* mySocket;
130};
131
132} // namespace makVre
DtNetworkForwarder(DtVreMessageFactory *factory)
Constructor.
virtual void flush() override
Flushes bundled messages to the network via UDP.
DtInetUdpSocket * mySocket
Pointer to the UDP socket used for communication.
Definition udpNetworkForwarder.h:129
virtual ~DtUdpNetworkForwarder() override
Virtual destructor.
DtUdpNetworkForwarder(DtVreMessageFactory *factory, std::string deviceAddress, std::string destinationAddress, int port)
Constructor.
virtual bool enable() override
Enables the UDP network forwarder.
virtual void tick(double dt)
Processes incoming UDP messages.
int myPort
UDP port number for both sending and receiving.
Definition udpNetworkForwarder.h:123
std::string myDestinationAddress
Destination address for outgoing messages.
Definition udpNetworkForwarder.h:120
virtual void disable() override
Disables the UDP network forwarder.
std::string myDeviceAddress
Network interface address to bind to.
Definition udpNetworkForwarder.h:114
virtual void handleBuffer(char *buffer, int length, DtInetEndpoint &from)
Processes a received UDP packet containing VREngage messages.
Factory for creating VREngage messages from serialized data.
Definition vreMessageFactory.h:48
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
Defines base class for VREngage message network forwarding.
Defines hierarchical message routing tree for the VREngage messaging system.