mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-06 20:43:44 +02:00
More work in progress on new Multicaster. This should be pretty much good to go, and performance should not be too O(crappy).
This commit is contained in:
parent
770fbaf4b2
commit
62a6f7ca63
1 changed files with 55 additions and 35 deletions
|
@ -33,8 +33,7 @@
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <list>
|
||||||
#include <set>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include "Constants.hpp"
|
#include "Constants.hpp"
|
||||||
|
@ -63,13 +62,13 @@ public:
|
||||||
inline void likesGroup(const Address &a,const MulticastGroup &mg,uint64_t now)
|
inline void likesGroup(const Address &a,const MulticastGroup &mg,uint64_t now)
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_lock);
|
Mutex::Lock _l(_lock);
|
||||||
std::map< Address,_PeerInfo >::iterator pi(_peers.find(a));
|
_SubInfo &si = _subscriptions[_Subscription(a,mg)];
|
||||||
if (pi == _peers.end()) {
|
if (!si.lastLike) { // on first LIKE, we must add to _proximity[mg]
|
||||||
pi = _peers.insert(std::pair< Address,_PeerInfo >(a,_PeerInfo())).first;
|
std::list< Address > &p = _proximity[mg];
|
||||||
_proximity.push_front(a);
|
p.push_front(a);
|
||||||
pi->second.proximitySlot = _proximity.begin();
|
si.proximitySlot = p.begin(); // list's iterators remain valid until erase()
|
||||||
}
|
}
|
||||||
pi->second.groups[mg] = now;
|
si.lastLike = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,10 +80,19 @@ public:
|
||||||
inline void bringCloser(const Address &a)
|
inline void bringCloser(const Address &a)
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_lock);
|
Mutex::Lock _l(_lock);
|
||||||
std::map< Address,_PeerInfo >::iterator pi(_peers.find(a));
|
|
||||||
if (pi != _peers.end()) {
|
// _subscriptions contains pairs of <Address,MulticastGroup>, so we can
|
||||||
if (pi->second.proximitySlot != _proximity.begin())
|
// easily iterate through all subscriptions for a given address by
|
||||||
_proximity.splice(_proximity.begin(),_proximity,pi->second.proximitySlot);
|
// starting with the default all-zero MulticastGroup() as lower bound
|
||||||
|
// and stopping when we're not looking at the right address anymore.
|
||||||
|
// Then we can look up _proximity and rapidly splice() the list using
|
||||||
|
// the saved iterator in _SubInfo.
|
||||||
|
std::map< _Subscription,_SubInfo >::iterator s(_subscriptions.lower_bound(_Subscription(a,MulticastGroup())));
|
||||||
|
while ((s != _subscriptions.end())&&(s->first.first == a)) {
|
||||||
|
std::map< MulticastGroup,std::list< Address > >::iterator p(_proximity.find(s->first.second));
|
||||||
|
if (s->second.proximitySlot != p->second.begin())
|
||||||
|
p->second.splice(p->second.begin(),p->second,s->second.proximitySlot);
|
||||||
|
++s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +114,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erase entries for expired LIKEs
|
* Erase entries for expired LIKEs and GOT records
|
||||||
*/
|
*/
|
||||||
inline void clean(uint64_t now)
|
inline void clean(uint64_t now)
|
||||||
{
|
{
|
||||||
|
@ -118,22 +126,23 @@ public:
|
||||||
else ++g;
|
else ++g;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(std::map< Address,_PeerInfo >::iterator pi(_peers.begin());pi!=_peers.end();) {
|
for(std::map< _Subscription,_SubInfo >::iterator s(_subscriptions.begin());s!=_subscriptions.end();) {
|
||||||
for(std::map< MulticastGroup,uint64_t >::iterator g(pi->second.groups.begin());g!=pi->second.groups.end();) {
|
if ((now - s->second.lastLike) > ZT_MULTICAST_LIKE_EXPIRE) {
|
||||||
if ((now - g->second) > ZT_MULTICAST_LIKE_EXPIRE)
|
std::map< MulticastGroup,std::list< Address > > p(_proximity.find(s->first.second));
|
||||||
pi->second.groups.erase(g++);
|
p->second.erase(s->second.proximitySlot);
|
||||||
else ++g;
|
if (p->second.empty())
|
||||||
}
|
_proximity.erase(p);
|
||||||
if (pi->second.groups.empty()) {
|
_subscriptions.erase(s++);
|
||||||
_proximity.erase(pi->second.proximitySlot);
|
} else ++s;
|
||||||
_peers.erase(pi++);
|
|
||||||
} else ++pi;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pick next hops for a multicast by proximity
|
* Pick next hops for a multicast by proximity
|
||||||
*
|
*
|
||||||
|
* The function or function object must return true if more hops are desired
|
||||||
|
* or false to stop finding new hops and return.
|
||||||
|
*
|
||||||
* @param mg Multicast group
|
* @param mg Multicast group
|
||||||
* @param mcGuid Multicast message GUID (signer and signer unique ID)
|
* @param mcGuid Multicast message GUID (signer and signer unique ID)
|
||||||
* @param nextHopFunc Function to call for each address, search stops if it returns false
|
* @param nextHopFunc Function to call for each address, search stops if it returns false
|
||||||
|
@ -143,32 +152,43 @@ public:
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_lock);
|
Mutex::Lock _l(_lock);
|
||||||
std::map< uint64_t,std::pair< uint64_t,std::set< Address > > > g(_got.find(mcGuid));
|
std::map< uint64_t,std::pair< uint64_t,std::set< Address > > > g(_got.find(mcGuid));
|
||||||
for(std::list< Address >::iterator a(_proximity.begin());a!=_proximity.end();++a) {
|
std::map< MulticastGroup,std::list< Address > > p(_proximity.find(mg));
|
||||||
if (((g == _got.end())||(!g->second.second.count(*a)))&&(_peers.find(*a)->second.groups.count(mg))) {
|
if (p != _proximity.end()) {
|
||||||
|
for(std::list< Address >::iterator a(p->second.begin());a!=p->second.end();++a) {
|
||||||
|
if ((g == _got.end())||(!g->second.second.count(*a))) {
|
||||||
if (!nextHopFunc(*a))
|
if (!nextHopFunc(*a))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// GOTs by multicast GUID: time of last GOT, addresses that GOT
|
// GOTs by multicast GUID: time of last GOT, addresses that GOT
|
||||||
std::map< uint64_t,std::pair< uint64_t,std::set< Address > > > _got;
|
std::map< uint64_t,std::pair< uint64_t,std::set< Address > > > _got;
|
||||||
|
|
||||||
// Peer proximity ordering
|
// Peer proximity ordering for peers subscribed to each group
|
||||||
std::list< Address > _proximity;
|
std::map< MulticastGroup,std::list< Address > > _proximity;
|
||||||
|
|
||||||
struct _PeerInfo
|
// An address and multicast group tuple
|
||||||
|
typedef std::pair<Address,MulticastGroup> _Subscription;
|
||||||
|
|
||||||
|
// Information about a subscription
|
||||||
|
struct _SubInfo
|
||||||
{
|
{
|
||||||
// Groups and time of last LIKE for each group
|
_SubInfo() :
|
||||||
std::map< MulticastGroup,uint64_t > groups;
|
lastLike(0),
|
||||||
|
proximitySlot() {}
|
||||||
|
|
||||||
// Peer's slot in _proximity
|
// Time of last MULTICAST_LIKE for this group
|
||||||
|
uint64_t lastLike;
|
||||||
|
|
||||||
|
// Slot in corresponding list in _proximity
|
||||||
std::list< Address >::iterator proximitySlot;
|
std::list< Address >::iterator proximitySlot;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Time of last LIKE for each address's group subscriptions
|
// Peer subscriptions to multicast groups
|
||||||
std::map< Address,_PeerInfo > _peers;
|
std::map< _Subscription,_SubInfo > _subscriptions;
|
||||||
|
|
||||||
Mutex _lock;
|
Mutex _lock;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue