Пример #1
0
 public EndGameSwitcher(StandardPicker standard, EndGamePicker endgame, int blocksPerPiece, TorrentManager torrentManager)
     : base(null)
 {
     this.standard       = standard;
     this.endgame        = endgame;
     this.blocksPerPiece = blocksPerPiece;
     this.torrentManager = torrentManager;
 }
Пример #2
0
 public TorrentEventArgs(TorrentManager manager)
 {
     TorrentManager = manager;
 }
Пример #3
0
 public AsyncConnectState(TorrentManager manager, Peer peer, IConnection connection)
 {
     Manager    = manager;
     Peer       = peer;
     Connection = connection;
 }
Пример #4
0
 /// <summary>
 ///   Creates a new inactive peer manager for a torrent manager
 /// </summary>
 /// <param name="torrentManager"> The torrent manager this choke/unchoke manager belongs to </param>
 public InactivePeerManager(TorrentManager torrentManager)
 {
     _owningTorrent = torrentManager;
 }
Пример #5
0
 public StoppedMode(TorrentManager manager)
     : base(manager)
 {
     CanAcceptConnections = false;
 }
Пример #6
0
 protected virtual void RaiseConnectionReceived(Peer peer, IConnection connection, TorrentManager manager)
 {
     if (ConnectionReceived != null)
     {
         Toolbox.RaiseAsyncEvent(ConnectionReceived, this, new NewConnectionEventArgs(peer, connection, manager));
     }
 }
Пример #7
0
        public void PieceDataReceived(PeerId peer, PieceMessage message)
        {
            Piece piece;

            if (_picker.ValidatePiece(peer, message.PieceIndex, message.StartOffset, message.RequestLength, out piece))
            {
                PeerId         id      = peer;
                TorrentManager manager = id.TorrentManager;
                Block          block   = piece.Blocks [message.StartOffset / Piece.BlockSize];
                long           offset  = (long)message.PieceIndex * id.TorrentManager.Torrent.PieceLength + message.StartOffset;

                id.LastBlockReceived = DateTime.Now;
                id.TorrentManager.PieceManager.RaiseBlockReceived(new BlockEventArgs(manager, block, piece, id));
                id.TorrentManager.Engine.DiskManager.QueueWrite(manager, offset, message.Data, message.RequestLength, delegate {
                    piece.Blocks[message.StartOffset / Piece.BlockSize].Written = true;
                    ClientEngine.BufferManager.FreeBuffer(ref message.Data);
                    // If we haven't written all the pieces to disk, there's no point in hash checking
                    if (!piece.AllBlocksWritten)
                    {
                        return;
                    }

                    // Hashcheck the piece as we now have all the blocks.
                    id.Engine.DiskManager.BeginGetHash(id.TorrentManager, piece.Index, delegate(object o) {
                        byte[] hash = (byte[])o;
                        bool result = hash == null ? false : id.TorrentManager.Torrent.Pieces.IsValid(hash, piece.Index);
                        id.TorrentManager.Bitfield[message.PieceIndex] = result;

                        ClientEngine.MainLoop.Queue(delegate
                        {
                            id.TorrentManager.PieceManager.UnhashedPieces[piece.Index] = false;

                            id.TorrentManager.HashedPiece(new PieceHashedEventArgs(id.TorrentManager, piece.Index, result));
                            var peers = new List <PeerId>(piece.Blocks.Length);
                            foreach (var b in piece.Blocks.Where(b => b.RequestedOff != null && !peers.Contains(b.RequestedOff)))
                            {
                                peers.Add(b.RequestedOff);
                            }

                            foreach (var p in peers.Where(p => p.Connection != null))
                            {
                                p.Peer.HashedPiece(result);
                                if (p.Peer.TotalHashFails == 5)
                                {
                                    p.ConnectionManager.CleanupSocket(id, "Too many hash fails");
                                }
                            }

                            // If the piece was successfully hashed, enqueue a new "have" message to be sent out
                            if (result)
                            {
                                id.TorrentManager.FinishedPieces.Enqueue(piece.Index);
                            }
                        });
                    });
                });

                if (piece.AllBlocksReceived)
                {
                    _unhashedPieces[message.PieceIndex] = true;
                }
            }
        }
