Skip to content

Commit e621ce0

Browse files
authored
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
1 parent 3e17f86 commit e621ce0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

msgq/impl_zmq.cc

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77

88
#include "msgq/impl_zmq.h"
99

10+
static size_t fnv1a_hash(const std::string &str) {
11+
const size_t fnv_prime = 0x100000001b3;
12+
size_t hash_value = 0xcbf29ce484222325;
13+
for (char c : str) {
14+
hash_value ^= (unsigned char)c;
15+
hash_value *= fnv_prime;
16+
}
17+
return hash_value;
18+
}
19+
1020
//FIXME: This is a hack to get the port number from the socket name, might have collisions
1121
static int get_port(std::string endpoint) {
12-
std::hash<std::string> hasher;
13-
size_t hash_value = hasher(endpoint);
22+
size_t hash_value = fnv1a_hash(endpoint);
1423
int start_port = 8023;
1524
int max_port = 65535;
1625
int port = start_port + (hash_value % (max_port - start_port));

0 commit comments

Comments
 (0)