Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            int    port     = 5000;
            int    uiPort   = 5001;
            bool   runWebUi = false;
            string input    = null;
            string output   = null;
            bool   verbose  = false;

            ArgumentSyntax.Parse(args, syntax =>
            {
                syntax.DefineOption("p|port", ref port, "Port to listen for incoming connections on.");
                syntax.DefineOption("o|output", ref output, "Path to save downloaded files to.");
                syntax.DefineOption("v|verbose", ref verbose, "Show detailed logging information.");
                var uiPortArgument = syntax.DefineOption("ui", ref uiPort, false, "Run a web UI, optionally specifying the port to listen on (default: 5001).");
                runWebUi           = uiPortArgument.IsSpecified;

                syntax.DefineParameter("input", ref input, "Path of torrent file to download.");
            });

            if (verbose)
            {
                LogManager.Configure(factory => factory.AddConsole(LogLevel.Debug));
            }
            else
            {
                LogManager.Configure(factory => factory.AddConsole(LogLevel.Information));
            }

            var client = new TorrentClient(port);
            var extensionProtocolModule = new ExtensionProtocolModule();

            //extensionProtocolModule.RegisterMessageHandler(new PeerExchangeMessageHandler(client.AdapterAddress));
            extensionProtocolModule.RegisterMessageHandler(new MetadataMessageHandler());
            client.Modules.Register(extensionProtocolModule);

            if (runWebUi)
            {
                var uri = client.EnableWebUI(uiPort);
                Console.WriteLine($"Web UI started at {uri}");
            }

            var download = client.Add(input, output);

            download.Start();

            Console.WriteLine("Downloading...");

            download.WaitForDownloadCompletionAsync().Wait();
            Console.ReadKey();
        }
Exemplo n.º 2
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();
            }
        }