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,
|
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")
|
||||||
|
|
|
@ -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
|
||||||
|
|
4
misc.go
4
misc.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue