Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Place Peer info in listview in Windows forms #621

Open
rjclarkewp007 opened this issue Feb 2, 2023 · 8 comments
Open

Place Peer info in listview in Windows forms #621

rjclarkewp007 opened this issue Feb 2, 2023 · 8 comments

Comments

@rjclarkewp007
Copy link

Hey hey!

I would just like to know how I can place peer information into a listview for Windows forms please? :)

First of all, can I use manager.PeerConnected += Manager_PeerConnected; to get the info?
Secondly how do I then go about about adding the info for each peer into the list view, like peer IP, upload speed, download speed etc?

thanks!

@rjclarkewp007
Copy link
Author

So i managed to get it this far..

private async void Manager_PeerConnected(object sender, PeerConnectedEventArgs e)
        {

                foreach (PeerId peer in await manager.GetPeersAsync())
                {
                    string[] row = { peer.ClientApp.Client.ToString(), peer.Monitor.UploadSpeed.ToString(), peer.Monitor.DownloadSpeed.ToString(), peer.Monitor.DataBytesUploaded.ToString(), peer.Monitor.DataBytesDownloaded.ToString() };
                    var listViewItem = new ListViewItem(row);
                    listView1.Items.Add(listViewItem);
                }
            
        }

Seems to be showing up in my listview, but I have no idea how to refresh these values and also to remove the no longer connected peers hahaha. Any help will be much appreciated as always!
Also, I don't see an option to for peer IP address? (client address)

@jpmikkers
Copy link
Contributor

jpmikkers commented Feb 2, 2023

Updating the list on every peerconnected event is a bit overkill, what I do is just call getpeersasync() on a timer (once every second or so) and update the list then. Oh and the peer Uri property contains the peer address.

@rjclarkewp007
Copy link
Author

Ah okay thanks! Will do that as well.
Found that Host, thanks so much.

So only thing now is that it keeps adding the same IP over and over and not updating the one which already there... :(

image

I also need to figure out a way to convert the bytes to kb/mb/gb hehe

@jpmikkers
Copy link
Contributor

Speed conversion:
https://gist.github.com/jpmikkers/39caa99e930542dedc7c

@rjclarkewp007
Copy link
Author

rjclarkewp007 commented Feb 2, 2023

Thanks so much! That conversion seems to work!

So now I just need to figure out a way to update/refresh the stats and stop it from creating duplicates I guess..
I also probably only want it to show leachers and not the seeders as well..

image

I removed the code from peersconnected and rather set it up under a timer as you mentioned @jpmikkers.
Now, the stats are updating nicely hehe, but still creating a new line every time. :(

Using this now:

private async void getpeers_timer_Tick(object sender, EventArgs e)
        {
            foreach (PeerId peer in await manager.GetPeersAsync())
            {
                string[] row = { peer.Uri.Host, ConvertSpeed(peer.Monitor.UploadSpeed), ConvertSpeed(peer.Monitor.DownloadSpeed), ConvertSpeed(peer.Monitor.DataBytesUploaded), ConvertSpeed(peer.Monitor.DataBytesDownloaded), peer.BitField.PercentComplete.ToString() + "%" };
                var listViewItem = new ListViewItem(row);
                listView1.Items.Add(listViewItem);
            }
        }

image

@jpmikkers
Copy link
Contributor

jpmikkers commented Feb 2, 2023

If you keep the result from the previous call to GetPeersAsync(), it's easy to work out which peers disappeared (or updated), and you should remove (or update) the corresponding lines from your listview.

By the way, at this point your questions are more about 'how to do WinForm' so perhaps you can retire this issue as resolved ?

@rjclarkewp007
Copy link
Author

rjclarkewp007 commented Feb 8, 2023

Yeah sorry I am a complete noob here. So you lost me already there, but I'm using another alternative way to update the list view now. I am clearing the list with listView1.Items.Clear(); before populating it again hahaha.

private async void getpeers_timer_Tick(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            foreach (PeerId peer in await manager.GetPeersAsync())
            {
                string[] row = { peer.Uri.Host, ConvertSpeed(peer.Monitor.UploadSpeed), ConvertSpeed(peer.Monitor.DownloadSpeed), Convertbytes(peer.Monitor.DataBytesUploaded), Convertbytes(peer.Monitor.DataBytesDownloaded), peer.BitField.PercentComplete.ToString("F") + "%" };
                var listViewItem = new ListViewItem(row);
                listView1.Items.Add(listViewItem);
            }        
        }

I'm running this on a timer of 5 seconds. Will this cause problems to run the GetPeersAsync() every 5 seconds?
If not, well then I guess I will just use it like this for now.

I am having issues though and I think it might be since I started added this list view thing.
I am seeding all the time, but new leachers do not download from my application anymore. :(
To be more specific, other bittorrent clients seem to download from my application while I am seeding, but when I use my application to download the torrent while I am seeding from another computer (not on same network), then my application does not see my seeding app as a seeder.

Is there a way to force it to use certain trackers perhaps or does it load the trackers automatically from the torrent file as it was created? (torrent file was created in qBittorrent)

@rjclarkewp007
Copy link
Author

I have a feeling it has something to do with the downloading side seeing as the seeding side does seem to work when i download from other clients.

If you guys are at all able to tell me if this is the correct why to start a download and making sure it finds all the seeders then that will be great :) So sorry for being such a noob, i should probably just go study to a a developer. This is just a side hobby thing for me which i enjoy way too much!

private Thread downloadthread;

        private void downloadThreadStart()
        {
            downloadthread = new Thread(Updater);
            downloadthread.IsBackground = true;
            downloadthread.Start();
        }

        public async void Updater()
        {
            MethodInvoker StartingDownload = delegate
            {
                try
                {
                    label1.Text = "Checking files..please wait...";
                }
                catch (Exception)
                {

                }
            };

            //Download torrent file first
            startTorrentFileDownload();

            try
            {
                // Give an example of how settings can be modified for the engine.
                var settingBuilder = new EngineSettingsBuilder
                {
                    // Allow the engine to automatically forward ports using upnp/nat-pmp (if a compatible router is available)
                    AllowPortForwarding = true,

                    // Automatically save a cache of the DHT table when all torrents are stopped.
                    AutoSaveLoadDhtCache = true,

                    // Automatically save 'FastResume' data when TorrentManager.StopAsync is invoked, automatically load it
                    // before hash checking the torrent. Fast Resume data will be loaded as part of 'engine.AddAsync' if
                    // torrent metadata is available. Otherwise, if a magnetlink is used to download a torrent, fast resume
                    // data will be loaded after the metadata has been downloaded. 
                    AutoSaveLoadFastResume = true,

                    // If a MagnetLink is used to download a torrent, the engine will try to load a copy of the metadata
                    // it's cache directory. Otherwise the metadata will be downloaded and stored in the cache directory
                    // so it can be reloaded later.
                    AutoSaveLoadMagnetLinkMetadata = true,

                    AllowLocalPeerDiscovery = true,

                    MaximumConnections = 300,

                };

                var torrent = await Torrent.LoadAsync(torrentfile);
                dhtEngine = new DhtEngine(new IPEndPoint(IPAddress.Any, 0));
                TorrentEngine = new ClientEngine(settingBuilder.ToSettings());
                manager = await TorrentEngine.AddAsync(torrent, installdir);
                manager.TorrentStateChanged += Manager_TorrentStateChanged;

                label1.Invoke(StartingDownload);

                await dhtEngine.StartAsync();
                await manager.StartAsync();
                await manager.LocalPeerAnnounceAsync();
                await manager.DhtAnnounceAsync();

            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message + "\r\n" + e1.StackTrace);
            }

        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants