Skip to content

Commit

Permalink
Move a bunch of stuff into subpackages
Browse files Browse the repository at this point in the history
The core package is very large now, and often only parts of it are needed.
  • Loading branch information
anacrolix committed Nov 15, 2022
1 parent 455913c commit 5501f99
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 114 deletions.
34 changes: 6 additions & 28 deletions dialer.go
Original file line number Diff line number Diff line change
@@ -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
}

// 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
}
type (
Dialer = dialer.T
NetworkDialer = dialer.WithNetwork
)

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 changes: 34 additions & 0 deletions dialer/dialer.go
Original file line number Diff line number Diff line change
@@ -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)
}
76 changes: 6 additions & 70 deletions metainfo/hash.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion metainfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"strings"

"github.com/anacrolix/missinggo/slices"
"github.com/anacrolix/missinggo/v2/slices"
)

// The info dictionary.
Expand Down
6 changes: 4 additions & 2 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 2 additions & 11 deletions peerid.go
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion test/unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/dialer"
)

func TestUnixConns(t *testing.T) {
Expand All @@ -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)
Expand Down
80 changes: 80 additions & 0 deletions types/infohash/infohash.go
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions types/peerid.go
Original file line number Diff line number Diff line change
@@ -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[:])
// }

0 comments on commit 5501f99

Please sign in to comment.