![]() |
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.
|
The RoveCommUDP class is used to send and receive data over a UDP connection. More...
#include <RoveCommUDP.h>


Classes | |
| struct | CallbackEntry |
Public Member Functions | |
| RoveCommUDP () | |
| Construct a new RoveCommUDP::RoveCommUDP object. | |
| ~RoveCommUDP () | |
| Destroy the RoveCommUDP::RoveCommUDP object. | |
| 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 will continuously receive UDP packets and invoke the appropriate callback function. | |
| template<typename T> | |
| 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 times, as it uses sendmmsg() on Linux and sends the same packet to multiple destinations in one call. | |
| template<typename T> | |
| 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. | |
| template<typename T> | |
| 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 that have subscribed to this node. | |
| template<typename manifest::ManifestEntry Entry> | |
| ssize_t | Write (std::span< const EntryType< Entry > > spData) |
| Writes a packet, checked against the manifest. | |
| template<typename manifest::ManifestEntry Entry> | |
| ssize_t | Write (std::initializer_list< EntryType< Entry > > ilData) |
| template<typename manifest::ManifestEntry Entry> | |
| 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) |
| template<typename manifest::ManifestEntry Entry> | |
| 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) |
| template<typename T> | |
| 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: packets received by any other node never invoke it. | |
| template<typename manifest::ManifestEntry Entry> | |
| CallbackHandle | On (std::function< void(const RoveCommPacket< EntryType< Entry > > &)> fnCallback) |
| void | Clear (const CallbackHandle &stHandle) |
| Remove the one callback identified by a handle from On(). Other callbacks for the same data ID stay registered. Waits for a callback that is already running, so once this returns the removed callback can no longer run. An empty or already-removed handle is ignored. | |
| ssize_t | Subscribe (const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT) |
| ssize_t | Unsubscribe (const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT) |
| void | Close () |
| Close the UDP socket. This method is called when the RoveCommUDP object is destroyed. Or when the user calls the CloseUDPSocket method. | |
| IPS & | GetIPS () |
| Accessor for the Frame I P S private member. | |
Private Types | |
| template<typename T> | |
| using | CallbackMap = std::unordered_map<uint16_t, std::vector<CallbackEntry<T>>> |
| using | SubscriberInfo = std::pair<manifest::AddressEntry, int> |
| Private Types inherited from AutonomyThread< void > | |
| enum | AutonomyThreadState |
| enum | AutonomyThreadPriority |
Private Member Functions | |
| template<typename T> | |
| constexpr CallbackMap< T > & | GetCallbackMap () |
| template<typename T> | |
| void | RemoveCallback (const CallbackHandle &stHandle) |
| Remove the one callback identified by a handle from this node's map for payload type T. | |
| template<typename T> | |
| 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 ReceiveUDPPacketAndCallback function and is used to invoke the appropriate callback function based on the data id of the received packet. The callback function is invoked with the received packet and the client address. | |
| void | ReceiveAndCallback () |
| Receive a UDP packet and invoke the appropriate callback function. Since data types are not known at compile time, this function calls the appropriate ProcessPacket function based on the data type of the received packet. | |
| void | ThreadedContinuousCode () override |
| Initialize the thread that will continuously receive UDP packets and invoke the appropriate callback function. This is a component of the AutonomyThread class and is called when the thread is started. | |
| void | PooledLinearCode () override |
| This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method. It currently does nothing and is not needed in the current implementation of the RoveCommUDP class. | |
| Private Member Functions inherited from AutonomyThread< void > | |
| AutonomyThread () | |
| Construct a new Autonomy Thread object. | |
| virtual | ~AutonomyThread () |
| Destroy the Autonomy Thread object. If the parent object or main thread is destroyed or exited while this thread is still running, a race condition will occur. Stopping and joining the thread here insures that the main program can't exit if the user forgot to stop and join the thread. | |
| void | Start () |
| When this method is called, it starts a new thread that runs the code within the ThreadedContinuousCode method. This is the users main code that will run the important and continuous code for the class. | |
| void | RequestStop () |
| Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the thread to exit, if the user code is not written properly and contains WHILE statement or any other long-executing or blocking code, then the thread will not exit until the next iteration. | |
| void | Join () |
| Waits for thread to finish executing and then closes thread. This method will block the calling code until thread is finished. | |
| bool | Joinable () const |
| Check if the code within the thread and all pools created by it are finished executing and the thread is ready to be closed. | |
| AutonomyThreadState | GetThreadState () const |
| Accessor for the Threads State private member. | |
| IPS & | GetIPS () |
| Accessor for the Frame I P S private member. | |
| void | SetMainThreadPriority (AutonomyThreadPriority ePriority) |
| Set the OS priority for the main continuous thread. | |
| void | SetPoolThreadPriority (AutonomyThreadPriority ePriority) |
| Set the OS priority for the highly parallelized pool threads. | |
| void | RunPool (const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false) |
| When this method is called, it starts/adds tasks to a thread pool that runs nNumTasksToQueue copies of the code within the PooledLinearCode() method using nNumThreads number of threads. This is meant to be used as an internal utility of the child class to further improve parallelization. Default value for nNumThreads is 2. | |
| void | RunDetachedPool (const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false) |
| When this method is called, it starts a thread pool full of threads that don't return std::futures (like a placeholder for the thread return type). This means the thread will not have a return type and there is no way to determine if the thread has finished other than calling the Join() method. Only use this if you want to 'set and forget'. It will be faster as it doesn't return futures. Runs PooledLinearCode() method code. This is meant to be used as an internal utility of the child class to further improve parallelization. | |
| void | ParallelizeLoop (const int nNumThreads, const N tTotalIterations, F &&tLoopFunction) |
| Given a ref-qualified looping function and an arbitrary number of iterations, this method will divide up the loop and run each section in a thread pool. This function must not return anything. This method will block until the loop has completed. | |
| void | ClearPoolQueue () |
| Clears any tasks waiting to be ran in the queue, tasks currently running will remain running. | |
| void | JoinPool () |
| Waits for pool to finish executing tasks. This method will block the calling code until thread is finished. | |
| bool | PoolJoinable () const |
| Check if the internal pool threads are done executing code and the queue is empty. | |
| void | SetMainThreadIPSLimit (int nMaxIterationsPerSecond=0) |
| Mutator for the Main Thread Max I P S private member. | |
| int | GetPoolNumOfThreads () |
| Accessor for the Pool Num Of Threads private member. | |
| int | GetPoolQueueLength () |
| Accessor for the Pool Queue Size private member. | |
| std::vector< void > | GetPoolResults () |
| Accessor for the Pool Results private member. The action of getting results will destroy and remove them from this object. This method blocks if the thread is not finished, so no need to call JoinPool() before getting results. | |
| int | GetMainThreadMaxIPS () const |
| Accessor for the Main Thread Max I P S private member. | |
Private Attributes | |
| 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 |
| uint64_t | m_ullNextCallbackID = 1 |
| std::atomic_int | m_nUDPSocket |
| struct sockaddr_in | m_saUDPServerAddr |
| std::set< SubscriberInfo > | m_seSubscribers |
| std::shared_mutex | m_muCallbackMutex |
| std::mutex | m_muSocketSendMutex |
| std::mutex | m_muSocketReceiveMutex |
| Private Attributes inherited from AutonomyThread< void > | |
| IPS | m_IPS |
The RoveCommUDP class is used to send and receive data over a UDP connection.
|
private |
|
private |
| rovecomm::RoveCommUDP::RoveCommUDP | ( | ) |
Construct a new RoveCommUDP::RoveCommUDP object.

