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
RoveCommTCP.h
Go to the documentation of this file.
1
12
13#ifndef ROVECOMM_TCP_H
14#define ROVECOMM_TCP_H
15
16#include "ExternalIncludes.h"
17#include "RoveCommConsts.h"
18#include "RoveCommManifest.h"
19#include "RoveCommPacket.h"
20
22#include <atomic>
23#include <csignal>
24#include <cstring>
25#include <functional>
26#include <iostream>
27#include <shared_mutex>
28#include <tuple>
29#include <unordered_map>
30#include <vector>
31
33
34
42namespace rovecomm
43{
44
52 {
53 private:
54 // One callback registered with On(). The ID lets Clear() remove exactly this callback.
55 template<typename T>
57 {
58 uint64_t ullID;
59 std::function<void(const RoveCommPacket<T>&)> fnCallback;
60 };
61
62 // This node's callbacks for one payload type, by data ID.
63 template<typename T>
64 using CallbackMap = std::unordered_map<uint16_t, std::vector<CallbackEntry<T>>>;
65
66 // This node's callbacks, one map per payload type. They belong to this instance, so two nodes never
67 // share callbacks, and m_muCallbackMutex guards every map.
68 std::tuple<CallbackMap<int8_t>,
78 uint64_t m_ullNextCallbackID = 1; // Guarded by m_muCallbackMutex. 0 marks an empty CallbackHandle.
79
80 template<typename T>
82 {
83 return std::get<CallbackMap<T>>(m_tpCallbackMaps);
84 }
85
86 template<typename T>
87 void RemoveCallback(const CallbackHandle& stHandle);
88
89 // Private member variables
90 std::atomic_int m_nTCPSocket;
91 struct sockaddr_in m_saTCPServerAddr;
93 struct sockaddr_in m_saClientAddr;
94 std::shared_mutex m_muCallbackMutex;
96
97#if defined(__ROVECOMM_TESTS_MODE__) && __ROVECOMM_TESTS_MODE__ == 1
98 public:
99#endif
100
101 // Packet processing functions
102 template<typename T>
103 void ProcessPacket(std::span<const uint8_t> stData);
104 void ReceiveAndCallback();
105
106 private:
107 // AutonomyThread member functions
108 void ThreadedContinuousCode() override;
109 void PooledLinearCode() override;
110
111 public:
112 // Constructor
113 RoveCommTCP();
114 // Destructor
115 ~RoveCommTCP();
116
117 // Initialization
118 bool Init(const manifest::AddressEntry& stIPAddress = {0, 0, 0, 0}, int nPort = manifest::General::ETHERNET_TCP_PORT);
119
120 // Data transmission
121 template<typename T>
122 ssize_t WriteTo(const RoveCommPacket<T>& stPacket, const manifest::AddressEntry& stIPAddress, int nPort = manifest::General::ETHERNET_UDP_PORT);
123
124 template<typename manifest::ManifestEntry Entry>
125 ssize_t WriteTo(std::span<const EntryType<Entry>> spData,
126 const manifest::AddressEntry& stIPAddress = manifest::Helpers::FindBoardById(Entry.DATA_ID).ADDRESS,
128 {
129 return WriteTo(rovecomm::CreatePacket<Entry>(spData), stIPAddress, nPort);
130 }
131
132 template<typename manifest::ManifestEntry Entry>
133 ssize_t WriteTo(std::initializer_list<EntryType<Entry>> ilData,
134 const manifest::AddressEntry& stIPAddress = manifest::Helpers::FindBoardById(Entry.DATA_ID).ADDRESS,
136 {
137 return WriteTo(rovecomm::CreatePacket<Entry>(ilData), stIPAddress, nPort);
138 }
139
140 // Callback management
141 template<typename T>
142 CallbackHandle On(const uint16_t unDataId, std::function<void(const RoveCommPacket<T>&)> fnCallback);
143
144 template<typename manifest::ManifestEntry Entry>
145 CallbackHandle On(std::function<void(const RoveCommPacket<EntryType<Entry>>&)> fnCallback)
146 {
147 return On(Entry.DATA_ID, fnCallback);
148 }
149
150 void Clear(const CallbackHandle& stHandle);
151
152 // Deinitialization
153 void Close();
154
155 // Selectively make inherited method public so we can get RoveCommNode FPS.
157 };
158} // namespace rovecomm
159
160#endif // ROVECOMM_TCP_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
void PooledLinearCode() override
The pooled linear code for the TCP class. This method is not implemented and will do nothing.
Definition RoveCommTCP.cpp:451
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 RoveCommTCP.cpp:251
void Close()
Closes the TCP socket.
Definition RoveCommTCP.cpp:459
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 RoveCommTCP.h:77
void ProcessPacket(std::span< const uint8_t > stData)
Processes a received packet and invokes the appropriate callback function from the vector of TCP call...
Definition RoveCommTCP.cpp:329
struct sockaddr_in m_saClientAddr
Definition RoveCommTCP.h:93
std::unordered_map< uint16_t, std::vector< CallbackEntry< T > > > CallbackMap
Definition RoveCommTCP.h:64
struct sockaddr_in m_saTCPServerAddr
Definition RoveCommTCP.h:91
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 RoveCommTCP.h:125
std::atomic_int m_nTCPSocket
Definition RoveCommTCP.h:90
std::atomic_int m_nCurrentTCPClientSocket
Definition RoveCommTCP.h:92
void ThreadedContinuousCode() override
The threaded continuous code for the TCP class. This method calls the ReceiveTCPPacketAndCallback met...
Definition RoveCommTCP.cpp:436
void RemoveCallback(const CallbackHandle &stHandle)
Remove the one callback identified by a handle from this node's map for payload type T.
Definition RoveCommTCP.cpp:285
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 RoveCommTCP.h:133
~RoveCommTCP()
Destroy the RoveCommTCP::RoveCommTCP object.
Definition RoveCommTCP.cpp:57
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for TCP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommTCP.cpp:227
bool Init(const manifest::AddressEntry &stIPAddress={0, 0, 0, 0}, int nPort=manifest::General::ETHERNET_TCP_PORT)
Initializes a TCP socket and binds it to the specified IP address and port. And then starts the threa...
Definition RoveCommTCP.cpp:76
constexpr CallbackMap< T > & GetCallbackMap()
Definition RoveCommTCP.h:81
std::shared_mutex m_muCallbackMutex
Definition RoveCommTCP.h:94
ssize_t WriteTo(const RoveCommPacket< T > &stPacket, const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Sends a TCP packet to the specified client IP address and port. The data is packed into a RoveCommDat...
Definition RoveCommTCP.cpp:162
uint64_t m_ullNextCallbackID
Definition RoveCommTCP.h:78
RoveCommTCP()
Construct a new RoveCommTCP::RoveCommTCP object.
Definition RoveCommTCP.cpp:41
CallbackHandle On(std::function< void(const RoveCommPacket< EntryType< Entry > > &)> fnCallback)
Definition RoveCommTCP.h:145
void ReceiveAndCallback()
Receives a TCP packet from a client and invokes the appropriate callback function from the vector of ...
Definition RoveCommTCP.cpp:364
std::mutex m_muSocketSendMutex
Definition RoveCommTCP.h:95
const int ETHERNET_UDP_PORT
Definition RoveCommManifest.h:889
const int ETHERNET_TCP_PORT
Definition RoveCommManifest.h:890
const BoardEntry & FindBoardById(uint16_t unDataId)
Definition RoveCommManifest.h:1202
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 RoveCommTCP.h:57
uint64_t ullID
Definition RoveCommTCP.h:58
std::function< void(const RoveCommPacket< T > &)> fnCallback
Definition RoveCommTCP.h:59