예제 #1
0
        public async Task Test()
        {
            // Create a 256kB file
            var fileData = Enumerable.Repeat(Enumerable.Range(0, 255).Select(x => (byte)x), 1000)
                           .SelectMany(x => x).ToArray();

            var metainfo = new MetainfoBuilder("Test")
                           .AddFile("file.dat", fileData)
                           .Build();

            var tracker = new MockTracker();

            var sourceFiles = new MemoryFileHandler("file.dat", fileData);

            var seed         = new TorrentClient(0);
            var seedDownload = seed.Add(metainfo, tracker.CreateTrackerClient(null), sourceFiles);

            tracker.RegisterPeer(seed.Transport.Port);

            var peer            = new TorrentClient(0);
            var peerFileHandler = new MemoryFileHandler();
            var peerDownload    = peer.Add(metainfo, tracker.CreateTrackerClient(null), peerFileHandler);

            seedDownload.Start();
            peerDownload.Start();

            try
            {
                await peerDownload.WaitForDownloadCompletionAsync();

                // Verify downloaded data
                var resultFile = peerFileHandler.GetFileStream("file.dat") as MemoryStream;
                Assert.That(resultFile, Is.Not.Null, "Result file does not exist.");
                Assert.That(resultFile.ToArray().SequenceEqual(fileData), "Downloaded data is not correct.");
            }
            finally
            {
                seed.Dispose();
                peer.Dispose();
            }
        }
예제 #2
0
        private void Shutdown()
        {
            var fastResume = new BEncodedDictionary();

            foreach (var torrentManager in Managers)
            {
                torrentManager.Stop();
                while (torrentManager.State != TorrentState.Stopped)
                {
                    Console.WriteLine("{0} is {1}", torrentManager.Torrent.Name, torrentManager.State);
                    Thread.Sleep(250);
                }

                fastResume.Add(torrentManager.Torrent.InfoHash.ToHex(), torrentManager.SaveFastResume().Encode());
            }

//#if !DISABLE_DHT
//            File.WriteAllBytes(_dhtNodeFile, _engine.DhtEngine.SaveNodes());
//#endif
            File.WriteAllBytes(Path.Combine(Options.Client.Path, "fastresume.data"), fastResume.Encode());
            TorrentClient.Dispose();

            Thread.Sleep(2000);
        }