Handle webseed Client events

This commit is contained in:
Matt Joiner 2020-06-01 18:41:21 +10:00
parent ff53ab860c
commit 5602ecd810
2 changed files with 27 additions and 3 deletions

View File

@ -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) {

View File

@ -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)
}
}
}