X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=misc.go;h=7d3007ecee74eb92f58eee434dfb9431a86f9098;hb=HEAD;hp=4a6e1debe3d21634ca2a6f5d40590d8132183a66;hpb=175b826e738c76d1a96e435bc5396e054ca5796a;p=btrtrc.git diff --git a/misc.go b/misc.go index 4a6e1deb..7d3007ec 100644 --- a/misc.go +++ b/misc.go @@ -4,12 +4,14 @@ import ( "errors" "net" + "github.com/RoaringBitmap/roaring" "github.com/anacrolix/missinggo/v2" - "github.com/anacrolix/torrent/types" "golang.org/x/time/rate" "github.com/anacrolix/torrent/metainfo" pp "github.com/anacrolix/torrent/peer_protocol" + "github.com/anacrolix/torrent/types" + "github.com/anacrolix/torrent/types/infohash" ) type ( @@ -43,7 +45,7 @@ func newRequestFromMessage(msg *pp.Message) Request { } // The size in bytes of a metadata extension piece. -func metadataPieceSize(totalSize int, piece int) int { +func metadataPieceSize(totalSize, piece int) int { ret := totalSize - piece*(1<<14) if ret > 1<<14 { ret = 1 << 14 @@ -99,7 +101,7 @@ func validateInfo(info *metainfo.Info) error { return nil } -func chunkIndexSpec(index pp.Integer, pieceLength, chunkSize pp.Integer) ChunkSpec { +func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec { ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize} if ret.Begin+ret.Length > pieceLength { ret.Length = pieceLength - ret.Begin @@ -113,7 +115,8 @@ func connLessTrusted(l, r *Peer) bool { func connIsIpv6(nc interface { LocalAddr() net.Addr -}) bool { +}, +) bool { ra := nc.LocalAddr() rip := addrIpOrNil(ra) return rip.To4() == nil && rip.To16() != nil @@ -142,6 +145,16 @@ func max(as ...int64) int64 { return ret } +func maxInt(as ...int) int { + ret := as[0] + for _, a := range as[1:] { + if a > ret { + ret = a + } + } + return ret +} + func min(as ...int64) int64 { ret := as[0] for _, a := range as[1:] { @@ -166,6 +179,16 @@ var unlimited = rate.NewLimiter(rate.Inf, 0) type ( pieceIndex = int - InfoHash = metainfo.Hash - IpPort = missinggo.IpPort + // Deprecated: Use infohash.T directly to avoid unnecessary imports. + InfoHash = infohash.T + IpPort = missinggo.IpPort ) + +func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) { + for i, b := range slice { + if b { + rb.AddInt(i) + } + } + return +}