diff --git a/connection.go b/connection.go index 9b102eb8..c5c6c957 100644 --- a/connection.go +++ b/connection.go @@ -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 } } diff --git a/peer_protocol/protocol.go b/peer_protocol/protocol.go index 19db34fb..8c309bcb 100644 --- a/peer_protocol/protocol.go +++ b/peer_protocol/protocol.go @@ -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, + } +} diff --git a/protocol.go b/protocol.go new file mode 100644 index 00000000..3faeb6ec --- /dev/null +++ b/protocol.go @@ -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) +}