]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Handle webseed Client events
authorMatt Joiner <anacrolix@gmail.com>
Mon, 1 Jun 2020 08:41:21 +0000 (18:41 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 1 Jun 2020 08:41:21 +0000 (18:41 +1000)
torrent.go
web_seed.go

index bf18a441f8fa86a94cb3d6617b20fbbf798d491d..47225626eb65c1467a3d9b38485a349d7b5ae6ce 100644 (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) {
index 4db8b725c0029d94f23b724020aa8add59ff71f4..6e88d34b0626e5a883f7931caea7f718b25bcfec 100644 (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)
+               }
+       }
+}