10 "github.com/anacrolix/missinggo"
13 // Accesses torrent data via a client.
17 // Ensure operations that change the position are exclusive, like Read()
21 // Required when modifying pos and readahead, or reading them without
28 var _ io.ReadCloser = &Reader{}
30 // Don't wait for pieces to complete and be verified. Read calls return as
31 // soon as they can when the underlying chunks become available.
32 func (r *Reader) SetResponsive() {
36 // Configure the number of bytes ahead of a read that should also be
37 // prioritized in preparation for further reads.
38 func (r *Reader) SetReadahead(readahead int64) {
40 r.readahead = readahead
43 defer r.t.cl.mu.Unlock()
47 func (r *Reader) readable(off int64) (ret bool) {
48 if r.torrentClosed() {
51 req, ok := r.t.offsetRequest(off)
56 return r.t.haveChunk(req)
58 return r.t.pieceComplete(int(req.Index))
61 // How many bytes are available to read. Max is the most we could require.
62 func (r *Reader) available(off, max int64) (ret int64) {
64 req, ok := r.t.offsetRequest(off)
68 if !r.t.haveChunk(req) {
71 len1 := int64(req.Length) - (off - r.t.requestOffset(req))
76 // Ensure that ret hasn't exceeded our original max.
83 func (r *Reader) tickleClient() {
87 func (r *Reader) waitReadable(off int64) {
88 // We may have been sent back here because we were told we could read but
94 func (r *Reader) Read(b []byte) (n int, err error) {
95 return r.ReadContext(b, context.Background())
98 func (r *Reader) ReadContext(b []byte, ctx context.Context) (n int, err error) {
99 // This is set under the Client lock if the Context is canceled.
101 if ctx.Done() != nil {
102 ctx, cancel := context.WithCancel(ctx)
103 // Abort the goroutine when the function returns.
109 r.t.cl.event.Broadcast()
113 // Hmmm, if a Read gets stuck, this means you can't change position for
114 // other purposes. That seems reasonable, but unusual.
116 defer r.opMu.Unlock()
119 n1, err = r.readOnceAt(b, r.pos, &ctxErr)
122 panic("expected error")
132 if r.pos >= r.t.length {
134 } else if err == io.EOF {
135 err = io.ErrUnexpectedEOF
140 // Safe to call with or without client lock.
141 func (r *Reader) torrentClosed() bool {
142 return r.t.isClosed()
145 // Wait until some data should be available to read. Tickles the client if it
146 // isn't. Returns how much should be readable without blocking.
147 func (r *Reader) waitAvailable(pos, wanted int64, ctxErr *error) (avail int64) {
149 defer r.t.cl.mu.Unlock()
150 for !r.readable(pos) && *ctxErr == nil {
153 return r.available(pos, wanted)
156 // Performs at most one successful read to torrent storage.
157 func (r *Reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err error) {
158 if pos >= r.t.length {
163 avail := r.waitAvailable(pos, int64(len(b)), ctxErr)
165 if r.torrentClosed() {
166 err = errors.New("torrent closed")
175 pi := int(pos / r.t.Info().PieceLength)
176 ip := r.t.Info().Piece(pi)
177 po := pos % ip.Length()
178 missinggo.LimitLen(&b1, ip.Length()-po)
179 n, err = r.t.readAt(b1, pos)
184 // log.Printf("%s: error reading from torrent storage pos=%d: %s", r.t, pos, err)
186 r.t.updateAllPieceCompletions()
187 r.t.updatePiecePriorities()
192 func (r *Reader) Close() error {
198 func (r *Reader) posChanged() {
200 defer r.t.cl.mu.Unlock()
204 func (r *Reader) Seek(off int64, whence int) (ret int64, err error) {
206 defer r.opMu.Unlock()
215 r.pos = r.t.info.TotalLength() + off
217 err = errors.New("bad whence")
226 func (r *Reader) Torrent() *Torrent {