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(PeerId id, string message = null) { 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 && !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.Dispose(); 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 (canReuse && 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)); } }
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(); }