Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize random udp package id using nanosecond timestamp #1270

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions ecal/core/src/io/udp/snd_raw_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @brief raw message buffer handling
**/

#include <chrono>
#include <mutex>
#include <thread>

#include "ecal_process.h"
Expand Down Expand Up @@ -129,8 +131,18 @@ namespace eCAL
msg_header.type = msg_type_header;
{
// create random number for message id
static unsigned long x = 123456789, y = 362436069, z = 521288629;
msg_header.id = xorshf96(x, y, z);
{
static std::mutex xorshf96_mtx;
const std::lock_guard<std::mutex> lock(xorshf96_mtx);

static unsigned long x = static_cast<unsigned long>(std::chrono::duration_cast<std::chrono::nanoseconds>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rex-schilasky I was wondering if std::random would be a bit more appropriate here, to indicate we need a random number to start with. If you started two processes at the exact same time, would they get the same numbers this way? Though still unlikely.

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_);
Expand Down
Loading