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")
134 if r.pos >= r.t.length {
136 } else if err == io.EOF {
137 err = io.ErrUnexpectedEOF
142 // Wait until some data should be available to read. Tickles the client if it
143 // isn't. Returns how much should be readable without blocking.
144 func (r *Reader) waitAvailable(pos, wanted int64, ctxErr *error) (avail int64) {
146 defer r.t.cl.mu.Unlock()
147 for !r.readable(pos) && *ctxErr == nil {
150 return r.available(pos, wanted)
153 // Performs at most one successful read to torrent storage.
154 func (r *Reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err error) {
155 if pos >= r.t.length {
160 avail := r.waitAvailable(pos, int64(len(b)), ctxErr)
162 if r.t.closed.IsSet() {
163 err = errors.New("torrent closed")
172 pi := int(pos / r.t.Info().PieceLength)
173 ip := r.t.Info().Piece(pi)
174 po := pos % r.t.Info().PieceLength
175 missinggo.LimitLen(&b1, ip.Length()-po)
176 n, err = r.t.readAt(b1, pos)
181 log.Printf("error reading torrent %q piece %d offset %d, %d bytes: %s", r.t, pi, po, len(b1), err)
183 r.t.updateAllPieceCompletions()
184 r.t.updatePiecePriorities()
189 func (r *Reader) Close() error {
195 func (r *Reader) posChanged() {
199 func (r *Reader) Seek(off int64, whence int) (ret int64, err error) {
201 defer r.opMu.Unlock()
211 r.pos = r.t.info.TotalLength() + off
213 err = errors.New("bad whence")
221 func (r *Reader) Torrent() *Torrent {