anacrolix / torrent

Full-featured BitTorrent client package and utilities
Mozilla Public License 2.0
5.41k stars 617 forks source link

Source code entry point questions #829

Closed whr819987540 closed 1 year ago

whr819987540 commented 1 year ago

Recently, I have been using this project. Specifically, it's a library. I use the cmd/torrent module in it to learn how this library works.

The command is like this:

cd cmd/torrent
go mod init torrentmain
go mod tidy
go build
./torrentmain download The.Mandalorian.S03E06.WEB.x264-TORRENTGALAXY.torrent

I got a few questions when I was debugging.

The download.go/downloadErr function calls the addTorrents function, which marks the entire torrent to be downloaded by calling t.DownloadAll().

As the t.DownloadAll() just announces which pieces should be downloaded, the actual download work is done by some goroutines (I guess).

What I would like to know is in which submodule it downloads data and how it downloads data.

anacrolix commented 1 year ago

The answer to that could be very involved. To address specifically the hand off from DownloadAll to continuing to download data, this occurs here, after DownloadAll marks all pieces for download, which eventually calls through to Torrent.initiateConn. If there are no peers after synchronously calling through to here, the Torrent is in a state that DHT and tracker announces will begin, and when those deliver peers connections will be initiated to those in the same way. Every time events occur relating to peers, data being received, and piece state changes, new connections, announces, and requests are sent as appropriate.

whr819987540 commented 1 year ago

I am sorry that I may not illustrate my question accurately. I would like to know after this project receives a piece from a peer through the Internet, which part processes the piece, like writing it the disk or something.

anacrolix commented 1 year ago

The spot right here is where a chunk is written. The enclosing method handles chunks as they're received.

https://github.com/anacrolix/torrent/blob/fdb0911e28c68fcb703c00d6b21230894baf1193/peer.go#L718

mborawi commented 1 year ago

Works

whr819987540 commented 1 year ago

The function you provided gives me a perfect entry point to debug and then understand the total process that the client receives a chunk, use ClientImpl to determine how the WriteAt function acts.

Therefore, I implements the ClientImpl interface, create my own MemoryClientImpl type and define my own WriteAt and ReadAt function.

As I am new to GoLang, this really takes my a lot time. After finishing this, I find GoLang's interface design is really flexible and interesting.

By the way, could you please give me another entry point which is the function that reads a specific part from the file(on the disk) and give it to a specific peer through the network?

Best wishes. Happy Labor Day.

anacrolix commented 1 year ago

https://github.com/anacrolix/torrent/blob/22b6252e4ffb93a828dc9bff67bd2c92a65aaa32/peerconn.go#L785 https://github.com/anacrolix/torrent/blob/22b6252e4ffb93a828dc9bff67bd2c92a65aaa32/peerconn.go#L583 https://github.com/anacrolix/torrent/blob/22b6252e4ffb93a828dc9bff67bd2c92a65aaa32/peerconn.go#L996

Go has implicit interfaces, which lends itself to structural typing, if that helps you in your understanding of it. It has advantages, but plenty of disadvantages too.

Please feel free to contribute back anything you find wrong, or was improved for your purposes.