client.go | 32 ++++++++++++++++++-------------- cmd/magnet-metainfo/main.go | 2 +- torrent.go | 22 ++++++++-------------- torrent_test.go | 2 +- diff --git a/client.go b/client.go index e4549eeaf2b7ebd7759326c2c4ce19052a9bb187..7e5f2b4588865780d376a8d108b70f6c42b2f4ae 100644 --- a/client.go +++ b/client.go @@ -111,7 +111,7 @@ } firstIndex := int(off / int64(t.UsualPieceSize())) for i := 0; i < 5; i++ { index := firstIndex + i - if index >= t.NumPieces() { + if index >= t.numPieces() { continue } me.queueFirstHash(t, index) @@ -283,13 +283,13 @@ func (cl *Client) readRaisePiecePriorities(t *torrent, off, _len int64) { index := int(off / int64(t.UsualPieceSize())) cl.raisePiecePriority(t, index, piecePriorityNow) index++ - if index >= t.NumPieces() { + if index >= t.numPieces() { return } cl.raisePiecePriority(t, index, piecePriorityNext) for i := 0; i < t.numConnsUnchoked()-2; i++ { index++ - if index >= t.NumPieces() { + if index >= t.numPieces() { break } cl.raisePiecePriority(t, index, piecePriorityReadahead) @@ -523,7 +523,7 @@ l.Close() } me.event.Broadcast() for _, t := range me.torrents { - t.Close() + t.close() } me.mu.Unlock() } @@ -957,9 +957,9 @@ func (t *torrent) initRequestOrdering(c *connection) { if c.pieceRequestOrder != nil || c.piecePriorities != nil { panic("double init of request ordering") } - c.piecePriorities = mathRand.Perm(t.NumPieces()) + c.piecePriorities = mathRand.Perm(t.numPieces()) c.pieceRequestOrder = pieceordering.New() - for i := 0; i < t.NumPieces(); i++ { + for i := 0; i < t.numPieces(); i++ { if !c.PeerHasPiece(pp.Integer(i)) { continue } @@ -973,7 +973,7 @@ func (me *Client) peerGotPiece(t *torrent, c *connection, piece int) { if t.haveInfo() { if c.PeerPieces == nil { - c.PeerPieces = make([]bool, t.NumPieces()) + c.PeerPieces = make([]bool, t.numPieces()) } } else { for piece >= len(c.PeerPieces) { @@ -1206,11 +1206,11 @@ err = errors.New("received unexpected bitfield") break } if t.haveInfo() { - if len(msg.Bitfield) < t.NumPieces() { + if len(msg.Bitfield) < t.numPieces() { err = errors.New("received invalid bitfield") break } - msg.Bitfield = msg.Bitfield[:t.NumPieces()] + msg.Bitfield = msg.Bitfield[:t.numPieces()] } c.PeerPieces = msg.Bitfield for index, has := range c.PeerPieces { @@ -1632,6 +1632,10 @@ cl *Client *torrent } +func (t Torrent) NumPieces() int { + return t.numPieces() +} + func (t Torrent) Drop() { t.cl.dropTorrent(t.InfoHash) } @@ -1693,14 +1697,14 @@ } func (t Torrent) DownloadAll() { t.cl.mu.Lock() - for i := 0; i < t.NumPieces(); i++ { + for i := 0; i < t.numPieces(); i++ { // TODO: Leave higher priorities as they were? t.cl.prioritizePiece(t.torrent, i, piecePriorityNormal) } // Nice to have the first and last pieces soon for various interactive // purposes. t.cl.prioritizePiece(t.torrent, 0, piecePriorityReadahead) - t.cl.prioritizePiece(t.torrent, t.NumPieces()-1, piecePriorityReadahead) + t.cl.prioritizePiece(t.torrent, t.numPieces()-1, piecePriorityReadahead) t.cl.mu.Unlock() } @@ -1795,7 +1799,7 @@ if !ok { err = fmt.Errorf("no such torrent") return } - err = t.Close() + err = t.close() if err != nil { panic(err) } @@ -2039,7 +2043,7 @@ for _, t := range cl.torrents { if !t.haveInfo() { return false } - if t.NumPiecesCompleted() != t.NumPieces() { + if t.NumPiecesCompleted() != t.numPieces() { return false } } @@ -2206,7 +2210,7 @@ me.replenishConnRequests(t, conn) } } if t.haveAllPieces() && me.noUpload { - t.CeaseNetworking() + t.ceaseNetworking() } me.event.Broadcast() } diff --git a/cmd/magnet-metainfo/main.go b/cmd/magnet-metainfo/main.go index a356e80239d09adaf86fc9311690fd4b17d32860..fb2a5cecc19dfb04c213c6bcaa30f23bb6e03ff0 100644 --- a/cmd/magnet-metainfo/main.go +++ b/cmd/magnet-metainfo/main.go @@ -29,7 +29,7 @@ go func() { defer wg.Done() <-t.GotMetainfo mi := t.MetaInfo() - t.Close() + t.Drop() f, err := os.Create(mi.Info.Name + ".torrent") if err != nil { log.Fatalf("error creating torrent metainfo file: %s", err) diff --git a/torrent.go b/torrent.go index 41b97368e8f14be381523b05f83a81f42e153a71..a2901ba5c5188141952d062228376026f16281d2 100644 --- a/torrent.go +++ b/torrent.go @@ -107,7 +107,7 @@ heap.Init(wcs) return } -func (t *torrent) CeaseNetworking() { +func (t *torrent) ceaseNetworking() { t.stateMu.Lock() defer t.stateMu.Unlock() select { @@ -191,7 +191,7 @@ t.Pieces = append(t.Pieces, piece) } for _, conn := range t.Conns { t.initRequestOrdering(conn) - if err := conn.setNumPieces(t.NumPieces()); err != nil { + if err := conn.setNumPieces(t.numPieces()); err != nil { log.Printf("closing connection: %s", err) conn.Close() } @@ -410,7 +410,7 @@ func (t *torrent) BytesLeft() (left int64) { if !t.haveInfo() { return -1 } - for i := pp.Integer(0); i < pp.Integer(t.NumPieces()); i++ { + for i := pp.Integer(0); i < pp.Integer(t.numPieces()); i++ { left += int64(t.PieceNumPendingBytes(i)) } return @@ -424,21 +424,15 @@ func NumChunksForPiece(chunkSize int, pieceSize int) int { return (pieceSize + chunkSize - 1) / chunkSize } -func (t *torrent) ChunkCount() (num int) { - num += (t.NumPieces() - 1) * NumChunksForPiece(chunkSize, int(t.PieceLength(0))) - num += NumChunksForPiece(chunkSize, int(t.PieceLength(pp.Integer(t.NumPieces()-1)))) - return -} - func (t *torrent) UsualPieceSize() int { return int(t.Info.PieceLength) } func (t *torrent) LastPieceSize() int { - return int(t.PieceLength(pp.Integer(t.NumPieces() - 1))) + return int(t.PieceLength(pp.Integer(t.numPieces() - 1))) } -func (t *torrent) NumPieces() int { +func (t *torrent) numPieces() int { return len(t.Info.Pieces) / 20 } @@ -464,11 +458,11 @@ return false } } -func (t *torrent) Close() (err error) { +func (t *torrent) close() (err error) { if t.isClosed() { return } - t.CeaseNetworking() + t.ceaseNetworking() close(t.closing) t.dataLock.Lock() if t.Data != nil { @@ -565,7 +559,7 @@ Source peerSource } func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) { - if int(piece) == t.NumPieces()-1 { + if int(piece) == t.numPieces()-1 { len_ = pp.Integer(t.Length() % t.Info.PieceLength) } if len_ == 0 { diff --git a/torrent_test.go b/torrent_test.go index ffe43ca7cdd231848b94325386924ef26a875b91..ab92429bb06fb457c26b6451e6bb5a0bce8ff170 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -52,7 +52,7 @@ wg := sync.WaitGroup{} for i := 0; i < 2; i++ { wg.Add(1) go func() { - tt.Close() + tt.close() wg.Done() }() }