From e664a6aa1457beed6af80d912eeb0064573b28ac Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 12 Nov 2019 11:16:37 -0500 Subject: [PATCH] use varints for delimiting plaintext 2.0 msgs --- sec/insecure/insecure.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/sec/insecure/insecure.go b/sec/insecure/insecure.go index e3670c2..6a600a3 100644 --- a/sec/insecure/insecure.go +++ b/sec/insecure/insecure.go @@ -6,11 +6,13 @@ package insecure import ( "context" "fmt" + "io" "net" + gogoio "github.com/gogo/protobuf/io" + "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/sec" - "github.com/libp2p/go-msgio" ci "github.com/libp2p/go-libp2p-core/crypto" pb "github.com/libp2p/go-libp2p-core/sec/insecure/pb" @@ -143,7 +145,6 @@ func (ic *Conn) runHandshakeSync() error { return nil } - rw := msgio.NewReadWriter(ic.Conn) // Generate an Exchange message msg, err := makeExchangeMessage(ic.localPrivKey.GetPublic()) if err != nil { @@ -151,7 +152,7 @@ func (ic *Conn) runHandshakeSync() error { } // Send our Exchange and read theirs - remoteMsg, err := readWriteMsg(rw, msg) + remoteMsg, err := readWriteMsg(ic.Conn, msg) if err != nil { return err } @@ -181,31 +182,28 @@ func (ic *Conn) runHandshakeSync() error { } // read and write a message at the same time. -func readWriteMsg(c msgio.ReadWriter, out *pb.Exchange) (*pb.Exchange, error) { - outBytes, err := out.Marshal() - if err != nil { - return nil, err - } +func readWriteMsg(rw io.ReadWriter, out *pb.Exchange) (*pb.Exchange, error) { + const maxMsgSize = 1 << 32 + r := gogoio.NewDelimitedReader(rw, maxMsgSize) + w := gogoio.NewDelimitedWriter(rw) wresult := make(chan error) go func() { - wresult <- c.WriteMsg(outBytes) + wresult <- w.WriteMsg(out) }() - msg, err1 := c.ReadMsg() + inMsg := pb.Exchange{} + err := r.ReadMsg(&inMsg) // Always wait for the read to finish. err2 := <-wresult - if err1 != nil { - return nil, err1 + if err != nil { + return nil, err } if err2 != nil { - c.ReleaseMsg(msg) return nil, err2 } - inMsg := new(pb.Exchange) - err = inMsg.Unmarshal(msg) - return inMsg, err + return &inMsg, err } // LocalPeer returns the local peer ID.