Move a bunch of stuff into subpackages

The core package is very large now, and often only parts of it are needed.
This commit is contained in:
Matt Joiner 2022-11-02 23:03:25 +11:00
parent 455913c752
commit 5501f994ca
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
10 changed files with 151 additions and 114 deletions

View File

@ -1,34 +1,12 @@
package torrent
import (
"context"
"net"
"github.com/anacrolix/torrent/dialer"
)
// Dialers have the network locked in.
type Dialer interface {
Dial(_ context.Context, addr string) (net.Conn, error)
DialerNetwork() string
}
type (
Dialer = dialer.T
NetworkDialer = dialer.WithNetwork
)
// An interface to ease wrapping dialers that explicitly include a network parameter.
type DialContexter interface {
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}
// Used by wrappers of standard library network types.
var DefaultNetDialer = &net.Dialer{}
// Adapts a DialContexter to the Dial interface in this package.
type NetworkDialer struct {
Network string
Dialer DialContexter
}
func (me NetworkDialer) DialerNetwork() string {
return me.Network
}
func (me NetworkDialer) Dial(ctx context.Context, addr string) (_ net.Conn, err error) {
return me.Dialer.DialContext(ctx, me.Network, addr)
}
var DefaultNetDialer = &dialer.Default

34
dialer/dialer.go Normal file
View File

@ -0,0 +1,34 @@
package dialer
import (
"context"
"net"
)
// Dialers have the network locked in.
type T interface {
Dial(_ context.Context, addr string) (net.Conn, error)
DialerNetwork() string
}
// An interface to ease wrapping dialers that explicitly include a network parameter.
type WithContext interface {
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}
// Used by wrappers of standard library network types.
var Default = &net.Dialer{}
// Adapts a WithContext to the Dial interface in this package.
type WithNetwork struct {
Network string
Dialer WithContext
}
func (me WithNetwork) DialerNetwork() string {
return me.Network
}
func (me WithNetwork) Dial(ctx context.Context, addr string) (_ net.Conn, err error) {
return me.Dialer.DialContext(ctx, me.Network, addr)
}

View File

@ -1,80 +1,16 @@
package metainfo
import (
"crypto/sha1"
"encoding"
"encoding/hex"
"fmt"
"github.com/anacrolix/torrent/types/infohash"
)
const HashSize = 20
// This type has been moved to allow avoiding importing everything in metainfo to get at it.
// 20-byte SHA1 hash used for info and pieces.
type Hash [HashSize]byte
const HashSize = infohash.Size
var _ fmt.Formatter = (*Hash)(nil)
func (h Hash) Format(f fmt.State, c rune) {
// TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless
// with the "default" 'v', or .String() already returning the hex.
f.Write([]byte(h.HexString()))
}
func (h Hash) Bytes() []byte {
return h[:]
}
func (h Hash) AsString() string {
return string(h[:])
}
func (h Hash) String() string {
return h.HexString()
}
func (h Hash) HexString() string {
return fmt.Sprintf("%x", h[:])
}
func (h *Hash) FromHexString(s string) (err error) {
if len(s) != 2*HashSize {
err = fmt.Errorf("hash hex string has bad length: %d", len(s))
return
}
n, err := hex.Decode(h[:], []byte(s))
if err != nil {
return
}
if n != HashSize {
panic(n)
}
return
}
type Hash = infohash.T
var (
_ encoding.TextUnmarshaler = (*Hash)(nil)
_ encoding.TextMarshaler = Hash{}
NewHashFromHex = infohash.FromHexString
HashBytes = infohash.HashBytes
)
func (h *Hash) UnmarshalText(b []byte) error {
return h.FromHexString(string(b))
}
func (h Hash) MarshalText() (text []byte, err error) {
return []byte(h.HexString()), nil
}
func NewHashFromHex(s string) (h Hash) {
err := h.FromHexString(s)
if err != nil {
panic(err)
}
return
}
func HashBytes(b []byte) (ret Hash) {
hasher := sha1.New()
hasher.Write(b)
copy(ret[:], hasher.Sum(nil))
return
}

View File

@ -8,7 +8,7 @@ import (
"path/filepath"
"strings"
"github.com/anacrolix/missinggo/slices"
"github.com/anacrolix/missinggo/v2/slices"
)
// The info dictionary.

