// WriteAnnounce writes a bencode dict representation of an AnnounceResponse. func (w *Writer) WriteAnnounce(res *models.AnnounceResponse) error { dict := bencode.Dict{ "complete": res.Complete, "incomplete": res.Incomplete, "interval": res.Interval, "min interval": res.MinInterval, } if res.Compact { if res.IPv4Peers != nil { dict["peers"] = compactPeers(false, res.IPv4Peers) } if res.IPv6Peers != nil { compact := compactPeers(true, res.IPv6Peers) // Don't bother writing the IPv6 field if there is no value. if len(compact) > 0 { dict["peers6"] = compact } } } else if res.IPv4Peers != nil || res.IPv6Peers != nil { dict["peers"] = peersList(res.IPv4Peers, res.IPv6Peers) } bencoder := bencode.NewEncoder(w) return bencoder.Encode(dict) }
// WriteError writes a bencode dict with a failure reason. func (w *Writer) WriteError(err error) error { bencoder := bencode.NewEncoder(w) return bencoder.Encode(bencode.Dict{ "failure reason": err.Error(), }) }
// WriteScrape writes a bencode dict representation of a ScrapeResponse. func (w *Writer) WriteScrape(res *models.ScrapeResponse) error { dict := bencode.Dict{ "files": filesDict(res.Files), } bencoder := bencode.NewEncoder(w) return bencoder.Encode(dict) }
// WriteError writes a bencode dict with a failure reason. func (w *Writer) WriteError(err error) error { bencoder := bencode.NewEncoder(w) w.Header().Set("Content-Type", "text/plain") return bencoder.Encode(bencode.Dict{ "failure reason": err.Error(), }) }
// WriteScrape writes a bencode dict representation of a ScrapeResponse. func (w *Writer) WriteScrape(res *models.ScrapeResponse) error { dict := bencode.Dict{ "files": filesDict(res.Files), } w.Header().Set("Content-Type", "text/plain") bencoder := bencode.NewEncoder(w) return bencoder.Encode(dict) }
// WriteAnnounce writes a bencode dict representation of an AnnounceResponse. func (w *Writer) WriteAnnounce(res *models.AnnounceResponse) error { dict := bencode.Dict{ "complete": res.Complete, "incomplete": res.Incomplete, "interval": res.Interval, "min interval": res.MinInterval, "compact": 1, } dict["peers"] = compactPeers(res.Peers) w.Header().Set("Content-Type", "text/plain") bencoder := bencode.NewEncoder(w) return bencoder.Encode(dict) }