1
0
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:
Jesse
2019-12-30 10:19:09 -05:00
parent 46b55339aa
commit 928bbfeab4

View File

@@ -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