| rovecomm::RoveCommUDP::~RoveCommUDP | ( | ) |
Destroy the RoveCommUDP::RoveCommUDP object.

| void rovecomm::RoveCommUDP::Clear | ( | const CallbackHandle & | stHandle | ) |
Remove the one callback identified by a handle from On(). Other callbacks for the same data ID stay registered. Waits for a callback that is already running, so once this returns the removed callback can no longer run. An empty or already-removed handle is ignored.
| stHandle | - The handle On() returned for the callback. |


| void rovecomm::RoveCommUDP::Close | ( | ) |
Close the UDP socket. This method is called when the RoveCommUDP object is destroyed. Or when the user calls the CloseUDPSocket method.


|
inlineconstexprprivate |

|
inline |
Accessor for the Frame I P S private member.
| bool rovecomm::RoveCommUDP::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 will continuously receive UDP packets and invoke the appropriate callback function.
| nPort | - The port that the UDP socket is to be bound to. If set to 0, then the operating system will automatically assign to an available port. |


| CallbackHandle rovecomm::RoveCommUDP::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: packets received by any other node never invoke it.
| T | - The payload type of the packets. Must be one of the manifest's types. |
| unDataId | - The data ID that invokes the callback. |
| fnCallback | - The callback. It runs on this node's receive thread and must not call On() or Clear() on this node. |


|
inline |

|
overrideprivatevirtual |
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method. It currently does nothing and is not needed in the current implementation of the RoveCommUDP class.
Implements AutonomyThread< void >.

|
private |
Process a UDP packet and invoke the appropriate callback function. This function is called from the ReceiveUDPPacketAndCallback function and is used to invoke the appropriate callback function based on the data id of the received packet. The callback function is invoked with the received packet and the client address.
| T | - The type of data that the callback function will be invoked with. This can be any of the types defined in the manifest. |
| spData | - The received RoveCommData that is to be processed. |
| vCallbacks | - The list of callback functions that are to be invoked when a packet with the specified data id is received. |
| saClientAddr | - The address of the client that sent the packet. |


|
private |
Receive a UDP packet and invoke the appropriate callback function. Since data types are not known at compile time, this function calls the appropriate ProcessPacket function based on the data type of the received packet.


|
private |
Remove the one callback identified by a handle from this node's map for payload type T.
| T | - The payload type the handle was registered with. |
| stHandle | - The handle On() returned for the callback. |


|
inline |


|
overrideprivatevirtual |
Initialize the thread that will continuously receive UDP packets and invoke the appropriate callback function. This is a component of the AutonomyThread class and is called when the thread is started.
Implements AutonomyThread< void >.

|
inline |


|
inline |
Sends a packet to all subscribers. This is often used to send errors or telemetry data to all devices that have subscribed to this node.
| T | - The payload type of the packet. Must be one of the manifest's types. |
| stPacket | - The packet to be sent. |


|
inline |

|
inline |
Writes a packet, checked against the manifest.
| Entry | - |
| spData | - |

| template ssize_t rovecomm::RoveCommUDP::WriteAll< char > | ( | 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 times, as it uses sendmmsg() on Linux and sends the same packet to multiple destinations in one call.
| T | - The payload type of the packet. Must be one of the manifest's types. |
| stPacket | - The packet to be sent. |
| spDestinations | - A span of pairs of AddressEntry and port number, representing the destinations to send the packet to. |


|
inline |
Sends a packet to a specific destination. This is often used to send a command to a specific device.
| T | - The payload type of the packet. Must be one of the manifest's types. |
| stPacket | - The packet to be sent. |
| stIPAddress | - The IP address of the destination. |
| nPort | - The port number of the destination. |


|
inline |

|
inline |

|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |