Begin: msg.Begin,
Piece: p,
})
+ case peer_protocol.Cancel:
+ req := newRequest(msg.Index, msg.Begin, msg.Length)
+ if !conn.PeerCancel(req) {
+ log.Printf("received unexpected cancel: %v", req)
+ }
case peer_protocol.Bitfield:
if len(msg.Bitfield) < len(torrent.Pieces) {
err = errors.New("received invalid bitfield")
return true
}
+// Returns true if an unsatisfied request was canceled.
+func (c *connection) PeerCancel(r Request) bool {
+ if c.PeerRequests == nil {
+ return false
+ }
+ if _, ok := c.PeerRequests[r]; !ok {
+ return false
+ }
+ delete(c.PeerRequests, r)
+ return true
+}
+
func (c *connection) Unchoke() {
if !c.Choked {
return
chunkSpec
}
+func newRequest(index, begin, length peer_protocol.Integer) Request {
+ return Request{index, chunkSpec{begin, length}}
+}
+
type pieceByBytesPendingSlice struct {
Pending, Indices []peer_protocol.Integer
}