internal void CleanupSocket(TorrentManager manager, PeerId id) { if (id == null || id.Disposed) // Sometimes onEncryptoError will fire with a null id { return; } try { // We can reuse this peer if the connection says so and it's not marked as inactive bool canReuse = (id.Connection?.CanReconnect ?? false) && !manager.InactivePeerManager.InactivePeerList.Contains(id.Uri) && id.Peer.AllowedEncryption != EncryptionTypes.None; manager.PieceManager.Picker.CancelRequests(id); id.Peer.CleanedUpCount++; if (id.PeerExchangeManager != null) { id.PeerExchangeManager.Dispose(); } if (!id.AmChoking) { manager.UploadingTo--; } manager.Peers.ConnectedPeers.Remove(id); manager.Peers.HandshakingPeers.Remove(id); manager.Peers.ActivePeers.Remove(id.Peer); // If we get our own details, this check makes sure we don't try connecting to ourselves again if (canReuse && !LocalPeerId.Equals(id.Peer.PeerId)) { if (!manager.Peers.AvailablePeers.Contains(id.Peer) && id.Peer.CleanedUpCount < 5) { manager.Peers.AvailablePeers.Insert(0, id.Peer); } else if (manager.Peers.BannedPeers.Contains(id.Peer) && id.Peer.CleanedUpCount >= 5) { manager.Peers.BannedPeers.Add(id.Peer); } } } catch (Exception ex) { Logger.Log(null, "CleanupSocket Error " + ex.Message); } finally { manager.RaisePeerDisconnected(new PeerDisconnectedEventArgs(manager, id)); } id.Dispose(); }
internal void CleanupSocket(TorrentManager manager, PeerId id) { if (id == null || id.Disposed) // Sometimes onEncryptoError will fire with a null id { return; } try { // We can reuse this peer if the connection says so and it's not marked as inactive bool canReuse = (id.Connection?.CanReconnect ?? false) && !manager.InactivePeerManager.InactivePeerList.Contains(id.Uri) && id.Peer.AllowedEncryption.Count > 0 && !manager.Engine.PeerId.Equals(id.PeerID); manager.PieceManager.Picker.CancelRequests(id); id.Peer.CleanedUpCount++; id.PeerExchangeManager?.Dispose(); if (!id.AmChoking) { manager.UploadingTo--; } if (manager.Peers.ConnectedPeers.Remove(id)) { Interlocked.Decrement(ref openConnections); } manager.Peers.ActivePeers.Remove(id.Peer); // If we get our own details, this check makes sure we don't try connecting to ourselves again if (canReuse && !LocalPeerId.Equals(id.Peer.PeerId)) { if (!manager.Peers.AvailablePeers.Contains(id.Peer) && id.Peer.CleanedUpCount < 5) { manager.Peers.AvailablePeers.Insert(0, id.Peer); } else if (manager.Peers.BannedPeers.Contains(id.Peer) && id.Peer.CleanedUpCount >= 5) { manager.Peers.BannedPeers.Add(id.Peer); } } } catch (Exception ex) { logger.Exception(ex, "An unexpected error occured cleaning up a connection"); } finally { manager.RaisePeerDisconnected(new PeerDisconnectedEventArgs(manager, id)); } id.Dispose(); }