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 package torrent
import ( import (
"context" "github.com/anacrolix/torrent/dialer"
"net"
) )
// Dialers have the network locked in. type (
type Dialer interface { Dialer = dialer.T
Dial(_ context.Context, addr string) (net.Conn, error) NetworkDialer = dialer.WithNetwork
DialerNetwork() string )
}
// An interface to ease wrapping dialers that explicitly include a network parameter. var DefaultNetDialer = &dialer.Default
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)
}

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 package metainfo
import ( import (
"crypto/sha1" "github.com/anacrolix/torrent/types/infohash"
"encoding"
"encoding/hex"
"fmt"
) )
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. const HashSize = infohash.Size
type Hash [HashSize]byte
var _ fmt.Formatter = (*Hash)(nil) type Hash = infohash.T
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
}
var ( var (
_ encoding.TextUnmarshaler = (*Hash)(nil) NewHashFromHex = infohash.FromHexString
_ encoding.TextMarshaler = Hash{} 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" "path/filepath"
"strings" "strings"
"github.com/anacrolix/missinggo/slices" "github.com/anacrolix/missinggo/v2/slices"
) )
// The info dictionary. // The info dictionary.

View File

@ -7,6 +7,7 @@ import (
"github.com/RoaringBitmap/roaring" "github.com/RoaringBitmap/roaring"
"github.com/anacrolix/missinggo/v2" "github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/torrent/types" "github.com/anacrolix/torrent/types"
"github.com/anacrolix/torrent/types/infohash"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
@ -177,7 +178,8 @@ var unlimited = rate.NewLimiter(rate.Inf, 0)
type ( type (
pieceIndex = int pieceIndex = int
InfoHash = metainfo.Hash // Deprecated: Use infohash.T directly to avoid unnecessary imports.
InfoHash = infohash.T
IpPort = missinggo.IpPort IpPort = missinggo.IpPort
) )

View File

@ -1,14 +1,5 @@
package torrent package torrent
// Peer client ID. import "github.com/anacrolix/torrent/types"
type PeerID [20]byte
// // Pretty prints the ID as hex, except parts that adher to the PeerInfo ID type PeerID = types.PeerID
// // 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[:])
// }

View File

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

View File

@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/dialer"
) )
func TestUnixConns(t *testing.T) { func TestUnixConns(t *testing.T) {
@ -24,7 +25,7 @@ func TestUnixConns(t *testing.T) {
cfg.Debug = true cfg.Debug = true
}, },
Client: func(cl *torrent.Client) { 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")) l, err := net.Listen("unix", filepath.Join(t.TempDir(), "socket"))
if err != nil { if err != nil {
panic(err) 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[:])
// }