7 "github.com/anacrolix/missinggo"
8 "github.com/anacrolix/torrent/metainfo"
15 func NewClient(cl ClientImpl) *Client {
19 func (cl Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (*Torrent, error) {
20 t, err := cl.ci.OpenTorrent(info, infoHash)
21 return &Torrent{t}, err
28 func (t Torrent) Piece(p metainfo.Piece) Piece {
29 return Piece{t.TorrentImpl.Piece(p), p}
37 func (p Piece) WriteAt(b []byte, off int64) (n int, err error) {
38 // Callers should not be writing to completed pieces, but it's too
39 // expensive to be checking this on every single write using uncached
42 // c := p.Completion()
43 // if c.Ok && c.Complete {
44 // err = errors.New("piece already completed")
47 if off+int64(len(b)) > p.mip.Length() {
48 panic("write overflows piece")
50 b = missinggo.LimitLen(b, p.mip.Length()-off)
51 return p.PieceImpl.WriteAt(b, off)
54 func (p Piece) ReadAt(b []byte, off int64) (n int, err error) {
59 if off >= p.mip.Length() {
63 b = missinggo.LimitLen(b, p.mip.Length()-off)
67 n, err = p.PieceImpl.ReadAt(b, off)
72 if err == io.EOF && off < p.mip.Length() {
73 err = io.ErrUnexpectedEOF
75 if err == nil && off >= p.mip.Length() {
78 if n == 0 && err == nil {
79 err = io.ErrUnexpectedEOF
81 if off < p.mip.Length() && err != nil {