Refactor get_port to use FNV-1a hash for consistency across platforms (#634)

Fix ZMQ issue caused by mismatched port values between macOS and Linux
This commit is contained in:
Dean Lee 2024-11-17 02:28:33 +08:00 committed by GitHub
parent 3e17f865bb
commit e621ce0763
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 2 deletions

View File

@ -7,10 +7,19 @@
#include "msgq/impl_zmq.h" #include "msgq/impl_zmq.h"
static size_t fnv1a_hash(const std::string &str) {
const size_t fnv_prime = 0x100000001b3;
size_t hash_value = 0xcbf29ce484222325;
for (char c : str) {
hash_value ^= (unsigned char)c;
hash_value *= fnv_prime;
}
return hash_value;
}
//FIXME: This is a hack to get the port number from the socket name, might have collisions //FIXME: This is a hack to get the port number from the socket name, might have collisions
static int get_port(std::string endpoint) { static int get_port(std::string endpoint) {
std::hash<std::string> hasher; size_t hash_value = fnv1a_hash(endpoint);
size_t hash_value = hasher(endpoint);
int start_port = 8023; int start_port = 8023;
int max_port = 65535; int max_port = 65535;
int port = start_port + (hash_value % (max_port - start_port)); int port = start_port + (hash_value % (max_port - start_port));