You've already forked torrent-client
mirror of
https://github.com/veggiedefender/torrent-client.git
synced 2025-11-06 09:29:16 +02:00
Simplify download algorithm
This commit is contained in:
14
p2p/p2p.go
14
p2p/p2p.go
@@ -113,16 +113,8 @@ func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) {
|
||||
defer c.conn.SetDeadline(time.Time{}) // Disable the deadline
|
||||
|
||||
for state.downloaded < pw.length {
|
||||
// Block and consume messages until not choked
|
||||
if c.choked {
|
||||
err := readMessages(&state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Send requests until we have enough unfulfilled requests
|
||||
// If unchoked, send requests until we have enough unfulfilled requests
|
||||
if !state.client.choked {
|
||||
for state.backlog < MaxBacklog && state.requested < pw.length {
|
||||
blockSize := MaxBlockSize
|
||||
// Last block might be shorter than the typical block
|
||||
@@ -134,7 +126,9 @@ func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) {
|
||||
state.backlog++
|
||||
state.requested += blockSize
|
||||
}
|
||||
}
|
||||
|
||||
// Wait until we receive at least one message, and consume them
|
||||
err := readMessages(&state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user