RoveComm C++ 3.0.0
Welcome to the RoveComm C++ repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
Loading...
Searching...
No Matches
RoveCommUDP.h
Go to the documentation of this file.
1
11
12#ifndef ROVECOMM_UDP_H
13#define ROVECOMM_UDP_H
14
15#include "ExternalIncludes.h"
16#include "RoveCommConsts.h"
17#include "RoveCommManifest.h"
18#include "RoveCommPacket.h"
19
21#include <atomic>
22#include <csignal>
23#include <cstring>
24#include <functional>
25#include <iostream>
26#include <shared_mutex>
27#include <set>
28#include <tuple>
29#include <unordered_map>
30#include <vector>
31
33
34
42namespace rovecomm
43{
44
52 {
53 private:
54#if defined(__ROVECOMM_WINDOWS_MODE__) && __ROVECOMM_WINDOWS_MODE__ == 1
55 // Windows specific private member variables.
56 HANDLE m_stdIOCP;
57#endif
58
59 // One callback registered with On(). The ID lets Clear() remove exactly this callback.
60 template<typename T>
62 {
63 uint64_t ullID;
64 std::function<void(const RoveCommPacket<T>&)> fnCallback;
65 };
66
67 // This node's callbacks for one payload type, by data ID.
68 template<typename T>
69 using CallbackMap = std::unordered_map<uint16_t, std::vector<CallbackEntry<T>>>;
70
71 // This node's callbacks, one map per payload type. They belong to this instance, so two nodes never
72 // share callbacks, and m_muCallbackMutex guards every map.
73 std::tuple<CallbackMap<int8_t>,
83 uint64_t m_ullNextCallbackID = 1; // Guarded by m_muCallbackMutex. 0 marks an empty CallbackHandle.
84
85 template<typename T>
87 {
88 return std::get<CallbackMap<T>>(m_tpCallbackMaps);
89 }
90
91 template<typename T>
92 void RemoveCallback(const CallbackHandle& stHandle);
93
94 // Private member variables
95 std::atomic_int m_nUDPSocket;
96 struct sockaddr_in m_saUDPServerAddr;
97 using SubscriberInfo = std::pair<manifest::AddressEntry, int>;
98 std::set<SubscriberInfo> m_seSubscribers;
99 std::shared_mutex m_muCallbackMutex;
102
103#if defined(__ROVECOMM_TESTS_MODE__) && __ROVECOMM_TESTS_MODE__ == 1
104 public:
105#endif
106
107 // Packet processing functions
108 template<typename T>
109 void ProcessPacket(std::span<const uint8_t> stData, const sockaddr_in& saClientAddr);
110 void ReceiveAndCallback();
111
112 private:
113 // AutonomyThread member functions
114 void ThreadedContinuousCode() override;
115 void PooledLinearCode() override;
116
117 public:
118 // Constructor
119 RoveCommUDP();
120 // Destructor
121 ~RoveCommUDP();
122
123 // Initialization
125
126 // Data transmission
127 template<typename T>
128 ssize_t WriteAll(const RoveCommPacket<T>& stPacket, std::span<const std::pair<manifest::AddressEntry, int>> spDestinations);
129
130
142 template<typename T>
143 ssize_t WriteTo(const RoveCommPacket<T>& stPacket, const manifest::AddressEntry& stIPAddress, int nPort = manifest::General::ETHERNET_UDP_PORT)
144 {
145 std::pair<manifest::AddressEntry, int> stdDestination{stIPAddress, nPort};
146 return WriteAll(stPacket, std::span<const std::pair<manifest::AddressEntry, int>>{&stdDestination, 1});
147 }
148
149
159 template<typename T>
160 ssize_t Write(const RoveCommPacket<T>& stPacket)
161 {
162 // std::set is non-contiguous, so we need to copy the subscribers into a contiguous vector before passing it to WriteAll().
163 // In C++26 we will have std::flat_set, which will allow us to avoid this copy.
164 std::vector<std::pair<manifest::AddressEntry, int>> vDestinations(m_seSubscribers.begin(), m_seSubscribers.end());
165 return WriteAll(stPacket, vDestinations);
166 }
167
168
178 template<typename manifest::ManifestEntry Entry>
179 ssize_t Write(std::span<const EntryType<Entry>> spData)
180 {
181 return Write(rovecomm::CreatePacket<Entry>(spData));
182 }
183
184 template<typename manifest::ManifestEntry Entry>
185 ssize_t Write(std::initializer_list<EntryType<Entry>> ilData)
186 {
187 // Necessary because std::initializer_list cannot be deduced as std::span for some stupid reason: https://isocpp.org/files/papers/P4144R1.html
188 return Write(rovecomm::CreatePacket<Entry>(ilData));
189 }
190
191 template<typename manifest::ManifestEntry Entry>
192 ssize_t WriteTo(std::span<const EntryType<Entry>> spData,
193 const manifest::AddressEntry& stIPAddress = manifest::Helpers::FindBoardById(Entry.DATA_ID).ADDRESS,
195 {
196 return WriteTo(rovecomm::CreatePacket<Entry>(spData), stIPAddress, nPort);
197 }
198
199 template<typename manifest::ManifestEntry Entry>
200 ssize_t WriteTo(std::initializer_list<EntryType<Entry>> ilData,
201 const manifest::AddressEntry& stIPAddress = manifest::Helpers::FindBoardById(Entry.DATA_ID).ADDRESS,
203 {
204 return WriteTo(rovecomm::CreatePacket<Entry>(ilData), stIPAddress, nPort);
205 }
206
207 // Callback management functions
208
209 template<typename T>
210 CallbackHandle On(const uint16_t unDataId, std::function<void(const RoveCommPacket<T>&)> fnCallback);
211
212 template<typename manifest::ManifestEntry Entry>
213 CallbackHandle On(std::function<void(const RoveCommPacket<EntryType<Entry>>&)> fnCallback)
214 {
215 return On(Entry.DATA_ID, fnCallback);
216 }
217
218 void Clear(const CallbackHandle& stHandle);
219
221 {
222 return WriteTo(RoveCommPacket<uint8_t>{.unDataId = manifest::System::SUBSCRIBE_DATA_ID, .vData = {}}, stIPAddress, nPort);
223 }
224
226 {
227 return WriteTo(RoveCommPacket<uint8_t>{.unDataId = manifest::System::UNSUBSCRIBE_DATA_ID, .vData = {}}, stIPAddress, nPort);
228 }
229
230 // Deinitialization
231 void Close();
232
233 // Selectively make inherited method public so we can get RoveCommNode FPS.
235 };
236
237} // namespace rovecomm
238
239#endif // ROVECOMM_UDP_H
ExternalIncludes.h is a file that is included in the RoveComm library to include external dependencie...
Defines Global Constants within RoveComm C++.
RoveComm Manifest.
RoveComm Packet Definition.
AutonomyThread()
Definition AutonomyThread.hpp:75
IPS & GetIPS()
Accessor for the Frame I P S private member.
Definition AutonomyThread.hpp:242
std::set< SubscriberInfo > m_seSubscribers
Definition RoveCommUDP.h:98
void ProcessPacket(std::span< const uint8_t > stData, const sockaddr_in &saClientAddr)
Process a UDP packet and invoke the appropriate callback function. This function is called from the R...
Definition RoveCommUDP.cpp:433
~RoveCommUDP()
Destroy the RoveCommUDP::RoveCommUDP object.
Definition RoveCommUDP.cpp:91
ssize_t WriteTo(const RoveCommPacket< T > &stPacket, const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Sends a packet to a specific destination. This is often used to send a command to a specific device.
Definition RoveCommUDP.h:143
ssize_t WriteTo(std::span< const EntryType< Entry > > spData, const manifest::AddressEntry &stIPAddress=manifest::Helpers::FindBoardById(Entry.DATA_ID).ADDRESS, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:192
void Close()
Close the UDP socket. This method is called when the RoveCommUDP object is destroyed....
Definition RoveCommUDP.cpp:696
bool Init(int nPort=manifest::General::ETHERNET_UDP_PORT)
Initialize the UDP socket and bind it to the specified port. This method also starts the thread that ...
Definition RoveCommUDP.cpp:110
std::mutex m_muSocketSendMutex
Definition RoveCommUDP.h:100
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for UDP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommUDP.cpp:333
ssize_t Write(const RoveCommPacket< T > &stPacket)
Sends a packet to all subscribers. This is often used to send errors or telemetry data to all devices...
Definition RoveCommUDP.h:160
RoveCommUDP()
Construct a new RoveCommUDP::RoveCommUDP object.
Definition RoveCommUDP.cpp:75
void RemoveCallback(const CallbackHandle &stHandle)
Remove the one callback identified by a handle from this node's map for payload type T.
Definition RoveCommUDP.cpp:391
std::mutex m_muSocketReceiveMutex
Definition RoveCommUDP.h:101
void ThreadedContinuousCode() override
Initialize the thread that will continuously receive UDP packets and invoke the appropriate callback ...
Definition RoveCommUDP.cpp:665
std::pair< manifest::AddressEntry, int > SubscriberInfo
Definition RoveCommUDP.h:97
constexpr CallbackMap< T > & GetCallbackMap()
Definition RoveCommUDP.h:86
ssize_t Subscribe(const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:220
void Clear(const CallbackHandle &stHandle)
Remove the one callback identified by a handle from On(). Other callbacks for the same data ID stay r...
Definition RoveCommUDP.cpp:357
ssize_t WriteAll(const RoveCommPacket< T > &stPacket, std::span< const std::pair< manifest::AddressEntry, int > > spDestinations)
Sends a packet to all specified destinations. This is more efficient than using WriteTo() multiple ti...
Definition RoveCommUDP.cpp:211
ssize_t Write(std::initializer_list< EntryType< Entry > > ilData)
Definition RoveCommUDP.h:185
std::shared_mutex m_muCallbackMutex
Definition RoveCommUDP.h:99
CallbackHandle On(std::function< void(const RoveCommPacket< EntryType< Entry > > &)> fnCallback)
Definition RoveCommUDP.h:213
void ReceiveAndCallback()
Receive a UDP packet and invoke the appropriate callback function. Since data types are not known at ...
Definition RoveCommUDP.cpp:481
uint64_t m_ullNextCallbackID
Definition RoveCommUDP.h:83
std::atomic_int m_nUDPSocket
Definition RoveCommUDP.h:95
ssize_t Unsubscribe(const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:225
void PooledLinearCode() override
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method....
Definition RoveCommUDP.cpp:684
std::unordered_map< uint16_t, std::vector< CallbackEntry< T > > > CallbackMap
Definition RoveCommUDP.h:69
std::tuple< CallbackMap< int8_t >, CallbackMap< uint8_t >, CallbackMap< int16_t >, CallbackMap< uint16_t >, CallbackMap< int32_t >, CallbackMap< uint32_t >, CallbackMap< float >, CallbackMap< double >, CallbackMap< char > > m_tpCallbackMaps
Definition RoveCommUDP.h:82
ssize_t Write(std::span< const EntryType< Entry > > spData)
Writes a packet, checked against the manifest.
Definition RoveCommUDP.h:179
ssize_t WriteTo(std::initializer_list< EntryType< Entry > > ilData, const manifest::AddressEntry &stIPAddress=manifest::Helpers::FindBoardById(Entry.DATA_ID).ADDRESS, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:200
struct sockaddr_in m_saUDPServerAddr
Definition RoveCommUDP.h:96
const int ETHERNET_UDP_PORT
Definition RoveCommManifest.h:889
const BoardEntry & FindBoardById(uint16_t unDataId)
Definition RoveCommManifest.h:1202
const int UNSUBSCRIBE_DATA_ID
Definition RoveCommManifest.h:906
const int SUBSCRIBE_DATA_ID
Definition RoveCommManifest.h:905
The RoveComm namespace contains all of the functionality for the RoveComm library....
Definition RoveCommConsts.h:23
typename manifest::Helpers::RoveCommToCType< Entry.DATA_TYPE >::c_type EntryType
Definition RoveCommPacket.h:114
RoveCommPacket< EntryType< Entry > > CreatePacket()
Definition RoveCommPacket.h:117
IP Address Object for RoveComm. Default is 0.0.0.0.
Definition RoveCommManifest.h:63
Identifies one callback registered with a node's On(), so Clear() can remove that callback without to...
Definition RoveCommPacket.h:104
The RoveCommPacket struct is used to store data in a packet format for transmission over a network....
Definition RoveCommPacket.h:75
Definition RoveCommUDP.h:62
uint64_t ullID
Definition RoveCommUDP.h:63
std::function< void(const RoveCommPacket< T > &)> fnCallback
Definition RoveCommUDP.h:64