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,28 +113,22 @@ func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) {
defer c.conn.SetDeadline(time.Time{}) // Disable the deadline defer c.conn.SetDeadline(time.Time{}) // Disable the deadline
for state.downloaded < pw.length { for state.downloaded < pw.length {
// Block and consume messages until not choked // If unchoked, send requests until we have enough unfulfilled requests
if c.choked { if !state.client.choked {
err := readMessages(&state) for state.backlog < MaxBacklog && state.requested < pw.length {
if err != nil { blockSize := MaxBlockSize
return nil, err // Last block might be shorter than the typical block
if pw.length-state.requested < blockSize {
blockSize = pw.length - state.requested
}
c.request(pw.index, state.requested, blockSize)
state.backlog++
state.requested += blockSize
} }
continue
}
// Send requests until we have enough unfulfilled requests
for state.backlog < MaxBacklog && state.requested < pw.length {
blockSize := MaxBlockSize
// Last block might be shorter than the typical block
if pw.length-state.requested < blockSize {
blockSize = pw.length - state.requested
}
c.request(pw.index, state.requested, blockSize)
state.backlog++
state.requested += blockSize
} }
// Wait until we receive at least one message, and consume them
err := readMessages(&state) err := readMessages(&state)
if err != nil { if err != nil {
return nil, err return nil, err