Skip to content

Commit

Permalink
Use of std::random_device for GUId generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfence committed Jan 4, 2024
1 parent 5665585 commit f86b9fd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
23 changes: 20 additions & 3 deletions src/guid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,31 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <sstream>

#include "serverenvironment.h"
#include "util/base64.h"

GUIdGenerator::GUIdGenerator()
{
m_rand_usable = m_rand.entropy() > 0.01;
}

GUId GUIdGenerator::next(const std::string &prefix)
{
std::stringstream s_guid;

s_guid << prefix << std::hex << m_next << '-' << std::hex << m_env->getGameTime();

m_next++;
std::mt19937_64 gen(m_rand());
std::uniform_int_distribution<unsigned long long> m_uniform(0, UINT64_MAX);

u64 a[2];
if (m_rand_usable) {
a[0] = m_uniform(m_rand);
a[1] = m_uniform(m_rand);
}
else {
a[0] = (static_cast<u64>(m_env->getGameTime()) << 32) + m_next;
a[1] = m_uniform(m_rand);
m_next++;
}
s_guid << prefix << base64_encode(reinterpret_cast<unsigned char *>(a), 16);

return s_guid.str();
}
Expand Down
30 changes: 9 additions & 21 deletions src/guid.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "irrlichttypes.h"
#include "util/basic_macros.h"
#include <vector>
#include <random>
#include <string>

class ServerEnvironment;
Expand All @@ -44,7 +44,7 @@ class GUIdGenerator {
* @param prefix Prefix of generated GUId
* @param next next value getted from loadMeta
*/
GUIdGenerator() = default;
GUIdGenerator();

~GUIdGenerator() = default;
DISABLE_CLASS_COPY(GUIdGenerator)
Expand All @@ -56,25 +56,13 @@ class GUIdGenerator {
GUId next(const std::string &prefix);

private:
/**
* Set ServerEnvironment,
* @param env ServerEnvironment.
*/
void setServerEnvironment(ServerEnvironment *env, u64 state) { m_env = env; m_next = state; };
/**
* Set state to defined/known state,
* @param next New state value.
*/
void setState(u64 state) { m_next = state; };
/**
* Get state,
* @return Return aactual state value.
*/
u64 getState() const { return m_next; };

private:
void setServerEnvironment(ServerEnvironment *env) { m_env = env; }

ServerEnvironment *m_env;
u64 m_next;

std::random_device m_rand;
//std::uniform_int_distribution<> m_uniform(0, UINT64_MAX);
bool m_rand_usable;
u32 m_next;

friend class ServerEnvironment;
};
2 changes: 1 addition & 1 deletion src/serverenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ ServerEnvironment::ServerEnvironment(ServerMap *map,
m_active_object_gauge = mb->addGauge(
"minetest_env_active_objects", "Number of active objects");

m_guid_generator.setServerEnvironment(this, porting::getTimeS());
m_guid_generator.setServerEnvironment(this);
}

void ServerEnvironment::init()
Expand Down

0 comments on commit f86b9fd

Please sign in to comment.