10 "github.com/anacrolix/missinggo"
11 "golang.org/x/net/context"
14 // Accesses torrent data via a client.
18 // Ensure operations that change the position are exclusive, like Read()
22 // Required when modifying pos and readahead, or reading them without
29 var _ io.ReadCloser = &Reader{}
31 // Don't wait for pieces to complete and be verified. Read calls return as
32 // soon as they can when the underlying chunks become available.
33 func (r *Reader) SetResponsive() {
37 // Configure the number of bytes ahead of a read that should also be
38 // prioritized in preparation for further reads.
39 func (r *Reader) SetReadahead(readahead int64) {
41 r.readahead = readahead
44 defer r.t.cl.mu.Unlock()
48 func (r *Reader) readable(off int64) (ret bool) {
49 if r.t.closed.IsSet() {
52 req, ok := r.t.offsetRequest(off)
57 return r.t.haveChunk(req)
59 return r.t.pieceComplete(int(req.Index))
62 // How many bytes are available to read. Max is the most we could require.
63 func (r *Reader) available(off, max int64) (ret int64) {
65 req, ok := r.t.offsetRequest(off)
69 if !r.t.haveChunk(req) {
72 len1 := int64(req.Length) - (off - r.t.requestOffset(req))
77 // Ensure that ret hasn't exceeded our original max.
84 func (r *Reader) tickleClient() {
88 func (r *Reader) waitReadable(off int64) {
89 // We may have been sent back here because we were told we could read but
95 func (r *Reader) Read(b []byte) (n int, err error) {
96 return r.ReadContext(b, context.Background())
99 func (r *Reader) ReadContext(b []byte, ctx context.Context) (n int, err error) {
100 // This is set under the Client lock if the Context is canceled.
102 if ctx.Done() != nil {
103 ctx, cancel := context.WithCancel(ctx)
104 // Abort the goroutine when the function returns.
110 r.t.cl.event.Broadcast()
114 // Hmmm, if a Read gets stuck, this means you can't change position for
115 // other purposes. That seems reasonable, but unusual.
117 defer r.opMu.Unlock()
120 n1, err = r.readOnceAt(b, r.pos, &ctxErr)
123 panic("expected error")
133 if r.pos >= r.t.length {
135 } else if err == io.EOF {
136 err = io.ErrUnexpectedEOF
141 // Wait until some data should be available to read. Tickles the client if it
142 // isn't. Returns how much should be readable without blocking.
143 func (r *Reader) waitAvailable(pos, wanted int64, ctxErr *error) (avail int64) {
145 defer r.t.cl.mu.Unlock()
146 for !r.readable(pos) && *ctxErr == nil {
149 return r.available(pos, wanted)
152 // Performs at most one successful read to torrent storage.
153 func (r *Reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err error) {
154 if pos >= r.t.length {
159 avail := r.waitAvailable(pos, int64(len(b)), ctxErr)
161 if r.t.closed.IsSet() {
162 err = errors.New("torrent closed")
171 pi := int(pos / r.t.Info().PieceLength)
172 ip := r.t.Info().Piece(pi)
173 po := pos % r.t.Info().PieceLength
174 missinggo.LimitLen(&b1, ip.Length()-po)
175 n, err = r.t.readAt(b1, pos)
180 log.Printf("%s: error reading from torrent storage pos=%d: %s", r.t, pos, err)
182 r.t.updateAllPieceCompletions()
183 r.t.updatePiecePriorities()
188 func (r *Reader) Close() error {
194 func (r *Reader) posChanged() {
196 defer r.t.cl.mu.Unlock()
200 func (r *Reader) Seek(off int64, whence int) (ret int64, err error) {
202 defer r.opMu.Unlock()
211 r.pos = r.t.info.TotalLength() + off
213 err = errors.New("bad whence")
222 func (r *Reader) Torrent() *Torrent {