metainfo "github.com/nsf/libtorgo/torrent"
- "bitbucket.org/anacrolix/go.torrent/peer_protocol"
+ pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
"bitbucket.org/anacrolix/go.torrent/tracker"
_ "bitbucket.org/anacrolix/go.torrent/tracker/udp"
)
// Currently doesn't really queue, but should in the future.
-func (cl *Client) queuePieceCheck(t *torrent, pieceIndex peer_protocol.Integer) {
+func (cl *Client) queuePieceCheck(t *torrent, pieceIndex pp.Integer) {
piece := t.Pieces[pieceIndex]
if piece.QueuedForHash {
return
err = errors.New("unknown torrent")
return
}
- index := peer_protocol.Integer(off / t.MetaInfo.PieceLength)
+ index := pp.Integer(off / t.MetaInfo.PieceLength)
// Reading outside the bounds of a file is an error.
if index < 0 {
err = os.ErrInvalid
err = ErrDataNotReady
return
}
- pieceOff := peer_protocol.Integer(off % int64(t.PieceLength(0)))
+ pieceOff := pp.Integer(off % int64(t.PieceLength(0)))
high := int(t.PieceLength(index) - pieceOff)
if high < len(p) {
p = p[:high]
}()
go conn.writer()
go conn.writeOptimizer()
- conn.post <- peer_protocol.Bytes(peer_protocol.Protocol)
- conn.post <- peer_protocol.Bytes("\x00\x00\x00\x00\x00\x00\x00\x00")
+ conn.post <- pp.Bytes(pp.Protocol)
+ conn.post <- pp.Bytes("\x00\x00\x00\x00\x00\x00\x00\x00")
if torrent != nil {
- conn.post <- peer_protocol.Bytes(torrent.InfoHash[:])
- conn.post <- peer_protocol.Bytes(me.PeerId[:])
+ conn.post <- pp.Bytes(torrent.InfoHash[:])
+ conn.post <- pp.Bytes(me.PeerId[:])
}
var b [28]byte
_, err = io.ReadFull(conn.Socket, b[:])
err = fmt.Errorf("when reading protocol and extensions: %s", err)
return
}
- if string(b[:20]) != peer_protocol.Protocol {
+ if string(b[:20]) != pp.Protocol {
err = fmt.Errorf("wrong protocol: %#v", string(b[:20]))
return
}
if torrent == nil {
return
}
- conn.post <- peer_protocol.Bytes(torrent.InfoHash[:])
- conn.post <- peer_protocol.Bytes(me.PeerId[:])
+ conn.post <- pp.Bytes(torrent.InfoHash[:])
+ conn.post <- pp.Bytes(me.PeerId[:])
}
me.mu.Lock()
defer me.mu.Unlock()
return
}
if torrent.haveAnyPieces() {
- conn.Post(peer_protocol.Message{
- Type: peer_protocol.Bitfield,
+ conn.Post(pp.Message{
+ Type: pp.Bitfield,
Bitfield: torrent.bitfield(),
})
}
}
func (me *Client) connectionLoop(t *torrent, c *connection) error {
- decoder := peer_protocol.Decoder{
+ decoder := pp.Decoder{
R: bufio.NewReader(c.Socket),
MaxLength: 256 * 1024,
}
for {
me.mu.Unlock()
- var msg peer_protocol.Message
+ var msg pp.Message
err := decoder.Decode(&msg)
me.mu.Lock()
if err != nil {
continue
}
switch msg.Type {
- case peer_protocol.Choke:
+ case pp.Choke:
c.PeerChoked = true
c.Requests = nil
- case peer_protocol.Unchoke:
+ case pp.Unchoke:
c.PeerChoked = false
me.peerUnchoked(t, c)
me.replenishConnRequests(t, c)
- case peer_protocol.Interested:
+ case pp.Interested:
c.PeerInterested = true
// TODO: This should be done from a dedicated unchoking routine.
c.Unchoke()
- case peer_protocol.NotInterested:
+ case pp.NotInterested:
c.PeerInterested = false
c.Choke()
- case peer_protocol.Have:
+ case pp.Have:
me.peerGotPiece(t, c, int(msg.Index))
- case peer_protocol.Request:
+ case pp.Request:
if c.PeerRequests == nil {
c.PeerRequests = make(map[request]struct{}, maxRequests)
}
if n != int(msg.Length) {
return fmt.Errorf("bad request: %s", msg)
}
- c.Post(peer_protocol.Message{
- Type: peer_protocol.Piece,
+ c.Post(pp.Message{
+ Type: pp.Piece,
Index: msg.Index,
Begin: msg.Begin,
Piece: p,
})
- case peer_protocol.Cancel:
+ case pp.Cancel:
req := newRequest(msg.Index, msg.Begin, msg.Length)
if !c.PeerCancel(req) {
log.Printf("received unexpected cancel: %v", req)
}
- case peer_protocol.Bitfield:
+ case pp.Bitfield:
if len(msg.Bitfield) < len(t.Pieces) {
err = errors.New("received invalid bitfield")
break
me.peerGotPiece(t, c, index)
}
}
- case peer_protocol.Piece:
+ case pp.Piece:
err = me.downloadedChunk(t, c, &msg)
default:
err = fmt.Errorf("received unknown message type: %#v", msg.Type)
piece := &piece{}
copyHashSum(piece.Hash[:], hash)
t.Pieces = append(t.Pieces, piece)
- t.pendAllChunkSpecs(peer_protocol.Integer(len(t.Pieces) - 1))
+ t.pendAllChunkSpecs(pp.Integer(len(t.Pieces) - 1))
}
t.Trackers = make([][]tracker.Client, len(metaInfo.AnnounceList))
for tierIndex := range metaInfo.AnnounceList {
}
go func() {
for i := range torrent.Pieces {
- me.verifyPiece(torrent, peer_protocol.Integer(i))
+ me.verifyPiece(torrent, pp.Integer(i))
}
}()
}
}
-func (me *Client) downloadedChunk(t *torrent, c *connection, msg *peer_protocol.Message) error {
- req := newRequest(msg.Index, msg.Begin, peer_protocol.Integer(len(msg.Piece)))
+func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) error {
+ req := newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
// Request has been satisfied.
delete(c.Requests, req)
return me.dataWaiter
}
-func (me *Client) pieceHashed(t *torrent, piece peer_protocol.Integer, correct bool) {
+func (me *Client) pieceHashed(t *torrent, piece pp.Integer, correct bool) {
p := t.Pieces[piece]
p.EverHashed = true
if correct {
me.dataReady(dataSpec{
t.InfoHash,
request{
- peer_protocol.Integer(piece),
- chunkSpec{0, peer_protocol.Integer(t.PieceLength(piece))},
+ pp.Integer(piece),
+ chunkSpec{0, pp.Integer(t.PieceLength(piece))},
},
})
} else {
}
for _, conn := range t.Conns {
if correct {
- conn.Post(peer_protocol.Message{
- Type: peer_protocol.Have,
- Index: peer_protocol.Integer(piece),
+ conn.Post(pp.Message{
+ Type: pp.Have,
+ Index: pp.Integer(piece),
})
// TODO: Cancel requests for this piece.
} else {
me.event.Broadcast()
}
-func (cl *Client) verifyPiece(t *torrent, index peer_protocol.Integer) {
+func (cl *Client) verifyPiece(t *torrent, index pp.Integer) {
cl.mu.Lock()
p := t.Pieces[index]
for p.Hashing {