/// <summary> /// This method is called when the ClientEngine recieves a valid incoming connection /// </summary> /// <param name="result"></param> private void IncomingConnectionAccepted(bool succeeded, int count, object state) { PeerId 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); } bool maxAlreadyOpen = (OpenConnections >= this.MaxOpenConnections) || (id.TorrentManager.OpenConnections >= 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.Error("Already connected to peer {0}", id.Peer); id.Connection.Dispose(); return; } logger.Info("Incoming connection fully accepted {0}, total {1}", id.Peer, OpenConnections); 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> /// <param name="result"></param> private void IncomingConnectionAccepted(bool succeeded, int count, object state) { PeerId 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); } bool maxAlreadyOpen = OpenConnections >= Math.Min(this.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.Error("Already connected to peer {0}", id.Peer); id.Connection.Dispose(); return; } logger.Info("Incoming connection fully accepted {0}, total {1}", id.Peer, OpenConnections); 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); } }