forked from Mirrorlandia_minetest/minetest
Assign peer IDs randomly
This commit is contained in:
parent
db88d24ff8
commit
2587302987
@ -1551,29 +1551,22 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
|
|||||||
{
|
{
|
||||||
// Somebody wants to make a new connection
|
// Somebody wants to make a new connection
|
||||||
|
|
||||||
// Get a unique peer id (2 or higher)
|
// Get a unique peer id
|
||||||
session_t peer_id_new = m_next_remote_peer_id;
|
const session_t minimum = 2;
|
||||||
u16 overflow = MAX_UDP_PEERS;
|
const session_t overflow = MAX_UDP_PEERS;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Find an unused peer id
|
Find an unused peer id
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MutexAutoLock lock(m_peers_mutex);
|
MutexAutoLock lock(m_peers_mutex);
|
||||||
bool out_of_ids = false;
|
session_t peer_id_new;
|
||||||
for(;;) {
|
for (int tries = 0; tries < 100; tries++) {
|
||||||
// Check if exists
|
peer_id_new = myrand_range(minimum, overflow - 1);
|
||||||
if (m_peers.find(peer_id_new) == m_peers.end())
|
if (m_peers.find(peer_id_new) == m_peers.end())
|
||||||
|
|
||||||
break;
|
break;
|
||||||
// Check for overflow
|
|
||||||
if (peer_id_new == overflow) {
|
|
||||||
out_of_ids = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
peer_id_new++;
|
|
||||||
}
|
}
|
||||||
|
if (m_peers.find(peer_id_new) != m_peers.end()) {
|
||||||
if (out_of_ids) {
|
|
||||||
errorstream << getDesc() << " ran out of peer ids" << std::endl;
|
errorstream << getDesc() << " ran out of peer ids" << std::endl;
|
||||||
return PEER_ID_INEXISTENT;
|
return PEER_ID_INEXISTENT;
|
||||||
}
|
}
|
||||||
@ -1585,8 +1578,6 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
|
|||||||
m_peers[peer->id] = peer;
|
m_peers[peer->id] = peer;
|
||||||
m_peer_ids.push_back(peer->id);
|
m_peer_ids.push_back(peer->id);
|
||||||
|
|
||||||
m_next_remote_peer_id = (peer_id_new +1 ) % MAX_UDP_PEERS;
|
|
||||||
|
|
||||||
LOG(dout_con << getDesc()
|
LOG(dout_con << getDesc()
|
||||||
<< "createPeer(): giving peer_id=" << peer_id_new << std::endl);
|
<< "createPeer(): giving peer_id=" << peer_id_new << std::endl);
|
||||||
|
|
||||||
|
@ -793,8 +793,6 @@ private:
|
|||||||
u32 m_bc_receive_timeout = 0;
|
u32 m_bc_receive_timeout = 0;
|
||||||
|
|
||||||
bool m_shutting_down = false;
|
bool m_shutting_down = false;
|
||||||
|
|
||||||
session_t m_next_remote_peer_id = 2;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -245,7 +245,7 @@ void TestConnection::testConnectSendReceive()
|
|||||||
UASSERT(hand_client.last_id == 1);
|
UASSERT(hand_client.last_id == 1);
|
||||||
// Server should have the client
|
// Server should have the client
|
||||||
UASSERT(hand_server.count == 1);
|
UASSERT(hand_server.count == 1);
|
||||||
UASSERT(hand_server.last_id == 2);
|
UASSERT(hand_server.last_id >= 2);
|
||||||
|
|
||||||
//sleep_ms(50);
|
//sleep_ms(50);
|
||||||
|
|
||||||
@ -300,7 +300,7 @@ void TestConnection::testConnectSendReceive()
|
|||||||
UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
|
UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
session_t peer_id_client = 2;
|
const session_t peer_id_client = hand_server.last_id;
|
||||||
/*
|
/*
|
||||||
Send a large packet
|
Send a large packet
|
||||||
*/
|
*/
|
||||||
@ -374,5 +374,5 @@ void TestConnection::testConnectSendReceive()
|
|||||||
UASSERT(hand_client.count == 1);
|
UASSERT(hand_client.count == 1);
|
||||||
UASSERT(hand_client.last_id == 1);
|
UASSERT(hand_client.last_id == 1);
|
||||||
UASSERT(hand_server.count == 1);
|
UASSERT(hand_server.count == 1);
|
||||||
UASSERT(hand_server.last_id == 2);
|
UASSERT(hand_server.last_id >= 2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user