Add some client callbacks

This commit is contained in:
Matt Joiner 2020-07-15 14:00:47 +10:00
parent 70504464ec
commit 7ee0fdafe3
4 changed files with 20 additions and 0 deletions

12
callbacks.go Normal file
View File

@ -0,0 +1,12 @@
package torrent
import (
pp "github.com/anacrolix/torrent/peer_protocol"
)
// These are called synchronously, and do not pass ownership. The Client and other locks may still
// be held. nil functions are not called.
type Callbacks struct {
CompletedHandshake func(_ *PeerConn, infoHash InfoHash)
ReadMessage func(*PeerConn, *pp.Message)
}

View File

@ -860,6 +860,9 @@ func (cl *Client) connBtHandshake(c *PeerConn, ih *metainfo.Hash) (ret metainfo.
c.PeerExtensionBytes = res.PeerExtensionBits
c.PeerID = res.PeerID
c.completedHandshake = time.Now()
if cb := cl.config.Callbacks.CompletedHandshake; cb != nil {
cb(c, res.Hash)
}
return
}

View File

@ -133,6 +133,8 @@ type ClientConfig struct {
DisableWebtorrent bool
DisableWebseeds bool
Callbacks Callbacks
}
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {

View File

@ -1059,6 +1059,9 @@ func (c *PeerConn) mainReadLoop() (err error) {
defer cl.lock()
err = decoder.Decode(&msg)
}()
if cb := cl.config.Callbacks.ReadMessage; cb != nil && err == nil {
cb(c, &msg)
}
if t.closed.IsSet() || c.closed.IsSet() {
return nil
}