udp: fix response encoding
This commit is contained in:
parent
eda825dfb0
commit
8ebe57a602
1 changed files with 24 additions and 12 deletions
|
@ -26,32 +26,44 @@ func WriteError(w io.Writer, txID []byte, err error) {
|
||||||
|
|
||||||
// WriteAnnounce encodes an announce response according to BEP 15.
|
// WriteAnnounce encodes an announce response according to BEP 15.
|
||||||
func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse) {
|
func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse) {
|
||||||
writeHeader(w, txID, announceActionID)
|
var buf bytes.Buffer
|
||||||
binary.Write(w, binary.BigEndian, uint32(resp.Interval/time.Second))
|
|
||||||
binary.Write(w, binary.BigEndian, uint32(resp.Incomplete))
|
writeHeader(&buf, txID, announceActionID)
|
||||||
binary.Write(w, binary.BigEndian, uint32(resp.Complete))
|
binary.Write(&buf, binary.BigEndian, uint32(resp.Interval/time.Second))
|
||||||
|
binary.Write(&buf, binary.BigEndian, uint32(resp.Incomplete))
|
||||||
|
binary.Write(&buf, binary.BigEndian, uint32(resp.Complete))
|
||||||
|
|
||||||
for _, peer := range resp.IPv4Peers {
|
for _, peer := range resp.IPv4Peers {
|
||||||
w.Write(peer.IP)
|
buf.Write(peer.IP)
|
||||||
binary.Write(w, binary.BigEndian, peer.Port)
|
binary.Write(&buf, binary.BigEndian, peer.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.Write(buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteScrape encodes a scrape response according to BEP 15.
|
// WriteScrape encodes a scrape response according to BEP 15.
|
||||||
func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse) {
|
func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse) {
|
||||||
writeHeader(w, txID, scrapeActionID)
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
writeHeader(&buf, txID, scrapeActionID)
|
||||||
|
|
||||||
for _, scrape := range resp.Files {
|
for _, scrape := range resp.Files {
|
||||||
binary.Write(w, binary.BigEndian, scrape.Complete)
|
binary.Write(&buf, binary.BigEndian, scrape.Complete)
|
||||||
binary.Write(w, binary.BigEndian, scrape.Snatches)
|
binary.Write(&buf, binary.BigEndian, scrape.Snatches)
|
||||||
binary.Write(w, binary.BigEndian, scrape.Incomplete)
|
binary.Write(&buf, binary.BigEndian, scrape.Incomplete)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.Write(buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteConnectionID encodes a new connection response according to BEP 15.
|
// WriteConnectionID encodes a new connection response according to BEP 15.
|
||||||
func WriteConnectionID(w io.Writer, txID, connID []byte) {
|
func WriteConnectionID(w io.Writer, txID, connID []byte) {
|
||||||
writeHeader(w, txID, connectActionID)
|
var buf bytes.Buffer
|
||||||
w.Write(connID)
|
|
||||||
|
writeHeader(&buf, txID, connectActionID)
|
||||||
|
buf.Write(connID)
|
||||||
|
|
||||||
|
w.Write(buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeHeader writes the action and transaction ID to the provided response
|
// writeHeader writes the action and transaction ID to the provided response
|
||||||
|
|
Loading…
Add table
Reference in a new issue