бляяяяя
This commit is contained in:
parent
6194da2dba
commit
90583f66c7
2 changed files with 31 additions and 13 deletions
|
|
@ -73,7 +73,11 @@ func newPeerClient(ctx context.Context, addr string, infoHash [20]byte, peerID [
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = pc.readInitialMessages(ctx)
|
if err := pc.readInitialMessages(ctx); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return pc, nil
|
return pc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,21 +186,35 @@ func (ps *pieceScheduler) run(pieceCount int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func selectPendingPiece(states []pieceState, have []bool, hasInfo bool) int {
|
func selectPendingPiece(states []pieceState, have []bool, hasInfo bool) int {
|
||||||
if hasInfo {
|
firstPending := firstPendingPiece(states)
|
||||||
|
if firstPending < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// If peer has not sent bitfield/have yet, treat it as potentially having any piece.
|
||||||
|
if !hasInfo {
|
||||||
|
return firstPending
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prefer pieces explicitly advertised by peer.
|
||||||
for pieceIndex, state := range states {
|
for pieceIndex, state := range states {
|
||||||
if state != piecePending {
|
if state != piecePending {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if pieceIndex >= len(have) {
|
if pieceIndex < len(have) && have[pieceIndex] {
|
||||||
continue
|
|
||||||
}
|
|
||||||
if have[pieceIndex] {
|
|
||||||
return pieceIndex
|
return pieceIndex
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Some peers send missing/truncated availability info; allow optimistic probing.
|
||||||
|
if len(have) == 0 || len(have) < len(states) {
|
||||||
|
return firstPending
|
||||||
|
}
|
||||||
|
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func firstPendingPiece(states []pieceState) int {
|
||||||
for pieceIndex, state := range states {
|
for pieceIndex, state := range states {
|
||||||
if state == piecePending {
|
if state == piecePending {
|
||||||
return pieceIndex
|
return pieceIndex
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue