From: Matt Joiner Date: Sat, 20 Feb 2016 16:32:59 +0000 (+1100) Subject: Move dataReadAt to torrent.readAt, and do the waitNoPendingWrites check there X-Git-Tag: v1.0.0~874 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=e20858a1e86fb27623030400a5e5e5f8ad284dc1;p=btrtrc.git Move dataReadAt to torrent.readAt, and do the waitNoPendingWrites check there --- diff --git a/client.go b/client.go index 9b3c870b..612152ef 100644 --- a/client.go +++ b/client.go @@ -253,17 +253,6 @@ func (cl *Client) WriteStatus(_w io.Writer) { } } -// TODO: Make this non-blocking Read on Torrent. -func dataReadAt(d Data, b []byte, off int64) (n int, err error) { - // defer func() { - // if err == io.ErrUnexpectedEOF && n != 0 { - // err = nil - // } - // }() - // log.Println("data read at", len(b), off) - return d.ReadAt(b, off) -} - // Calculates the number of pieces to set to Readahead priority, after the // Now, and Next pieces. func readaheadPieces(readahead, pieceLength int64) (ret int) { @@ -1357,10 +1346,8 @@ func (me *Client) sendChunk(t *torrent, c *connection, r request) error { // Count the chunk being sent, even if it isn't. c.chunksSent++ b := make([]byte, r.Length) - tp := &t.Pieces[r.Index] - tp.waitNoPendingWrites() p := t.Info.Piece(int(r.Index)) - n, err := dataReadAt(t.data, b, p.Offset()+int64(r.Begin)) + n, err := t.readAt(b, p.Offset()+int64(r.Begin)) if n != len(b) { if err == nil { panic("expected error") diff --git a/reader.go b/reader.go index 571ce4f2..12caf4ca 100644 --- a/reader.go +++ b/reader.go @@ -125,14 +125,12 @@ func (r *Reader) readOnceAt(b []byte, pos int64) (n int, err error) { } b1 := b[:avail] pi := int(pos / r.t.Info().PieceLength) - tp := &r.t.torrent.Pieces[pi] ip := r.t.Info().Piece(pi) po := pos % ip.Length() if int64(len(b1)) > ip.Length()-po { b1 = b1[:ip.Length()-po] } - tp.waitNoPendingWrites() - n, err = dataReadAt(r.t.torrent.data, b1, pos) + n, err = r.t.torrent.readAt(b1, pos) if n != 0 { return } diff --git a/torrent.go b/torrent.go index 0fffe67f..daca65c0 100644 --- a/torrent.go +++ b/torrent.go @@ -1039,3 +1039,14 @@ func (t *torrent) putPieceInclination(pi []int) { func (t *torrent) updatePieceCompletion(piece int) { t.completedPieces.Set(piece, t.pieceCompleteUncached(piece)) } + +// Non-blocking read. Client lock is not required. +func (t *torrent) readAt(b []byte, off int64) (n int, err error) { + if off+int64(len(b)) > t.length { + b = b[:t.length-off] + } + for pi := off / t.Info.PieceLength; pi*t.Info.PieceLength < off+int64(len(b)); pi++ { + t.Pieces[pi].waitNoPendingWrites() + } + return t.data.ReadAt(b, off) +}