]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Abstract out making a cancel message
authorMatt Joiner <anacrolix@gmail.com>
Mon, 18 Sep 2017 03:42:42 +0000 (13:42 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 18 Sep 2017 03:42:42 +0000 (13:42 +1000)
connection.go
peer_protocol/protocol.go
protocol.go [new file with mode: 0644]

index 9b102eb87dcac0e4305abf5bdcba8091d1f21099..c5c6c95743132debd5daec1457a04678b846c943 100644 (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
                        }
                }
index 19db34fb259d49cc4a09b600ab31ef3443aa0096..8c309bcbfcc525327026d41e16350aa77ad35af9 100644 (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,
+       }
+}
diff --git a/protocol.go b/protocol.go
new file mode 100644 (file)
index 0000000..3faeb6e
--- /dev/null
@@ -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)
+}