7 "github.com/anacrolix/missinggo"
9 "github.com/anacrolix/torrent/metainfo"
16 func NewClient(cl ClientImpl) *Client {
20 func (cl Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (*Torrent, error) {
21 t, err := cl.ci.OpenTorrent(info, infoHash)
22 return &Torrent{t}, err
29 func (t Torrent) Piece(p metainfo.Piece) Piece {
30 return Piece{t.TorrentImpl.Piece(p), p}
38 func (p Piece) WriteAt(b []byte, off int64) (n int, err error) {
39 // Callers should not be writing to completed pieces, but it's too
40 // expensive to be checking this on every single write using uncached
43 // c := p.Completion()
44 // if c.Ok && c.Complete {
45 // err = errors.New("piece already completed")
48 if off+int64(len(b)) > p.mip.Length() {
49 panic("write overflows piece")
51 b = missinggo.LimitLen(b, p.mip.Length()-off)
52 return p.PieceImpl.WriteAt(b, off)
55 func (p Piece) ReadAt(b []byte, off int64) (n int, err error) {
60 if off >= p.mip.Length() {
64 b = missinggo.LimitLen(b, p.mip.Length()-off)
68 n, err = p.PieceImpl.ReadAt(b, off)
73 if err == io.EOF && off < p.mip.Length() {
74 err = io.ErrUnexpectedEOF
76 if err == nil && off >= p.mip.Length() {
79 if n == 0 && err == nil {
80 err = io.ErrUnexpectedEOF
82 if off < p.mip.Length() && err != nil {