forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move a bunch of stuff into subpackages
The core package is very large now, and often only parts of it are needed.
- Loading branch information
Showing
10 changed files
with
151 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[:]) | ||
// } |