From 1657fdc239d5883bae7b9b7b4cfdab156d038342 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Thu, 23 Nov 2023 19:56:09 +0100 Subject: [PATCH 1/2] initialize random udp package id using nanosecond timestamp --- ecal/core/src/io/udp/snd_raw_buffer.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ecal/core/src/io/udp/snd_raw_buffer.cpp b/ecal/core/src/io/udp/snd_raw_buffer.cpp index e90875af88..e36c108147 100644 --- a/ecal/core/src/io/udp/snd_raw_buffer.cpp +++ b/ecal/core/src/io/udp/snd_raw_buffer.cpp @@ -21,6 +21,8 @@ * @brief raw message buffer handling **/ +#include +#include #include #include "ecal_process.h" @@ -31,8 +33,11 @@ namespace { // random number generator + std::mutex xorshf96_mtx; unsigned long xorshf96(unsigned long& x, unsigned long& y, unsigned long& z) // period 2^96-1 { + const std::lock_guard lock(xorshf96_mtx); + unsigned long t; x ^= x << 16; x ^= x >> 5; @@ -129,7 +134,11 @@ namespace eCAL msg_header.type = msg_type_header; { // create random number for message id - static unsigned long x = 123456789, y = 362436069, z = 521288629; + static unsigned long x = std::chrono::duration_cast( + std::chrono::high_resolution_clock::now().time_since_epoch() + ).count(); + static unsigned long y = 362436069; + static unsigned long z = 521288629; msg_header.id = xorshf96(x, y, z); } msg_header.num = total_packet_num; From f003e584a9b113ca420673587fae7786d291d5e5 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 24 Nov 2023 14:34:39 +0100 Subject: [PATCH 2/2] mutex moved --- ecal/core/src/io/udp/snd_raw_buffer.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/ecal/core/src/io/udp/snd_raw_buffer.cpp b/ecal/core/src/io/udp/snd_raw_buffer.cpp index e36c108147..2ab8c15e2f 100644 --- a/ecal/core/src/io/udp/snd_raw_buffer.cpp +++ b/ecal/core/src/io/udp/snd_raw_buffer.cpp @@ -33,11 +33,8 @@ namespace { // random number generator - std::mutex xorshf96_mtx; unsigned long xorshf96(unsigned long& x, unsigned long& y, unsigned long& z) // period 2^96-1 { - const std::lock_guard lock(xorshf96_mtx); - unsigned long t; x ^= x << 16; x ^= x >> 5; @@ -134,12 +131,18 @@ namespace eCAL msg_header.type = msg_type_header; { // create random number for message id - static unsigned long x = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now().time_since_epoch() - ).count(); - static unsigned long y = 362436069; - static unsigned long z = 521288629; - msg_header.id = xorshf96(x, y, z); + { + static std::mutex xorshf96_mtx; + const std::lock_guard lock(xorshf96_mtx); + + static unsigned long x = static_cast(std::chrono::duration_cast( + std::chrono::high_resolution_clock::now().time_since_epoch()).count() + ); + static unsigned long y = 362436069; + static unsigned long z = 521288629; + + msg_header.id = xorshf96(x, y, z); + } } msg_header.num = total_packet_num; msg_header.len = int32_t(buf_len_);