View File

@ -7,6 +7,7 @@ import (
"github.com/RoaringBitmap/roaring"
"github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/torrent/types"
"github.com/anacrolix/torrent/types/infohash"
"golang.org/x/time/rate"
"github.com/anacrolix/torrent/metainfo"
@ -177,8 +178,9 @@ 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) {

View File

@ -1,14 +1,5 @@
package torrent
// Peer client ID.
type PeerID [20]byte
import "github.com/anacrolix/torrent/types"
// // Pretty prints the ID as hex, except parts that adher to the PeerInfo ID
// // Conventions of BEP 20.
// func (me PeerID) String() string {
// // if me[0] == '-' && me[7] == '-' {
// // return string(me[:8]) + hex.EncodeToString(me[8:])
// // }
// // return hex.EncodeToString(me[:])
// return fmt.Sprintf("%+q", me[:])
// }
type PeerID = types.PeerID

View File

@ -8,6 +8,7 @@ import (
"github.com/anacrolix/log"
"github.com/anacrolix/missinggo/perf"
"github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/torrent/dialer"
"github.com/pkg/errors"
)
@ -42,7 +43,7 @@ func listenTcp(network, address string) (s socket, err error) {
Listener: l,
NetworkDialer: NetworkDialer{
Network: network,
Dialer: DefaultNetDialer,
Dialer: dialer.Default,
},
}, err
}

View File

@ -8,6 +8,7 @@ import (
"testing"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/dialer"
)
func TestUnixConns(t *testing.T) {
@ -24,7 +25,7 @@ func TestUnixConns(t *testing.T) {
cfg.Debug = true
},
Client: func(cl *torrent.Client) {
cl.AddDialer(torrent.NetworkDialer{Network: "unix", Dialer: torrent.DefaultNetDialer})
cl.AddDialer(torrent.NetworkDialer{Network: "unix", Dialer: dialer.Default})
l, err := net.Listen("unix", filepath.Join(t.TempDir(), "socket"))
if err != nil {
panic(err)

View File

@ -0,0 +1,80 @@
package infohash
import (
"crypto/sha1"
"encoding"
"encoding/hex"
"fmt"
)
const Size = 20
// 20-byte SHA1 hash used for info and pieces.
type T [Size]byte
var _ fmt.Formatter = (*T)(nil)
func (t T) Format(f fmt.State, c rune) {
// TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless
// with the "default" 'v', or .String() already returning the hex.
f.Write([]byte(t.HexString()))
}
func (t T) Bytes() []byte {
return t[:]
}
func (t T) AsString() string {
return string(t[:])
}
func (t T) String() string {
return t.HexString()
}
func (t T) HexString() string {
return fmt.Sprintf("%x", t[:])
}
func (t *T) FromHexString(s string) (err error) {
if len(s) != 2*Size {
err = fmt.Errorf("hash hex string has bad length: %d", len(s))
return
}
n, err := hex.Decode(t[:], []byte(s))
if err != nil {
return
}
if n != Size {
panic(n)
}
return
}
var (
_ encoding.TextUnmarshaler = (*T)(nil)
_ encoding.TextMarshaler = T{}
)
func (t *T) UnmarshalText(b []byte) error {
return t.FromHexString(string(b))
}
func (t T) MarshalText() (text []byte, err error) {
return []byte(t.HexString()), nil
}
func FromHexString(s string) (h T) {
err := h.FromHexString(s)
if err != nil {
panic(err)
}
return
}
func HashBytes(b []byte) (ret T) {
hasher := sha1.New()
hasher.Write(b)
copy(ret[:], hasher.Sum(nil))
return
}

14
types/peerid.go Normal file
View File

@ -0,0 +1,14 @@
package types
// Peer client ID.
type PeerID [20]byte
// // Pretty prints the ID as hex, except parts that adher to the PeerInfo ID
// // Conventions of BEP 20.
// func (me PeerID) String() string {
// // if me[0] == '-' && me[7] == '-' {
// // return string(me[:8]) + hex.EncodeToString(me[8:])
// // }
// // return hex.EncodeToString(me[:])
// return fmt.Sprintf("%+q", me[:])
// }