Пример #8
0
 public ErrorMode(TorrentManager manager)
     : base(manager)
 {
     CanAcceptConnections = false;
     CloseConnections();
 }
Пример #9
0
 public ReceiveMessageState Initialise(IConnection connection, IEncryption decryptor, IRateLimiter limiter, ConnectionMonitor peerMonitor, TorrentManager manager, byte[] buffer, AsyncMessageReceivedCallback callback, object state)
 {
     Connection     = connection;
     Decryptor      = decryptor;
     Manager        = manager;
     Buffer         = buffer;
     PeerMonitor    = peerMonitor;
     RateLimiter    = limiter;
     ManagerMonitor = manager == null ? null : manager.Monitor;
     Callback       = callback;
     State          = state;
     return(this);
 }
Пример #10
0
 public MetadataMode(TorrentManager manager, string savePath)
     : base(manager)
 {
     _savePath = savePath;
 }
Пример #11
0
        private void HandleHandshake(PeerId id, HandshakeMessage message)
        {
            TorrentManager man = null;

            try
            {
                if (message.ProtocolString != VersionInfo.ProtocolStringV100)
                {
                    throw new ProtocolException("Invalid protocol string in handshake");
                }
            }
            catch (Exception ex)
            {
                Logger.Log(id.Connection, ex.Message);
                id.Connection.Dispose();
                return;
            }

            ClientEngine.MainLoop.QueueWait(() =>
            {
                foreach (var torrentManager in _engine.Torrents.Where(tm => message.InfoHash == tm.InfoHash))
                {
                    man = torrentManager;
                }
            });

            //FIXME: #warning FIXME: Don't stop the message loop until Dispose() and track all incoming connections
            if (man == null) // We're not hosting that torrent
            {
                Logger.Log(id.Connection, "ListenManager - Handshake requested nonexistant torrent");
                id.Connection.Dispose();
                return;
            }
            if (man.State == TorrentState.Stopped)
            {
                Logger.Log(id.Connection, "ListenManager - Handshake requested for torrent which is not running");
                id.Connection.Dispose();
                return;
            }
            if (!man.Mode.CanAcceptConnections)
            {
                Logger.Log(id.Connection, "ListenManager - Current mode does not support connections");
                id.Connection.Dispose();
                return;
            }

            id.Peer.PeerId    = message.PeerId;
            id.TorrentManager = man;

            // If the handshake was parsed properly without encryption, then it definitely was not encrypted. If this is not allowed, abort
            if ((id.Encryptor is PlainTextEncryption &&
                 !Toolbox.HasEncryption(_engine.Settings.AllowedEncryption, EncryptionTypes.PlainText)) &&
                ClientEngine.SupportsEncryption)
            {
                Logger.Log(id.Connection, "ListenManager - Encryption is required but was not active");
                id.Connection.Dispose();
                return;
            }

            message.Handle(id);
            Logger.Log(id.Connection, "ListenManager - Handshake successful handled");

            id.ClientApp = new Software(message.PeerId);

            message = new HandshakeMessage(id.TorrentManager.InfoHash, _engine.PeerId, VersionInfo.ProtocolStringV100);
            var callback = _engine.ConnectionManager.IncomingConnectionAcceptedCallback;

            PeerIO.EnqueueSendMessage(id.Connection, id.Encryptor, message, id.TorrentManager.UploadLimiter,
                                      id.Monitor, id.TorrentManager.Monitor, callback, id);
        }
Пример #12
0
 public DownloadMode(TorrentManager manager)
     : base(manager)
 {
     _state = manager.Complete ? TorrentState.Seeding : TorrentState.Downloading;
 }
Пример #13
0
 /// <summary>
 /// Creates a new TorrentStateChangedEventArgs
 /// </summary>
 public TorrentStateChangedEventArgs(TorrentManager manager, TorrentState oldState, TorrentState newState)
     : base(manager)
 {
     _oldState = oldState;
     _newState = newState;
 }
Пример #14
0
 internal PeerConnectionEventArgs(TorrentManager manager, PeerId id, Direction direction)
     : this(manager, id, direction, string.Empty)
 {
 }