Exemplo n.º 1
0
        private void FormMain_Load(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            fd.Filter          = "Текстовые файлы (.txt)|*.txt|Все файлы (*.*)|*.*";
            fd.FilterIndex     = 0;
            fd.CheckFileExists = false;
            MemoryFileHandler fileHandler = null;

            if (MemoryFileHandler.CheckFileExistInMemory())
            {
                fileHandler = MemoryFileHandler.CreateInstance(fromMemory: true);
            }
            else
            {
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    fileHandler = MemoryFileHandler.CreateInstance(fd.FileName);
                }
                else
                {
                    Close();
                }
            }
            username                 = UsernameGenerator.CreateName();
            writeHandler             = new WriteHandler(username, fileHandler);
            readHandler              = new ReadHandler(fileHandler);
            readHandler.NewMessages += ReadHandler_NewMessage;
            readHandler.StartChecking();

            labelUsername.Text           = "Имя: " + username;
            textBoxMessageText.MaxLength = Encoding.Unicode.GetMaxCharCount(MessageModel.COUNT_TEXT);
        }
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();
            }
        }