From 669905e203009e404f384d86f9276f0675921bac Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 20 Jan 2021 14:22:44 +1100 Subject: [PATCH] Add ReceivedUsefulData Callback --- callbacks.go | 7 +++++++ peerconn.go | 3 +++ 2 files changed, 10 insertions(+) diff --git a/callbacks.go b/callbacks.go index fa9fea5b..f78c5854 100644 --- a/callbacks.go +++ b/callbacks.go @@ -18,4 +18,11 @@ type Callbacks struct { // Provides secret keys to be tried against incoming encrypted connections. ReceiveEncryptedHandshakeSkeys mse.SecretKeyIter + + ReceivedUsefulData []func(ReceivedUsefulDataEvent) +} + +type ReceivedUsefulDataEvent struct { + Peer *Peer + Message *pp.Message } diff --git a/peerconn.go b/peerconn.go index 4fcb4f3d..25d29b34 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1378,6 +1378,9 @@ func (c *Peer) receiveChunk(msg *pp.Message) error { c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadUseful })) c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulData })) + for _, f := range c.t.cl.config.Callbacks.ReceivedUsefulData { + f(ReceivedUsefulDataEvent{c, msg}) + } c.lastUsefulChunkReceived = time.Now() // if t.fastestPeer != c { // log.Printf("setting fastest connection %p", c)