/// <summary> /// This method is called when the ClientEngine recieves a valid incoming connection /// </summary> private void IncomingConnectionAccepted(bool succeeded, int count, object state) { var id = (PeerId)state; try { if (!succeeded) { var args = new PeerConnectionFailedEventArgs(id.TorrentManager, id.Peer, Direction.Incoming, "Incoming connection coult not be accepted"); id.TorrentManager.RaiseConnectionAttemptFailed(args); } var maxAlreadyOpen = OpenConnections >= Math.Min(MaxOpenConnections, id.TorrentManager.Settings.MaxConnections); if (!succeeded || id.Peer.PeerId == _engine.PeerId || maxAlreadyOpen) { CleanupSocket(id, "Connection was not accepted"); return; } if (id.TorrentManager.Peers.ActivePeers.Contains(id.Peer)) { Logger.Log(id.Connection, "ConnectionManager - Already connected to peer"); id.Connection.Dispose(); return; } Logger.Log(id.Connection, "ConnectionManager - Incoming connection fully accepted"); id.TorrentManager.Peers.AvailablePeers.Remove(id.Peer); id.TorrentManager.Peers.ActivePeers.Add(id.Peer); id.TorrentManager.Peers.ConnectedPeers.Add(id); id.WhenConnected = DateTime.Now; // Baseline the time the last block was received id.LastBlockReceived = DateTime.Now; id.TorrentManager.HandlePeerConnected(id, Direction.Incoming); // We've sent our handshake so begin our looping to receive incoming message PeerIO.EnqueueReceiveMessage(id.Connection, id.Decryptor, id.TorrentManager.DownloadLimiter, id.Monitor, id.TorrentManager, MessageReceivedCallback, id); } catch (Exception e) { CleanupSocket(id, e.Message); } }
/// <summary> /// Raise the connection attempt failed event /// </summary> /// <param name="args"></param> internal void RaiseConnectionAttemptFailed(PeerConnectionFailedEventArgs args) { Toolbox.RaiseAsyncEvent <PeerConnectionFailedEventArgs>(this.ConnectionAttemptFailed, this, args); }
/// <summary> /// This method is called when the ClientEngine recieves a valid incoming connection /// </summary> private void IncomingConnectionAccepted(bool succeeded, int count, object state) { var id = (PeerId) state; try { if (!succeeded) { var args = new PeerConnectionFailedEventArgs(id.TorrentManager, id.Peer, Direction.Incoming, "Incoming connection coult not be accepted"); id.TorrentManager.RaiseConnectionAttemptFailed(args); } var maxAlreadyOpen = OpenConnections >= Math.Min(MaxOpenConnections, id.TorrentManager.Settings.MaxConnections); if (!succeeded || id.Peer.PeerId == _engine.PeerId || maxAlreadyOpen) { CleanupSocket(id, "Connection was not accepted"); return; } if (id.TorrentManager.Peers.ActivePeers.Contains(id.Peer)) { Logger.Log(id.Connection, "ConnectionManager - Already connected to peer"); id.Connection.Dispose(); return; } Logger.Log(id.Connection, "ConnectionManager - Incoming connection fully accepted"); id.TorrentManager.Peers.AvailablePeers.Remove(id.Peer); id.TorrentManager.Peers.ActivePeers.Add(id.Peer); id.TorrentManager.Peers.ConnectedPeers.Add(id); id.WhenConnected = DateTime.Now; // Baseline the time the last block was received id.LastBlockReceived = DateTime.Now; id.TorrentManager.HandlePeerConnected(id, Direction.Incoming); // We've sent our handshake so begin our looping to receive incoming message PeerIO.EnqueueReceiveMessage(id.Connection, id.Decryptor, id.TorrentManager.DownloadLimiter, id.Monitor, id.TorrentManager, MessageReceivedCallback, id); } catch (Exception e) { CleanupSocket(id, e.Message); } }