From 5602ecd810981b548d9929109953bba783d8aac3 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 1 Jun 2020 18:41:21 +1000 Subject: [PATCH] Handle webseed Client events --- torrent.go | 6 +++--- web_seed.go | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/torrent.go b/torrent.go index bf18a441..47225626 100644 --- a/torrent.go +++ b/torrent.go @@ -741,8 +741,8 @@ func (t *Torrent) requestOffset(r request) int64 { return torrentRequestOffset(*t.length, int64(t.usualPieceSize()), r) } -// Return the request that would include the given offset into the torrent -// data. Returns !ok if there is no such request. +// Return the request that would include the given offset into the torrent data. Returns !ok if +// there is no such request. func (t *Torrent) offsetRequest(off int64) (req request, ok bool) { return torrentOffsetRequest(*t.length, t.info.PieceLength, int64(t.chunkSize), off) } @@ -2027,9 +2027,9 @@ func (t *Torrent) addWebSeed(url string) { Events: make(chan webseed.ClientEvent), }, } + go ws.eventProcessor() ws.peer.PeerImpl = &ws t.webSeeds[url] = &ws.peer - } func (t *Torrent) peerIsActive(p *peer) (active bool) { diff --git a/web_seed.go b/web_seed.go index 4db8b725..6e88d34b 100644 --- a/web_seed.go +++ b/web_seed.go @@ -3,6 +3,7 @@ package torrent import ( "net/http" + pp "github.com/anacrolix/torrent/peer_protocol" "github.com/anacrolix/torrent/segments" "github.com/anacrolix/torrent/webseed" ) @@ -66,3 +67,26 @@ func (ws *webSeed) UpdateRequests() { } func (ws *webSeed) Close() {} + +func (ws *webSeed) eventProcessor() { + for ev := range ws.client.Events { + if ev.Err != nil { + panic(ev) + } + r, ok := ws.peer.t.offsetRequest(ev.RequestSpec.Start) + if !ok { + panic(ev) + } + ws.peer.t.cl.lock() + err := ws.peer.receiveChunk(&pp.Message{ + Type: pp.Piece, + Index: r.Index, + Begin: r.Begin, + Piece: ev.Bytes, + }) + ws.peer.t.cl.unlock() + if err != nil { + panic(err) + } + } +} -- 2.48.1