private void ParseSession(BDictionary contents, Torrent newTorrent, string sessionPath)
        {
            string piecesState = standardParser.Parse <BString>(contents.First().Value.EncodeAsBytes()).ToString();
            var    sharedFile  = new DownloadingFile(this, newTorrent, piecesState, sessionPath);

            filesList.AddLast(sharedFile);
        }
        public MainForm()
        {
            fileIsDownloading = false;

            standardParser = new BencodeParser();
            if (!RestoreSessionIfPresent())
            {
                filesList = new LinkedList <DownloadingFile>();
            }
            myPeerID = GeneratePeerID();
            DownloadingFile.messageHandler = new MessageHandler(400);
            if (!DownloadingFile.messageHandler.isStarted)
            {
                DownloadingFile.messageHandler.Start();
            }
            try
            {
                FileWorker.CreateMainSession();
            }
            catch
            {
                // cannot create new main session; it won't be saved
                // old file hasn't been rewritten, so restart the program
                // in new location or something
            }
            nowSelected = null;
            InitializeComponent();
        }
 private async Task AddNewTorrentAsync(DownloadingFile newSharedFile, bool start)
 {
     filesList.AddLast(newSharedFile);
     if (start && !fileIsDownloading)
     {
         fileIsDownloading = true;
         await newSharedFile.StartAsync().ConfigureAwait(false);
     }
 }
 public void UpdateSeedersLeechersNum(DownloadingFile sharedFile, int seeders, int leechers)
 {
     if (InvokeRequired)
     {
         Invoke(new MethodInvoker(() => FilesArea.Items[sharedFile.listViewEntryID].SubItems[4].Text =
                                      seeders + "/" + leechers));
     }
     else
     {
         FilesArea.Items[sharedFile.listViewEntryID].SubItems[4].Text = seeders + "/" + leechers;
     }
 }
        private void AddNewViewListEntry(DownloadingFile newSharedFile)
        {
            ListViewItem newFile = new ListViewItem(newSharedFile.torrentContents.DisplayName);

            newFile.SubItems.Add(GetAppropriateSizeForm(newSharedFile.totalSize));
            newFile.SubItems.Add("0.00%");
            newFile.SubItems.Add(STOPPEDMSG);
            newFile.SubItems.Add("");
            FilesArea.Items.Add(newFile);

            newSharedFile.listViewEntryID = FilesArea.Items.IndexOf(newFile);
        }
        private DownloadingFile FindEntryByIndex(int index)
        {
            DownloadingFile found = null;

            foreach (var sharedFile in filesList)
            {
                if (sharedFile.listViewEntryID == index)
                {
                    found = sharedFile;
                    break;
                }
            }
            return(found);
        }
        public void UpdateProgress(DownloadingFile sharedFile)
        {
            string curMsg = Math.Round(sharedFile.downloaded / (double)sharedFile.totalSize * 100, 2) + "%";

            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => FilesArea.Items[sharedFile.listViewEntryID].SubItems[2].Text =
                                             curMsg));
            }
            else
            {
                FilesArea.Items[sharedFile.listViewEntryID].SubItems[2].Text = curMsg;
            }
        }
 private void FilesArea_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
 {
     StartButton.Enabled      = StopButton.Enabled = RehashButton.Enabled =
         RemoveButton.Enabled = DeleteButton.Enabled = false;
     nowSelected = null;
     if (e.IsSelected)
     {
         nowSelected = FindEntryByIndex(e.ItemIndex);
         ChangeButtonsState();
         UpdateAllInfo(nowSelected.downloadPath, nowSelected.torrentContents, nowSelected.stringStatus);
     }
     else
     {
         UpdateAllInfo(null, null, null);
     }
 }
        public async void TorrentSubmitted(Torrent torrent, string chosenPath, bool start, string subDir)
        {
            DownloadingFile newSharedFile;

            try
            {
                newSharedFile = new DownloadingFile(this, torrent, chosenPath,
                                                    subDir == "" ? null : subDir, false);
            }
            catch
            {
                MessageBox.Show(this, "Could not create some or all of the files.\r\nMake sure the directory is readable" +
                                "and writeable, and that you have enough free disk space.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddNewViewListEntry(newSharedFile);
            await AddNewTorrentAsync(newSharedFile, start).ConfigureAwait(false);
        }
        public void PeerConnectedDisconnectedEvent(DownloadingFile sharedFile, int totalPeers)
        {
            string curMsg = "";

            if (totalPeers > 0)
            {
                if (sharedFile.state == DownloadState.completed)
                {
                    curMsg = SHARINGMSG;
                }
                else if (sharedFile.state == DownloadState.downloading)
                {
                    curMsg = DOWNLOADINGMSG;
                }
                else if (sharedFile.state == DownloadState.stopped)
                {
                    curMsg = STOPPEDMSG;
                }
            }
            else
            {
                if (sharedFile.state == DownloadState.completed || sharedFile.state == DownloadState.stopped)
                {
                    curMsg = STOPPEDMSG;
                }
                else
                {
                    curMsg = SEARCHINGPEERSMSG;
                }
            }

            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => FilesArea.Items[sharedFile.listViewEntryID].SubItems[3].Text =
                                             curMsg));
            }
            else
            {
                FilesArea.Items[sharedFile.listViewEntryID].SubItems[3].Text = curMsg;
            }
        }
 public void UpdateStatus(DownloadingFile sharedFile, string message)
 {
     if (InvokeRequired)
     {
         Invoke(new MethodInvoker(() => {
             if (message != null)
             {
                 FilesArea.Items[sharedFile.listViewEntryID].SubItems[3].Text = message;
             }
             UpdateAllInfo(sharedFile.downloadPath, sharedFile.torrentContents, message);
             ChangeButtonsState();
         }));
     }
     else
     {
         if (message != null)
         {
             FilesArea.Items[sharedFile.listViewEntryID].SubItems[3].Text = message;
         }
         UpdateAllInfo(sharedFile.downloadPath, sharedFile.torrentContents, message);
         ChangeButtonsState();
     }
 }
Example #12
0
        public async Task GetTrackerResponse(DownloadingFile downloadingFile, string peerID, int myPort,
                                             string eventStr, int numWant)
        {
            string baseURI       = downloadingFile.torrentContents.Trackers.ElementAt(0).ElementAt(0);
            string URIFormedHash = URIEncode(downloadingFile.torrentContents.OriginalInfoHashBytes);

            string requestURI = baseURI + '?' + "info_hash=" + URIFormedHash +
                                "&peer_id=" + peerID + "&port=" + myPort.ToString() +
                                "&uploaded=" + 0 + "&downloaded=" + downloadingFile.downloaded + "&left=" + (downloadingFile.totalSize -
                                                                                                             downloadingFile.downloaded) + "&numwant=" + numWant +
                                "&compact=1" + "&no_peer_id=1" + (eventStr == null ? "" : "&event=" + eventStr) +
                                (downloadingFile.trackerID == null ? "" : "&trackerid=" + downloadingFile.trackerID);

            byte[] response;
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Connection", "Close");
                client.DefaultRequestHeaders.Add("User-Agent", "VST0001");
                response = await client.GetByteArrayAsync(requestURI).ConfigureAwait(false);;
            }
            var parser = new BencodeParser();

            this.response = parser.Parse <BDictionary>(response);
        }