internal void AnnounceAndRegisterPeer()
{
try
{
var peerService = new PeerDiscoveryService
{
Url = (GameConfig.WebRoot + "/discovery/discoverydb.asmx"),
Timeout = NetworkTimeoutMsec
};
while (true)
{
// Make sure our bad peer list remains manageable
_peerManager.TruncateBadPeerList();
// Register with the central server and get peers
try
{
if (GameConfig.UseConfigForDiscovery || ValidatePeer(peerService.ValidatePeer()))
{
_networkStatusMessage = "";
if (_discoveryLed != null)
_discoveryLed.LedState = LedStates.Waiting;
DataSet data;
if (GameConfig.UseConfigForDiscovery)
{
// Get it from the config file
_totalPeersOnChannel = GetNumPeersFromConfig(_peerChannel);
if (_discoveryLed != null)
_discoveryLed.LedState = LedStates.Idle;
if (_discoveryLed != null)
_discoveryLed.LedState = LedStates.Waiting;
data = GetAllPeersFromConfig(_peerChannel);
if (_discoveryLed != null)
_discoveryLed.LedState = LedStates.Idle;
}
else
{
peerService.RegisterMyPeerGetCountAndPeerList(
Assembly.GetExecutingAssembly().GetName().Version.ToString(), _peerChannel,
GameEngine.Current.CurrentVector.State.StateGuid, out data,
out _totalPeersOnChannel);
}
if (_discoveryLed != null)
_discoveryLed.LedState = LedStates.Idle;
if (data != null)
{
var peerTable = data.Tables["Peers"];
var newPeersHash = Hashtable.Synchronized(new Hashtable());
foreach (DataRow row in peerTable.Rows)
{
var ipAddress = (string) row["IPAddress"];
var peerLease = (DateTime) row["Lease"];
if (ipAddress == _hostIP.ToString())
{
continue;
}
// If the bad peer has updated their lease, then they
// are online again, count them as good
if (!_peerManager.ClearBadPeer(ipAddress, peerLease))
continue;
newPeersHash.Add(ipAddress, new Peer(ipAddress, peerLease));
}
_peerManager.KnownPeers = newPeersHash;
}
}
else
{
_networkStatusMessage = NetworkBehindNatMessage;
}
}
catch (Exception ex)
{
if (_discoveryLed != null)
_discoveryLed.LedState = LedStates.Failed;
ErrorLog.LogHandledException(ex);
}
// Sleep for a while so we don't flood the network
Thread.Sleep(AnnounceAndRegisterPeerWaitMsec);
}
}
catch (ThreadAbortException)
{
// percolate out and exit
Thread.ResetAbort();
}
catch (Exception e)
{
if (!(e.InnerException is ThreadAbortException))
{
ErrorLog.LogHandledException(e);
}
}
}