tracker/frontend/udp/writer.go

63 lines
1.8 KiB
Go
Raw Normal View History

package udp
import (
"bytes"
"encoding/binary"
"fmt"
2016-08-05 07:47:04 +02:00
"io"
"time"
2016-08-17 03:42:08 +02:00
"github.com/chihaya/chihaya/bittorrent"
)
// WriteError writes the failure reason as a null-terminated string.
2016-08-05 07:47:04 +02:00
func WriteError(w io.Writer, txID []byte, err error) {
// If the client wasn't at fault, acknowledge it.
if _, ok := err.(bittorrent.ClientError); !ok {
err = fmt.Errorf("internal error occurred: %s", err.Error())
}
var buf bytes.Buffer
2016-08-05 07:47:04 +02:00
writeHeader(&buf, txID, errorActionID)
buf.WriteString(err.Error())
buf.WriteRune('\000')
2016-08-05 07:47:04 +02:00
w.Write(buf.Bytes())
}
// WriteAnnounce encodes an announce response according to BEP 15.
2016-08-05 07:47:04 +02:00
func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse) {
writeHeader(w, txID, announceActionID)
binary.Write(w, binary.BigEndian, uint32(resp.Interval/time.Second))
binary.Write(w, binary.BigEndian, uint32(resp.Incomplete))
binary.Write(w, binary.BigEndian, uint32(resp.Complete))
for _, peer := range resp.IPv4Peers {
2016-08-05 07:47:04 +02:00
w.Write(peer.IP)
binary.Write(w, binary.BigEndian, peer.Port)
}
}
// WriteScrape encodes a scrape response according to BEP 15.
2016-08-05 07:47:04 +02:00
func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse) {
writeHeader(w, txID, scrapeActionID)
for _, scrape := range resp.Files {
2016-08-05 07:47:04 +02:00
binary.Write(w, binary.BigEndian, scrape.Complete)
binary.Write(w, binary.BigEndian, scrape.Snatches)
binary.Write(w, binary.BigEndian, scrape.Incomplete)
}
}
// WriteConnectionID encodes a new connection response according to BEP 15.
2016-08-05 07:47:04 +02:00
func WriteConnectionID(w io.Writer, txID, connID []byte) {
writeHeader(w, txID, connectActionID)
w.Write(connID)
}
// writeHeader writes the action and transaction ID to the provided response
// buffer.
2016-08-05 07:47:04 +02:00
func writeHeader(w io.Writer, txID []byte, action uint32) {
binary.Write(w, binary.BigEndian, action)
w.Write(txID)
}