From c75dbccea3568db7f7ad31ad62cbeea89f1a8976 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 25 Feb 2015 15:42:47 +1100 Subject: [PATCH] Hide a whole bunch of functions seen via torrent --- client.go | 40 ++++++++++++++++++++-------------------- torrent.go | 36 ++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/client.go b/client.go index 1f823edc..c7507932 100644 --- a/client.go +++ b/client.go @@ -222,7 +222,7 @@ func (cl *Client) WriteStatus(_w io.Writer) { } fmt.Fprint(w, "\n") if t.haveInfo() { - fmt.Fprintf(w, "%f%% of %d bytes", 100*(1-float32(t.BytesLeft())/float32(t.Length())), t.Length()) + fmt.Fprintf(w, "%f%% of %d bytes", 100*(1-float32(t.bytesLeft())/float32(t.Length())), t.Length()) } else { w.WriteString("") } @@ -236,7 +236,7 @@ func (cl *Client) WriteStatus(_w io.Writer) { func (cl *Client) torrentReadAt(t *torrent, off int64, p []byte) (n int, err error) { cl.mu.Lock() defer cl.mu.Unlock() - index := int(off / int64(t.UsualPieceSize())) + index := int(off / int64(t.usualPieceSize())) // Reading outside the bounds of a file is an error. if index < 0 { err = os.ErrInvalid @@ -247,7 +247,7 @@ func (cl *Client) torrentReadAt(t *torrent, off int64, p []byte) (n int, err err return } piece := t.Pieces[index] - pieceOff := pp.Integer(off % int64(t.UsualPieceSize())) + pieceOff := pp.Integer(off % int64(t.usualPieceSize())) pieceLeft := int(t.PieceLength(pp.Integer(index)) - pieceOff) if pieceLeft <= 0 { err = io.EOF @@ -267,7 +267,7 @@ func (cl *Client) torrentReadAt(t *torrent, off int64, p []byte) (n int, err err } func (cl *Client) readRaisePiecePriorities(t *torrent, off, _len int64) { - index := int(off / int64(t.UsualPieceSize())) + index := int(off / int64(t.usualPieceSize())) cl.raisePiecePriority(t, index, piecePriorityNow) index++ if index >= t.numPieces() { @@ -1017,8 +1017,8 @@ func (cl *Client) requestPendingMetadata(t *torrent, c *connection) { return } var pending []int - for index := 0; index < t.MetadataPieceCount(); index++ { - if !t.HaveMetadataPiece(index) { + for index := 0; index < t.metadataPieceCount(); index++ { + if !t.haveMetadataPiece(index) { pending = append(pending, index) } } @@ -1047,14 +1047,14 @@ func (cl *Client) completedMetadata(t *torrent) { CopyExact(&ih, h.Sum(nil)) if ih != t.InfoHash { log.Print("bad metadata") - t.InvalidateMetadata() + t.invalidateMetadata() return } var info metainfo.Info err := bencode.Unmarshal(t.MetaData, &info) if err != nil { log.Printf("error unmarshalling metadata: %s", err) - t.InvalidateMetadata() + t.invalidateMetadata() return } // TODO(anacrolix): If this fails, I think something harsher should be @@ -1062,7 +1062,7 @@ func (cl *Client) completedMetadata(t *torrent) { err = cl.setMetaData(t, info, t.MetaData) if err != nil { log.Printf("error setting metadata: %s", err) - t.InvalidateMetadata() + t.invalidateMetadata() return } log.Printf("%s: got metadata from peers", t) @@ -1095,17 +1095,17 @@ func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *torrent, c *connect t.SaveMetadataPiece(piece, payload[begin:]) c.UsefulChunksReceived++ c.lastUsefulChunkReceived = time.Now() - if !t.HaveAllMetadataPieces() { + if !t.haveAllMetadataPieces() { break } cl.completedMetadata(t) case pp.RequestMetadataExtensionMsgType: - if !t.HaveMetadataPiece(piece) { - c.Post(t.NewMetadataExtensionMessage(c, pp.RejectMetadataExtensionMsgType, d["piece"], nil)) + if !t.haveMetadataPiece(piece) { + c.Post(t.newMetadataExtensionMessage(c, pp.RejectMetadataExtensionMsgType, d["piece"], nil)) break } start := (1 << 14) * piece - c.Post(t.NewMetadataExtensionMessage(c, pp.DataMetadataExtensionMsgType, piece, t.MetaData[start:start+t.metadataPieceSize(piece)])) + c.Post(t.newMetadataExtensionMessage(c, pp.DataMetadataExtensionMsgType, piece, t.MetaData[start:start+t.metadataPieceSize(piece)])) case pp.RejectMetadataExtensionMsgType: default: err = errors.New("unknown msg_type value") @@ -1685,7 +1685,7 @@ type FilePieceState struct { } func (f *File) Progress() (ret []FilePieceState) { - pieceSize := int64(f.t.UsualPieceSize()) + pieceSize := int64(f.t.usualPieceSize()) off := f.offset % pieceSize remaining := f.length for i := int(f.offset / pieceSize); ; i++ { @@ -1739,7 +1739,7 @@ func (t Torrent) Files() (ret []File) { func (t Torrent) SetRegionPriority(off, len int64) { t.cl.mu.Lock() defer t.cl.mu.Unlock() - pieceSize := int64(t.UsualPieceSize()) + pieceSize := int64(t.usualPieceSize()) for i := off / pieceSize; i*pieceSize < off+len; i++ { t.cl.prioritizePiece(t.torrent, int(i), piecePriorityNormal) } @@ -2057,7 +2057,7 @@ func (cl *Client) announceTorrentTrackers(t *torrent) { return } cl.mu.RLock() - req.Left = t.BytesLeft() + req.Left = t.bytesLeft() trackers := t.Trackers cl.mu.RUnlock() if cl.announceTorrentTrackersFastStart(&req, trackers, t) { @@ -2066,7 +2066,7 @@ func (cl *Client) announceTorrentTrackers(t *torrent) { newAnnounce: for cl.waitWantPeers(t) { cl.mu.RLock() - req.Left = t.BytesLeft() + req.Left = t.bytesLeft() trackers = t.Trackers cl.mu.RUnlock() numTrackersTried := 0 @@ -2101,7 +2101,7 @@ func (cl *Client) allTorrentsCompleted() bool { if !t.haveInfo() { return false } - if t.NumPiecesCompleted() != t.numPieces() { + if t.numPiecesCompleted() != t.numPieces() { return false } } @@ -2154,7 +2154,7 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er c.lastUsefulChunkReceived = time.Now() // Write the chunk out. - err := t.WriteChunk(int(msg.Index), int64(msg.Begin), msg.Piece) + err := t.writeChunk(int(msg.Index), int64(msg.Begin), msg.Piece) if err != nil { return fmt.Errorf("error writing chunk: %s", err) } @@ -2241,7 +2241,7 @@ func (cl *Client) verifyPiece(t *torrent, index pp.Integer) { p.Hashing = true p.QueuedForHash = false cl.mu.Unlock() - sum := t.HashPiece(index) + sum := t.hashPiece(index) cl.mu.Lock() select { case <-t.closing: diff --git a/torrent.go b/torrent.go index e962fdca..6b2bb053 100644 --- a/torrent.go +++ b/torrent.go @@ -136,7 +136,7 @@ func (t *torrent) AddPeers(pp []Peer) { } } -func (t *torrent) InvalidateMetadata() { +func (t *torrent) invalidateMetadata() { t.MetaData = nil t.metadataHave = nil t.Info = nil @@ -154,11 +154,11 @@ func (t *torrent) SaveMetadataPiece(index int, data []byte) { t.metadataHave[index] = true } -func (t *torrent) MetadataPieceCount() int { +func (t *torrent) metadataPieceCount() int { return (len(t.MetaData) + (1 << 14) - 1) / (1 << 14) } -func (t *torrent) HaveMetadataPiece(piece int) bool { +func (t *torrent) haveMetadataPiece(piece int) bool { if t.haveInfo() { return (1<<14)*piece < len(t.MetaData) } else { @@ -214,7 +214,7 @@ func (t *torrent) setStorage(td TorrentData) (err error) { return } -func (t *torrent) HaveAllMetadataPieces() bool { +func (t *torrent) haveAllMetadataPieces() bool { if t.haveInfo() { return true } @@ -261,7 +261,7 @@ func (t *torrent) pieceStatusChar(index int) byte { return 'H' case !p.EverHashed: return '?' - case t.PiecePartiallyDownloaded(index): + case t.piecePartiallyDownloaded(index): switch p.Priority { case piecePriorityNone: return 'F' // Forgotten @@ -288,7 +288,7 @@ func (t *torrent) metadataPieceSize(piece int) int { return metadataPieceSize(len(t.MetaData), piece) } -func (t *torrent) NewMetadataExtensionMessage(c *connection, msgType int, piece int, data []byte) pp.Message { +func (t *torrent) newMetadataExtensionMessage(c *connection, msgType int, piece int, data []byte) pp.Message { d := map[string]int{ "msg_type": msgType, "piece": piece, @@ -350,7 +350,7 @@ 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 fmt.Sprint(t.UsualPieceSize()) + return fmt.Sprint(t.usualPieceSize()) } else { return "?" } @@ -394,7 +394,7 @@ func (t *torrent) haveInfo() bool { } // TODO: Include URIs that weren't converted to tracker clients. -func (t *torrent) AnnounceList() (al [][]string) { +func (t *torrent) announceList() (al [][]string) { for _, tier := range t.Trackers { var l []string for _, tr := range tier { @@ -417,11 +417,11 @@ func (t *torrent) MetaInfo() *metainfo.MetaInfo { CreationDate: time.Now().Unix(), Comment: "dynamic metainfo from client", CreatedBy: "go.torrent", - AnnounceList: t.AnnounceList(), + AnnounceList: t.announceList(), } } -func (t *torrent) BytesLeft() (left int64) { +func (t *torrent) bytesLeft() (left int64) { if !t.haveInfo() { return -1 } @@ -431,7 +431,7 @@ func (t *torrent) BytesLeft() (left int64) { return } -func (t *torrent) PiecePartiallyDownloaded(index int) bool { +func (t *torrent) piecePartiallyDownloaded(index int) bool { return t.PieceNumPendingBytes(pp.Integer(index)) != t.PieceLength(pp.Integer(index)) } @@ -439,11 +439,11 @@ func NumChunksForPiece(chunkSize int, pieceSize int) int { return (pieceSize + chunkSize - 1) / chunkSize } -func (t *torrent) UsualPieceSize() int { +func (t *torrent) usualPieceSize() int { return int(t.Info.PieceLength) } -func (t *torrent) LastPieceSize() int { +func (t *torrent) lastPieceSize() int { return int(t.PieceLength(pp.Integer(t.numPieces() - 1))) } @@ -451,7 +451,7 @@ func (t *torrent) numPieces() int { return len(t.Info.Pieces) / 20 } -func (t *torrent) NumPiecesCompleted() (num int) { +func (t *torrent) numPiecesCompleted() (num int) { for _, p := range t.Pieces { if p.Complete() { num++ @@ -515,7 +515,7 @@ func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) } func (t *torrent) requestOffset(r request) int64 { - return torrentRequestOffset(t.Length(), int64(t.UsualPieceSize()), r) + return torrentRequestOffset(t.Length(), int64(t.usualPieceSize()), r) } // Return the request that would include the given offset into the torrent data. @@ -523,7 +523,7 @@ func (t *torrent) offsetRequest(off int64) (req request, ok bool) { return torrentOffsetRequest(t.Length(), t.Info.PieceLength, chunkSize, off) } -func (t *torrent) WriteChunk(piece int, begin int64, data []byte) (err error) { +func (t *torrent) writeChunk(piece int, begin int64, data []byte) (err error) { _, err = t.data.WriteAt(data, int64(piece)*t.Info.PieceLength+begin) return } @@ -580,7 +580,7 @@ func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) { return } -func (t *torrent) HashPiece(piece pp.Integer) (ps pieceSum) { +func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) { hash := pieceHash.New() t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength) util.CopyExact(ps[:], hash.Sum(nil)) @@ -646,7 +646,7 @@ func (t *torrent) connHasWantedPieces(c *connection) bool { } func (t *torrent) extentPieces(off, _len int64) (pieces []int) { - for i := off / int64(t.UsualPieceSize()); i*int64(t.UsualPieceSize()) < off+_len; i++ { + for i := off / int64(t.usualPieceSize()); i*int64(t.usualPieceSize()) < off+_len; i++ { pieces = append(pieces, int(i)) } return