Abstract out making a cancel message

This commit is contained in:
Matt Joiner 2017-09-18 13:42:42 +10:00
parent 9e6cdff175
commit 3ed8274384
3 changed files with 19 additions and 6 deletions

View File

@ -355,12 +355,7 @@ func (cn *connection) fillWriteBuffer(msg func(pp.Message) bool) {
for r := range cn.requests {
cn.deleteRequest(r)
// log.Printf("%p: cancelling request: %v", cn, r)
if !msg(pp.Message{
Type: pp.Cancel,
Index: r.Index,
Begin: r.Begin,
Length: r.Length,
}) {
if !msg(makeCancelMessage(r)) {
return
}
}

View File

@ -261,3 +261,12 @@ func marshalBitfield(bf []bool) (b []byte) {
}
return
}
func MakeCancelMessage(piece, offset, length Integer) Message {
return Message{
Type: Cancel,
Index: piece,
Begin: offset,
Length: length,
}
}

9
protocol.go Normal file
View File

@ -0,0 +1,9 @@
package torrent
import (
pp "github.com/anacrolix/torrent/peer_protocol"
)
func makeCancelMessage(r request) pp.Message {
return pp.MakeCancelMessage(r.Index, r.Begin, r.Length)
}