w.WriteString("<missing metainfo>")
}
fmt.Fprint(w, "\n")
- t.WriteStatus(w)
+ t.writeStatus(w)
fmt.Fprintln(w)
}
}
return nil
}
+// Creates a new client. Clients contain zero or more Torrents.
func NewClient(cfg *Config) (cl *Client, err error) {
if cfg == nil {
cfg = &Config{}
}
}
+// Returns a handle to the given torrent, if it's present in the client.
func (cl *Client) Torrent(ih InfoHash) (T Torrent, ok bool) {
cl.mu.Lock()
defer cl.mu.Unlock()
log.Printf("got bad metadata piece")
break
}
- t.SaveMetadataPiece(piece, payload[begin:])
+ t.saveMetadataPiece(piece, payload[begin:])
c.UsefulChunksReceived++
c.lastUsefulChunkReceived = time.Now()
if !t.haveAllMetadataPieces() {
if !ok {
log.Printf("bad metadata_size type: %T", metadata_sizeUntyped)
} else {
- t.SetMetadataSize(metadata_size)
+ t.setMetadataSize(metadata_size)
}
}
if _, ok := c.PeerExtensionIDs["ut_metadata"]; ok {
t.Trackers = newTrackers
}
+// A handle to a live torrent within a Client.
type Torrent struct {
cl *Client
*torrent
t.cl.mu.Unlock()
}
+// Provides access to regions of torrent data that correspond to its files.
type File struct {
t Torrent
path string
}
}
-func (t Torrent) MetainfoFilepath() string {
- return filepath.Join(t.cl.ConfigDir(), "torrents", t.InfoHash.HexString()+".torrent")
-}
-
func (t Torrent) AddPeers(pp []Peer) error {
cl := t.cl
cl.mu.Lock()
return nil
}
+// Marks the entire torrent for download.
func (t Torrent) DownloadAll() {
t.cl.mu.Lock()
- for i := 0; i < t.numPieces(); i++ {
- // TODO: Leave higher priorities as they were?
- t.cl.prioritizePiece(t.torrent, i, piecePriorityNormal)
+ defer t.cl.mu.Unlock()
+ for i := range iter.N(t.numPieces()) {
+ t.cl.raisePiecePriority(t.torrent, i, piecePriorityNormal)
}
- // Nice to have the first and last pieces soon for various interactive
+ // Nice to have the first and last pieces sooner for various interactive
// purposes.
- t.cl.prioritizePiece(t.torrent, 0, piecePriorityReadahead)
- t.cl.prioritizePiece(t.torrent, t.numPieces()-1, piecePriorityReadahead)
- t.cl.mu.Unlock()
+ t.cl.raisePiecePriority(t.torrent, 0, piecePriorityReadahead)
+ t.cl.raisePiecePriority(t.torrent, t.numPieces()-1, piecePriorityReadahead)
}
func (me Torrent) ReadAt(p []byte, off int64) (n int, err error) {
"github.com/anacrolix/libtorgo/metainfo"
)
-func (t *torrent) PieceNumPendingBytes(index int) (count pp.Integer) {
+func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) {
if t.pieceComplete(index) {
return 0
}
t.Info = nil
}
-func (t *torrent) SaveMetadataPiece(index int, data []byte) {
+func (t *torrent) saveMetadataPiece(index int, data []byte) {
if t.haveInfo() {
return
}
return true
}
-func (t *torrent) SetMetadataSize(bytes int64) {
+func (t *torrent) setMetadataSize(bytes int64) {
if t.MetaData != nil {
return
}
return
}
-func (t *torrent) WriteStatus(w io.Writer) {
+func (t *torrent) writeStatus(w io.Writer) {
fmt.Fprintf(w, "Infohash: %x\n", t.InfoHash)
fmt.Fprintf(w, "Piece length: %s\n", func() string {
if t.haveInfo() {
return
}
+// Returns a run-time generated MetaInfo that includes the info bytes and
+// announce-list as currently known to the client.
func (t *torrent) MetaInfo() *metainfo.MetaInfo {
if t.MetaData == nil {
panic("info bytes not set")
return -1
}
for i := 0; i < t.numPieces(); i++ {
- left += int64(t.PieceNumPendingBytes(i))
+ left += int64(t.pieceNumPendingBytes(i))
}
return
}
func (t *torrent) piecePartiallyDownloaded(index int) bool {
- return t.PieceNumPendingBytes(index) != t.PieceLength(index)
+ return t.pieceNumPendingBytes(index) != t.PieceLength(index)
}
func numChunksForPiece(chunkSize int, pieceSize int) int {