Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

share the buffer pool. #21

Merged
merged 2 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"context"
"io"

msgio "github.com/jbenet/go-msgio"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
msgio "github.com/libp2p/go-msgio"
)

// SessionGenerator constructs secure communication sessions for a peer.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "QmRQhVisS8dmPbjBUthVkenn81pBxrx1GxE281csJhm2vL",
"hash": "QmWBug6eBS7AxRdCDVuSY5CnSit7cS2XnPFYJWqWDumhCG",
"name": "go-msgio",
"version": "0.0.0"
"version": "0.0.3"
},
{
"author": "whyrusleeping",
Expand Down
2 changes: 1 addition & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"time"

logging "github.com/ipfs/go-log"
msgio "github.com/jbenet/go-msgio"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
pb "github.com/libp2p/go-libp2p-secio/pb"
msgio "github.com/libp2p/go-msgio"
mh "github.com/multiformats/go-multihash"
)

Expand Down
22 changes: 8 additions & 14 deletions rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,24 @@ import (
"sync"

proto "github.com/gogo/protobuf/proto"
msgio "github.com/jbenet/go-msgio"
mpool "github.com/jbenet/go-msgio/mpool"
msgio "github.com/libp2p/go-msgio"
mpool "github.com/libp2p/go-msgio/mpool"
)

// ErrMACInvalid signals that a MAC verification failed
var ErrMACInvalid = errors.New("MAC verification failed")

// bufPool is a ByteSlicePool for messages. we need buffers because (sadly)
// we cannot encrypt in place-- the user needs their buffer back.
var bufPool = mpool.ByteSlicePool

type etmWriter struct {
// params
pool mpool.Pool // for the buffers with encrypted data
str cipher.Stream // the stream cipher to encrypt with
mac HMAC // the mac to authenticate data with
w io.Writer
str cipher.Stream // the stream cipher to encrypt with
mac HMAC // the mac to authenticate data with
w io.Writer

sync.Mutex
}

// NewETMWriter Encrypt-Then-MAC
func NewETMWriter(w io.Writer, s cipher.Stream, mac HMAC) msgio.WriteCloser {
return &etmWriter{w: w, str: s, mac: mac, pool: bufPool}
return &etmWriter{w: w, str: s, mac: mac}
}

// Write writes passed in buffer as a single message.
Expand All @@ -52,7 +46,7 @@ func (w *etmWriter) WriteMsg(b []byte) error {

bufsize := uint32(4 + len(b) + w.mac.Size())
// encrypt.
buf := w.pool.Get(bufsize).([]byte)
buf := mpool.ByteSlicePool.Get(bufsize).([]byte)
data := buf[4 : 4+len(b)] // the pool's buffer may be larger
w.str.XORKeyStream(data, b)

Expand All @@ -70,7 +64,7 @@ func (w *etmWriter) WriteMsg(b []byte) error {
binary.BigEndian.PutUint32(buf[:4], uint32(len(data)))

_, err := w.w.Write(buf[:bufsize])
w.pool.Put(bufsize, buf)
mpool.ByteSlicePool.Put(bufsize, buf)
return err
}

Expand Down