Implement receiving cancel messages

This commit is contained in:
Matt Joiner 2014-04-16 17:33:33 +10:00
parent 8245f119ef
commit 036fd126e8
3 changed files with 21 additions and 0 deletions

View File

@ -425,6 +425,11 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error {
Begin: msg.Begin, Begin: msg.Begin,
Piece: p, 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: case peer_protocol.Bitfield:
if len(msg.Bitfield) < len(torrent.Pieces) { if len(msg.Bitfield) < len(torrent.Pieces) {
err = errors.New("received invalid bitfield") err = errors.New("received invalid bitfield")

View File

@ -88,6 +88,18 @@ func (c *connection) Request(chunk Request) bool {
return true 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() { func (c *connection) Unchoke() {
if !c.Choked { if !c.Choked {
return return

View File

@ -65,6 +65,10 @@ type Request struct {
chunkSpec chunkSpec
} }
func newRequest(index, begin, length peer_protocol.Integer) Request {
return Request{index, chunkSpec{begin, length}}
}
type pieceByBytesPendingSlice struct { type pieceByBytesPendingSlice struct {
Pending, Indices []peer_protocol.Integer Pending, Indices []peer_protocol.Integer
} }