c++ - Is it guaranteed that socket descriptors in linux/freebsd will have almost sequential or comparable numbers? -
i understand there nil preventing linux assign descriptors random numbers in range 0...2^32 when create new socket. but reality is? in application (web server) need mapping construction maps descriptor "connection structure". understand kind of rb-tree (int -> connection_ptr*) work, linear array of connection_ptr pointers (where each pointer placed @ offset (index) = descriptor value) little faster.
even if implemented way, if care portability , reliability, cannot rely on that.
to constant access complexity, can utilize hashed container, std:unordered_map
. can write custom hasher, storage optimized file descriptor number distribution.
struct sockethasher { size_t operator()(uint32_t key) { homecoming key & 0xffff; } } std::unordered_map<uint32_t, connection_ptr, sockethasher> connectionpool;
c++ c linux sockets
No comments:
Post a Comment