9 "github.com/anacrolix/missinggo"
10 "golang.org/x/net/context"
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.t.closed.IsSet() {
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 // Wait until some data should be available to read. Tickles the client if it
141 // isn't. Returns how much should be readable without blocking.
142 func (r *Reader) waitAvailable(pos, wanted int64, ctxErr *error) (avail int64) {
144 defer r.t.cl.mu.Unlock()
145 for !r.readable(pos) && *ctxErr == nil {
148 return r.available(pos, wanted)
151 // Performs at most one successful read to torrent storage.
152 func (r *Reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err error) {
153 if pos >= r.t.length {
158 avail := r.waitAvailable(pos, int64(len(b)), ctxErr)
160 if r.t.closed.IsSet() {
161 err = errors.New("torrent closed")
170 pi := int(pos / r.t.Info().PieceLength)
171 ip := r.t.Info().Piece(pi)
172 po := pos % ip.Length()
173 missinggo.LimitLen(&b1, ip.Length()-po)
174 n, err = r.t.readAt(b1, pos)
179 // log.Printf("%s: error reading from torrent storage pos=%d: %s", r.t, pos, err)
181 r.t.updateAllPieceCompletions()
182 r.t.updatePiecePriorities()
187 func (r *Reader) Close() error {
193 func (r *Reader) posChanged() {
195 defer r.t.cl.mu.Unlock()
199 func (r *Reader) Seek(off int64, whence int) (ret int64, err error) {
201 defer r.opMu.Unlock()
210 r.pos = r.t.info.TotalLength() + off
212 err = errors.New("bad whence")
221 func (r *Reader) Torrent() *Torrent {