From 99ba75f45818b82384b8980dd772e7c074ad4798 Mon Sep 17 00:00:00 2001 From: YenForYang Date: Sat, 4 Sep 2021 01:30:14 -0500 Subject: [PATCH] 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. --- peer_protocol/handshake.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/peer_protocol/handshake.go b/peer_protocol/handshake.go index 5e6f123d..ba81a0cf 100644 --- a/peer_protocol/handshake.go +++ b/peer_protocol/handshake.go @@ -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") + } + + 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) } - missinggo.CopyExact(&res.PeerExtensionBits, b[20:28]) - missinggo.CopyExact(&res.Hash, b[28:48]) - missinggo.CopyExact(&res.PeerID, b[48:68]) + 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 -- 2.44.0