Drop xerrors and reflection dependency
Nothing wrong with missinggo.CopyExact -- but fewer dependencies is better IMO. Also changed String() to use a consistent receiver name -- not a big deal.
This commit is contained in:
parent
c76bb42c07
commit
99ba75f458
|
@ -2,12 +2,11 @@ package peer_protocol
|
|||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/anacrolix/missinggo/v2"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
)
|
||||
|
||||
|
@ -34,8 +33,8 @@ type (
|
|||
PeerExtensionBits [8]byte
|
||||
)
|
||||
|
||||
func (me PeerExtensionBits) String() string {
|
||||
return hex.EncodeToString(me[:])
|
||||
func (pex PeerExtensionBits) String() string {
|
||||
return hex.EncodeToString(pex[:])
|
||||
}
|
||||
|
||||
func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits) {
|
||||
|
@ -98,7 +97,7 @@ func Handshake(
|
|||
// Wait until writes complete before returning from handshake.
|
||||
err = <-writeDone
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error writing: %s", err)
|
||||
err = fmt.Errorf("error writing: %w", err)
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -119,16 +118,21 @@ func Handshake(
|
|||
var b [68]byte
|
||||
_, err = io.ReadFull(sock, b[:68])
|
||||
if err != nil {
|
||||
err = xerrors.Errorf("while reading: %w", err)
|
||||
return
|
||||
return res, fmt.Errorf("while reading: %w", err)
|
||||
}
|
||||
if string(b[:20]) != Protocol {
|
||||
err = xerrors.Errorf("unexpected protocol string")
|
||||
return
|
||||
return res, errors.New("unexpected protocol string")
|
||||
}
|
||||
missinggo.CopyExact(&res.PeerExtensionBits, b[20:28])
|
||||
missinggo.CopyExact(&res.Hash, b[28:48])
|
||||
missinggo.CopyExact(&res.PeerID, b[48:68])
|
||||
|
||||
copyExact := func(dst []byte, src []byte) {
|
||||
if dstLen, srcLen := uint64(len(dst)), uint64(len(src)); dstLen != srcLen {
|
||||
panic("dst len " + strconv.FormatUint(dstLen,10) + " != src len " + strconv.FormatUint(srcLen,10))
|
||||
}
|
||||
copy(dst, src)
|
||||
}
|
||||
copyExact(res.PeerExtensionBits[:], b[20:28])
|
||||
copyExact(res.Hash[:], b[28:48])
|
||||
copyExact(res.PeerID[:], b[48:68])
|
||||
// peerExtensions.Add(res.PeerExtensionBits.String(), 1)
|
||||
|
||||
// TODO: Maybe we can just drop peers here if we're not interested. This
|
||||
|
|
Loading…
Reference in New Issue