src/MonoTorrent.Client/MonoTorrent.Client/Managers/ConnectionManager.cs
/// <summary>
/// This method is called when the ClientEngine recieves a valid incoming connection
/// </summary>
/// <param name="manager">The torrent which the peer is associated with.</param>
/// <param name="id">The peer who just conencted</param>
This method is called when the ClientEngine recieves a valid incoming connection
This method is called when the ClientEngine recieves a valid incoming connection
internal async ReusableTask<bool> IncomingConnectionAcceptedAsync (TorrentManager manager, PeerId id)
        {
            try {
                bool maxAlreadyOpen = OpenConnections >= Math.Min (MaxOpenConnections, Settings.MaximumConnections)
                    || OpenConnections >= manager.Settings.MaximumConnections;
                if (LocalPeerId.Equals (id.Peer.PeerId)) {
                    logger.Info ("Connected to self - disconnecting");
                    CleanupSocket (manager, id);
                    return false;
                }
                if (manager.Peers.ActivePeers.Contains (id.Peer)) {
                    logger.Info (id.Connection, "Already connected to peer");
                    id.Connection.Dispose ();
                    return false;
                }
                if (maxAlreadyOpen) {
                    logger.Info ("Connected to too many peers - disconnecting");
                    CleanupSocket (manager, id);
                    return false;
                }
                logger.Info (id.Connection, "Incoming connection fully accepted");
                manager.Peers.AvailablePeers.Remove (id.Peer);
                manager.Peers.ActivePeers.Add (id.Peer);
                manager.Peers.ConnectedPeers.Add (id);
                Interlocked.Increment (ref openConnections);
                id.WhenConnected.Restart ();
                id.LastBlockReceived.Restart ();
                var handshake = new HandshakeMessage (manager.InfoHash, manager.Engine.PeerId, Constants.ProtocolStringV100);
                await PeerIO.SendMessageAsync (id.Connection, id.Encryptor, handshake, manager.UploadLimiters, id.Monitor, manager.Monitor);
                manager.HandlePeerConnected (id);
                id.MessageQueue.SetReady ();
                TryProcessQueue (manager, id);
                ReceiveMessagesAsync (id.Connection, id.Decryptor, manager.DownloadLimiters, id.Monitor, manager, id);
                logger.InfoFormatted ("Incoming connection fully accepted", id.Uri);
                return true;
            } catch (Exception ex) {
                logger.Exception (ex, "Error handling incoming connection");
                CleanupSocket (manager, id);
                return false;
            }
        }
internal async ReusableTask < bool > IncomingConnectionAcceptedAsync ( TorrentManager manager , PeerId id ) { try { bool maxAlreadyOpen = OpenConnections >= Math . Min ( MaxOpenConnections , Settings . MaximumConnections ) || OpenConnections >= manager . Settings . MaximumConnections ; if ( LocalPeerId . Equals ( id . Peer . PeerId ) ) { logger . Info ( " " ) ; CleanupSocket ( manager , id ) ; return false ; } if ( manager . Peers . ActivePeers . Contains ( id . Peer ) ) { logger . Info ( id . Connection , " " ) ; id . Connection . Dispose ( ) ; return false ; } if ( maxAlreadyOpen ) { logger . Info ( " " ) ; CleanupSocket ( manager , id ) ; return false ; } logger . Info ( id . Connection , " " ) ; manager . Peers . AvailablePeers . Remove ( id . Peer ) ; manager . Peers . ActivePeers . Add ( id . Peer ) ; manager . Peers . ConnectedPeers . Add ( id ) ; Interlocked . Increment ( ref openConnections ) ; id . WhenConnected . Restart ( ) ; id . LastBlockReceived . Restart ( ) ; var handshake = new HandshakeMessage ( manager . InfoHash , manager . Engine . PeerId , Constants . ProtocolStringV100 ) ; await PeerIO . SendMessageAsync ( id . Connection , id . Encryptor , handshake , manager . UploadLimiters , id . Monitor , manager . Monitor ) ; manager . HandlePeerConnected ( id ) ; id . MessageQueue . SetReady ( ) ; TryProcessQueue ( manager , id ) ; ReceiveMessagesAsync ( id . Connection , id . Decryptor , manager . DownloadLimiters , id . Monitor , manager , id ) ; logger . InfoFormatted ( " " , id . Uri ) ; return true ; } catch ( Exception ex ) { logger . Exception ( ex , " " ) ; CleanupSocket ( manager , id ) ; return false ; } }
This method is called when the ClientEngine recieves a valid incoming connection
This method is called when the ClientEngine recieves a valid incoming connection
// Add the PeerId to the lists *before* doing anything asynchronous. This ensures that // all PeerIds are tracked in 'ConnectedPeers' as soon as they're created. // Baseline the time the last block was received // Send our handshake now that we've decided to keep the connection // We've sent our handshake so begin our looping to receive incoming message
manager
TorrentManager
The torrent which the peer is associated with.
The torrent which the peer is associated with .
id
PeerId
The peer who just conencted
The peer who just conencted