бляяяяя

This commit is contained in:
itexpert228 2026-03-06 14:17:55 +03:00
parent 6194da2dba
commit 90583f66c7
No known key found for this signature in database
2 changed files with 31 additions and 13 deletions

View file

@ -73,7 +73,11 @@ func newPeerClient(ctx context.Context, addr string, infoHash [20]byte, peerID [
return nil, err
}
_ = pc.readInitialMessages(ctx)
if err := pc.readInitialMessages(ctx); err != nil {
conn.Close()
return nil, err
}
return pc, nil
}

View file

@ -186,21 +186,35 @@ func (ps *pieceScheduler) run(pieceCount 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 {
if state != piecePending {
continue
}
if pieceIndex >= len(have) {
continue
}
if have[pieceIndex] {
if pieceIndex < len(have) && have[pieceIndex] {
return pieceIndex
}
}
return -1
// Some peers send missing/truncated availability info; allow optimistic probing.
if len(have) == 0 || len(have) < len(states) {
return firstPending
}
return -1
}
func firstPendingPiece(states []pieceState) int {
for pieceIndex, state := range states {
if state == piecePending {
return pieceIndex