Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
use varints for delimiting plaintext 2.0 msgs (#74)
Browse files Browse the repository at this point in the history
* use varints for delimiting plaintext 2.0 msgs

* lower size limit, fix comment

* go mod tidy - rm unused msgio dependency
  • Loading branch information
yusefnapora authored Nov 12, 2019
1 parent 42a4b34 commit f06e38f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/ipfs/go-cid v0.0.3
github.com/jbenet/goprocess v0.1.3
github.com/libp2p/go-flow-metrics v0.0.2
github.com/libp2p/go-msgio v0.0.4
github.com/libp2p/go-openssl v0.0.3
github.com/minio/sha256-simd v0.1.1
github.com/mr-tron/base58 v1.1.2
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/libp2p/go-buffer-pool v0.0.1 h1:9Rrn/H46cXjaA2HQ5Y8lyhOS1NhTkZ4yuEs2r3Eechg=
github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ=
github.com/libp2p/go-flow-metrics v0.0.2 h1:U5TvqfoyR6GVRM+bC15Ux1ltar1kbj6Zw6xOVR02CZs=
github.com/libp2p/go-flow-metrics v0.0.2/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
github.com/libp2p/go-msgio v0.0.4 h1:agEFehY3zWJFUHK6SEMR7UYmk2z6kC3oeCM7ybLhguA=
github.com/libp2p/go-msgio v0.0.4 h1:agEFehY3zWJFUHK6SEMR7UYmk2z6kC3oeCM7ybLhguA=
github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ=
github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ=
github.com/libp2p/go-openssl v0.0.3 h1:wjlG7HvQkt4Fq4cfH33Ivpwp0omaElYEi9z26qaIkIk=
github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic=
Expand Down
32 changes: 15 additions & 17 deletions sec/insecure/insecure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -143,15 +145,14 @@ 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 {
return err
}

// Send our Exchange and read theirs
remoteMsg, err := readWriteMsg(rw, msg)
remoteMsg, err := readWriteMsg(ic.Conn, msg)
if err != nil {
return err
}
Expand Down Expand Up @@ -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 << 16
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.
// Always wait for the write 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.
Expand Down

0 comments on commit f06e38f

Please sign in to comment.