Merge pull request #282 from mrd0ll4r/udp-scrapes
udp: fix ordering of scrapes
This commit is contained in:
commit
4aad0e992b
5 changed files with 10 additions and 5 deletions
|
@ -100,12 +100,16 @@ type ScrapeRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScrapeResponse represents the parameters used to create a scrape response.
|
// ScrapeResponse represents the parameters used to create a scrape response.
|
||||||
|
//
|
||||||
|
// The Scrapes must be in the same order as the InfoHashes in the corresponding
|
||||||
|
// ScrapeRequest.
|
||||||
type ScrapeResponse struct {
|
type ScrapeResponse struct {
|
||||||
Files map[InfoHash]Scrape
|
Files []Scrape
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scrape represents the state of a swarm that is returned in a scrape response.
|
// Scrape represents the state of a swarm that is returned in a scrape response.
|
||||||
type Scrape struct {
|
type Scrape struct {
|
||||||
|
InfoHash InfoHash
|
||||||
Snatches uint32
|
Snatches uint32
|
||||||
Complete uint32
|
Complete uint32
|
||||||
Incomplete uint32
|
Incomplete uint32
|
||||||
|
|
|
@ -74,8 +74,8 @@ func WriteAnnounceResponse(w http.ResponseWriter, resp *bittorrent.AnnounceRespo
|
||||||
// client over HTTP.
|
// client over HTTP.
|
||||||
func WriteScrapeResponse(w http.ResponseWriter, resp *bittorrent.ScrapeResponse) error {
|
func WriteScrapeResponse(w http.ResponseWriter, resp *bittorrent.ScrapeResponse) error {
|
||||||
filesDict := bencode.NewDict()
|
filesDict := bencode.NewDict()
|
||||||
for infohash, scrape := range resp.Files {
|
for _, scrape := range resp.Files {
|
||||||
filesDict[string(infohash[:])] = bencode.Dict{
|
filesDict[string(scrape.InfoHash[:])] = bencode.Dict{
|
||||||
"complete": scrape.Complete,
|
"complete": scrape.Complete,
|
||||||
"incomplete": scrape.Incomplete,
|
"incomplete": scrape.Incomplete,
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,7 +183,7 @@ func (h *responseHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeR
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, infoHash := range req.InfoHashes {
|
for _, infoHash := range req.InfoHashes {
|
||||||
resp.Files[infoHash] = h.store.ScrapeSwarm(infoHash, req.AddressFamily)
|
resp.Files = append(resp.Files, h.store.ScrapeSwarm(infoHash, req.AddressFamily))
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
|
|
|
@ -78,7 +78,7 @@ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceReque
|
||||||
// HandleScrape generates a response for a Scrape.
|
// HandleScrape generates a response for a Scrape.
|
||||||
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (resp *bittorrent.ScrapeResponse, err error) {
|
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (resp *bittorrent.ScrapeResponse, err error) {
|
||||||
resp = &bittorrent.ScrapeResponse{
|
resp = &bittorrent.ScrapeResponse{
|
||||||
Files: make(map[bittorrent.InfoHash]bittorrent.Scrape),
|
Files: make([]bittorrent.Scrape, 0, len(req.InfoHashes)),
|
||||||
}
|
}
|
||||||
for _, h := range l.preHooks {
|
for _, h := range l.preHooks {
|
||||||
if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
|
if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
|
||||||
|
|
|
@ -361,6 +361,7 @@ func (s *peerStore) ScrapeSwarm(ih bittorrent.InfoHash, addressFamily bittorrent
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resp.InfoHash = ih
|
||||||
shard := s.shards[s.shardIndex(ih, addressFamily)]
|
shard := s.shards[s.shardIndex(ih, addressFamily)]
|
||||||
shard.RLock()
|
shard.RLock()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue