Skip to content

Commit 2175827

Browse files
authored
Merge pull request #61 from MissouriMRDT/hotfix/data-corruption-bug
Rework Send and Receive of RoveComm_CPP to Improve Speed and Lower Latency
2 parents 787b0f2 + 1d85561 commit 2175827

10 files changed

Lines changed: 1015 additions & 741 deletions

File tree

data/RoveComm

Submodule RoveComm updated 1 file

src/RoveComm/RoveCommManifest.h

Lines changed: 71 additions & 65 deletions
Large diffs are not rendered by default.

src/RoveComm/RoveCommPacket.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ typedef int ssize_t;
2929
#undef _WIN32_WINNT
3030
#define _WIN32_WINNT 0x0600
3131
#endif
32-
#include <winsock2.h>
32+
#define _WINSOCKAPI_
33+
#include <mswsock.h>
3334
#include <windows.h>
35+
#include <winsock2.h>
3436
#include <ws2tcpip.h>
3537
#else
3638
#include <arpa/inet.h>

src/RoveComm/RoveCommTCP.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ namespace rovecomm
192192

193193
// Pack the data
194194
RoveCommData stData = PackPacket(stPacket);
195-
196195
// Get size of data not including the data not filled. (the null/zero data in RoveCommData)
197196
size_t siDataSize = ROVECOMM_PACKET_HEADER_SIZE + (sizeof(T) * stPacket.unDataCount);
197+
198+
// Acquire a write lock on the socket send mutex to protect the socket, which is shared between threads, but not thread-safe.
199+
std::unique_lock<std::mutex> lkSocketSendLock(m_muSocketSendMutex);
198200
// Send the data
199201
#if defined(__ROVECOMM_WINDOWS_MODE__) && __ROVECOMM_WINDOWS_MODE__ == 1
200202
ssize_t siBytesSent = send(nClientSocket, reinterpret_cast<char*>(&stData), siDataSize, 0);

src/RoveComm/RoveCommTCP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace rovecomm
5656
std::atomic_int m_nCurrentTCPClientSocket;
5757
struct sockaddr_in m_saClientAddr;
5858
std::shared_mutex m_muCallbackMutex;
59+
std::mutex m_muSocketSendMutex;
5960

6061
// Packet processing functions
6162
template<typename T>

src/RoveComm/RoveCommUDP.cpp

Lines changed: 293 additions & 21 deletions
Large diffs are not rendered by default.

src/RoveComm/RoveCommUDP.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ namespace rovecomm
5050
class RoveCommUDP : AutonomyThread<void>
5151
{
5252
private:
53+
#if defined(__ROVECOMM_WINDOWS_MODE__) && __ROVECOMM_WINDOWS_MODE__ == 1
54+
// Windows specific private member variables.
55+
HANDLE m_stdIOCP;
56+
#endif
57+
5358
// Private member variables
5459
std::atomic_int m_nUDPSocket;
5560
struct sockaddr_in m_saUDPServerAddr;
5661
std::vector<SubscriberInfo> vSubscribers;
5762
std::shared_mutex m_muCallbackMutex;
63+
std::mutex m_muSocketSendMutex;
64+
std::mutex m_muSocketReceiveMutex;
5865

5966
// Packet processing functions
6067
template<typename T>

src/interfaces/AutonomyThread.hpp

Lines changed: 624 additions & 649 deletions
Large diffs are not rendered by default.

src/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ int main()
132132
// Wait for packets to be processed.
133133
std::this_thread::sleep_for(std::chrono::milliseconds(500));
134134

135+
// Close the UDP and TCP sockets
136+
pRoveCommUDP_Node.CloseUDPSocket();
137+
pRoveCommTCP_Node.CloseTCPSocket();
138+
135139
exit(0);
136140
}
137141

tests/Unit/src/udp.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,14 @@ TEST(RoveCommUDP, SendUDPPacket)
110110

111111
// Send the packet to the localhost
112112
ssize_t siBytesSent = pRoveCommUDP_Node.SendUDPPacket<uint8_t>(stPacket, "127.0.0.1", 11001);
113-
114-
// Check if the packet successfully sent
115-
EXPECT_EQ(siBytesSent, ROVECOMM_PACKET_HEADER_SIZE + (sizeof(uint8_t) * stPacket.unDataCount));
113+
// Check if the packet successfully sent.
114+
EXPECT_EQ(siBytesSent, 1);
115+
116+
// Send two packets to the localhost
117+
ssize_t siBytesSent1 = pRoveCommUDP_Node.SendUDPPacket<uint8_t>(stPacket, "127.0.0.1", 11001);
118+
ssize_t siBytesSent2 = pRoveCommUDP_Node.SendUDPPacket<uint8_t>(stPacket, "127.0.0.1", 11001);
119+
// Check if the packets successfully sent.
120+
EXPECT_EQ(siBytesSent1 + siBytesSent2, 2);
116121

117122
// Close the socket
118123
pRoveCommUDP_Node.CloseUDPSocket();

0 commit comments

Comments
 (0)