github.com/anacrolix/torrent@v1.61.0/types/types.go (about)

     1  // package types contains types that are used by the request strategy and the torrent package and
     2  // need to be shared between them.
     3  package types
     4  
     5  import (
     6  	"fmt"
     7  
     8  	pp "github.com/anacrolix/torrent/peer_protocol"
     9  )
    10  
    11  type PieceIndex = int
    12  
    13  // TODO: Where is the doc/spec calling it this?
    14  type ChunkSpec struct {
    15  	Begin, Length pp.Integer
    16  }
    17  
    18  type Request struct {
    19  	Index pp.Integer
    20  	ChunkSpec
    21  }
    22  
    23  func (r Request) String() string {
    24  	return fmt.Sprintf("piece %v, %v bytes at %v", r.Index, r.Length, r.Begin)
    25  }
    26  
    27  func (r Request) ToMsg(mt pp.MessageType) pp.Message {
    28  	return pp.Message{
    29  		Type:   mt,
    30  		Index:  r.Index,
    31  		Begin:  r.Begin,
    32  		Length: r.Length,
    33  	}
    34  }
    35  
    36  // Describes the importance of obtaining a particular piece.
    37  type PiecePriority byte
    38  
    39  func (pp *PiecePriority) Raise(maybe PiecePriority) bool {
    40  	if maybe > *pp {
    41  		*pp = maybe
    42  		return true
    43  	}
    44  	return false
    45  }
    46  
    47  const (
    48  	PiecePriorityNone      PiecePriority = iota // Not wanted. Must be the zero value.
    49  	PiecePriorityNormal                         // Wanted.
    50  	PiecePriorityHigh                           // Wanted a lot.
    51  	PiecePriorityReadahead                      // May be required soon.
    52  	// Deprecated. Succeeds a piece where a read occurred. This used to prioritize the earlier
    53  	// pieces in the readahead window. The request strategy now does this, so it's no longer needed.
    54  	// There is downstream code that still assumes it's in use.
    55  	PiecePriorityNext
    56  	PiecePriorityNow // A Reader is reading in this piece. Highest urgency.
    57  )