Skip to content

Commit

Permalink
Add some client callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Jul 15, 2020
1 parent 7050446 commit 7ee0fda
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions callbacks.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ type ClientConfig struct {

DisableWebtorrent bool
DisableWebseeds bool

Callbacks Callbacks
}

func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
Expand Down
3 changes: 3 additions & 0 deletions peerconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 7ee0fda

Please sign in to comment.