From cc6441a7919128c6c3b2a9ab83ba3024f8856693 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 13 Jul 2018 21:33:21 +1000 Subject: [PATCH] Fix peer_protocol.Message.RequestSpec for Type Piece --- peer_protocol/msg.go | 14 ++++++++++++-- peer_protocol/reqspec.go | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/peer_protocol/msg.go b/peer_protocol/msg.go index 7d3764b7..d7e5f5a7 100644 --- a/peer_protocol/msg.go +++ b/peer_protocol/msg.go @@ -26,8 +26,18 @@ func MakeCancelMessage(piece, offset, length Integer) Message { } } -func (msg Message) RequestSpec() RequestSpec { - return RequestSpec{msg.Index, msg.Begin, msg.Length} +func (msg Message) RequestSpec() (ret RequestSpec) { + return RequestSpec{ + msg.Index, + msg.Begin, + func() Integer { + if msg.Type == Piece { + return Integer(len(msg.Piece)) + } else { + return msg.Length + } + }(), + } } func (msg Message) MustMarshalBinary() []byte { diff --git a/peer_protocol/reqspec.go b/peer_protocol/reqspec.go index f73a66df..f9989a26 100644 --- a/peer_protocol/reqspec.go +++ b/peer_protocol/reqspec.go @@ -1,5 +1,11 @@ package peer_protocol +import "fmt" + type RequestSpec struct { Index, Begin, Length Integer } + +func (me RequestSpec) String() string { + return fmt.Sprintf("{%d %d %d}", me.Index, me.Begin, me.Length) +}