Add some client callbacks
This commit is contained in:
parent
70504464ec
commit
7ee0fdafe3
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -133,6 +133,8 @@ type ClientConfig struct {
|
|||
|
||||
DisableWebtorrent bool
|
||||
DisableWebseeds bool
|
||||
|
||||
Callbacks Callbacks
|
||||
}
|
||||
|
||||
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue