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)
}
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) {
import (
"net/http"
+ pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/segments"
"github.com/anacrolix/torrent/webseed"
)
}
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)
+ }
+ }
+}