internal void AsyncCleanupSocket(PeerId id, bool localClose, string message)
{
if (id == null) // Sometimes onEncryptoError will fire with a null id
return;
try
{
// It's possible the peer could be in an async send *and* receive and so end up
// in this block twice. This check makes sure we don't try to double dispose.
if (id.Connection == null)
return;
// We can reuse this peer if the connection says so and it's not marked as inactive
bool canResuse = id.Connection.CanReconnect && !id.TorrentManager.InactivePeerManager.InactivePeerList.Contains(id.Uri);
Logger.Log(id.Connection, "Cleanup Reason : " + message);
Logger.Log(id.Connection, "*******Cleaning up*******");
id.TorrentManager.PieceManager.Picker.CancelRequests(id);
id.Peer.CleanedUpCount++;
if (id.PeerExchangeManager != null)
id.PeerExchangeManager.Dispose();
if (!id.AmChoking)
id.TorrentManager.UploadingTo--;
id.Connection.Dispose();
id.Connection = null;
id.TorrentManager.Peers.ConnectedPeers.RemoveAll(delegate(PeerId other) { return id == other; });
if (id.TorrentManager.Peers.ActivePeers.Contains(id.Peer))
id.TorrentManager.Peers.ActivePeers.Remove(id.Peer);
// If we get our own details, this check makes sure we don't try connecting to ourselves again
if (canResuse && id.Peer.PeerId != engine.PeerId)
{
if (!id.TorrentManager.Peers.AvailablePeers.Contains(id.Peer) && id.Peer.CleanedUpCount < 5)
id.TorrentManager.Peers.AvailablePeers.Insert(0, id.Peer);
}
}
finally
{
id.TorrentManager.RaisePeerDisconnected(
new PeerConnectionEventArgs( id.TorrentManager, id, Direction.None, message ) );
}
}