Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TransferManager" /> class.
        /// </summary>
        /// <param name="listeningPort">The port.</param>
        /// <param name="torrentInfo">The torrent information.</param>
        /// <param name="throttlingManager">The throttling manager.</param>
        /// <param name="persistenceManager">The persistence manager.</param>
        public TransferManager(int listeningPort, TorrentInfo torrentInfo, ThrottlingManager throttlingManager, PersistenceManager persistenceManager)
        {
            listeningPort.MustBeGreaterThanOrEqualTo(IPEndPoint.MinPort);
            listeningPort.MustBeLessThanOrEqualTo(IPEndPoint.MaxPort);
            torrentInfo.CannotBeNull();
            throttlingManager.CannotBeNull();
            persistenceManager.CannotBeNull();

            Tracker tracker = null;

            this.PeerId = "-AB1100-" + "0123456789ABCDEF".Random(24);

            Debug.WriteLine($"creating torrent manager for torrent {torrentInfo.InfoHash}");
            Debug.WriteLine($"local peer id {this.PeerId}");

            this.TorrentInfo = torrentInfo;

            this.throttlingManager = throttlingManager;

            this.persistenceManager = persistenceManager;

            // initialize trackers
            foreach (var trackerUri in torrentInfo.AnnounceList)
            {
                if (trackerUri.Scheme == "http" ||
                    trackerUri.Scheme == "https")
                {
                    tracker = new HttpTracker(trackerUri, this.PeerId, torrentInfo.InfoHash, listeningPort);
                }
                else if (trackerUri.Scheme == "udp")
                {
                    tracker = new UdpTracker(trackerUri, this.PeerId, torrentInfo.InfoHash, listeningPort);
                }

                if (tracker != null)
                {
                    tracker.TrackingEvent       = TrackingEvent.Started;
                    tracker.Announcing         += this.Tracker_Announcing;
                    tracker.Announced          += this.Tracker_Announced;
                    tracker.TrackingFailed     += this.Tracker_TrackingFailed;
                    tracker.BytesLeftToDownload = this.TorrentInfo.Length - this.Downloaded;
                    tracker.WantedPeerCount     = 30; // we can handle 30 peers at a time

                    this.trackers.Add(trackerUri, tracker);
                }
                else
                {
                    // unsupported tracker protocol
                    Debug.WriteLine($"unsupported tracker protocol {trackerUri.Scheme}");
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PeerCommunicator" /> class.
        /// </summary>
        /// <param name="throttlingManager">The throttling manager.</param>
        /// <param name="tcp">The TCP.</param>
        public PeerCommunicator(ThrottlingManager throttlingManager, TcpClient tcp)
        {
            throttlingManager.CannotBeNull();
            tcp.CannotBeNull();

            this.PieceData = null;

            this.tm  = throttlingManager;
            this.tcp = tcp;

            var data = new AsyncReadData(this.tcp.ReceiveBufferSize);

            this.stream = this.tcp.GetStream();
            this.stream.BeginRead(data.Buffer, data.OffsetStart, data.Buffer.Length, this.Receive, data);

            this.Endpoint = this.tcp.Client.RemoteEndPoint as IPEndPoint;
        }
Пример #3
0
        public void TestPeerTransfer()
        {
            TorrentInfo torrent;
            Peer        peer;
            IPEndPoint  endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.10"), 4009);
            TcpClient   tcp;

            byte[]            data;
            PieceManager      pm;
            ThrottlingManager tm;

            PieceStatus[] bitField;

            TorrentInfo.TryLoad(@"C:\Users\aljaz\Desktop\Torrent1\Torrent2.torrent", out torrent);

            tcp = new TcpClient();
            tcp.ReceiveBufferSize = 1024 * 1024;
            tcp.SendBufferSize    = 1024 * 1024;
            tcp.Connect(endpoint);

            data = new byte[torrent.Length];

            tm = new ThrottlingManager();
            tm.WriteSpeedLimit = 1024 * 1024;
            tm.ReadSpeedLimit  = 1024 * 1024;

            bitField = new PieceStatus[torrent.PiecesCount];

            pm = new PieceManager(torrent.InfoHash, torrent.Length, torrent.PieceHashes, torrent.PieceLength, torrent.BlockLength, bitField);
            pm.PieceCompleted += (sender, e) =>
            {
                Assert.AreEqual(torrent.PieceHashes.ElementAt(e.PieceIndex), e.PieceData.CalculateSha1Hash().ToHexaDecimalString());
            };

            peer = new Peer(new PeerCommunicator(tm, tcp), pm, "-UT3530-B9731F4C29D30E7DEA1F9FA7");
            peer.CommunicationErrorOccurred += (sender, e) =>
            {
                Assert.Fail();
            };

            Thread.Sleep(1000000);
        }
        public void TestTransferManager()
        {
            TorrentInfo        torrent;
            PersistenceManager pm;
            ThrottlingManager  tm;
            TransferManager    transfer;

            TorrentInfo.TryLoad(@"debian-9.9.0-amd64-netinst.torrent", out torrent);

            tm = new ThrottlingManager();
            tm.WriteSpeedLimit = 1024 * 1024;
            tm.ReadSpeedLimit  = 1024 * 1024;

            pm = new PersistenceManager(@"Test\", torrent.Length, torrent.PieceLength, torrent.PieceHashes, torrent.Files);

            transfer = new TransferManager(4000, torrent, tm, pm);
            transfer.Start();

            Thread.Sleep(1000000);
        }
        public void TestTransferManager()
        {
            TorrentInfo        torrent;
            PersistenceManager pm;
            ThrottlingManager  tm;
            TransferManager    transfer;

            TorrentInfo.TryLoad(@"C:\Users\aljaz\Desktop\Torrent1\Torrent2.torrent", out torrent);

            tm = new ThrottlingManager();
            tm.WriteSpeedLimit = 1024 * 1024;
            tm.ReadSpeedLimit  = 1024 * 1024;

            pm = new PersistenceManager(@"Test\", torrent.Length, torrent.PieceLength, torrent.PieceHashes, torrent.Files);

            transfer = new TransferManager(4000, torrent, tm, pm);
            transfer.Start();

            Thread.Sleep(1000000);
        }
Пример #6
0
 /// <summary>
 /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.  This method blocks until the throttled write operation has completed.
 /// </summary>
 /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
 /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
 /// <param name="count">The number of bytes to be written to the current stream.</param>
 /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
 /// <exception cref="T:System.NotSupportedException">The base stream does not support writing. </exception>
 /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
 /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
 /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
 /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
 public override void Write(byte[] buffer, int offset, int count)
 {
     ThrottlingManager.ThrottledWrite(this, buffer, offset, count);
 }
Пример #7
0
 /// <summary>
 /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.  This method blocks until the throttled read operation has completed.
 /// </summary>
 /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
 /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
 /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
 /// <returns>
 /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
 /// </returns>
 /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
 /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
 /// <exception cref="T:System.NotSupportedException">The base stream does not support reading. </exception>
 /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
 /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
 /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(ThrottlingManager.ThrottledRead(this, buffer, offset, count));
 }