Implement receiving cancel messages
This commit is contained in:
parent
8245f119ef
commit
036fd126e8
|
@ -425,6 +425,11 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error {
|
|||
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")
|
||||
|
|
|
@ -88,6 +88,18 @@ func (c *connection) Request(chunk Request) bool {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue