2018-07-04 10:51:47 +00:00
|
|
|
package msgio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
pool "github.com/libp2p/go-buffer-pool"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// varintWriter is the underlying type that implements the Writer interface.
|
|
|
|
type varintWriter struct {
|
|
|
|
W io.Writer
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
pool *pool.BufferPool
|
|
|
|
lock sync.Mutex // for threadsafe writes
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewVarintWriter wraps an io.Writer with a varint msgio framed writer.
|
|
|
|
// The msgio.Writer will write the length prefix of every message written
|
|
|
|
// as a varint, using https://golang.org/pkg/encoding/binary/#PutUvarint
|
|
|
|
func NewVarintWriter(w io.Writer) WriteCloser {
|
2019-10-04 15:21:24 +00:00
|
|
|
return NewVarintWriterWithPool(w, pool.GlobalPool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVarintWriterWithPool(w io.Writer, p *pool.BufferPool) WriteCloser {
|
2018-07-04 10:51:47 +00:00
|
|
|
return &varintWriter{
|
2019-10-04 15:21:24 +00:00
|
|
|
pool: p,
|
2018-07-04 10:51:47 +00:00
|
|
|
W: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintWriter) Write(msg []byte) (int, error) {
|
|
|
|
err := s.WriteMsg(msg)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(msg), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintWriter) WriteMsg(msg []byte) error {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
buf := s.pool.Get(len(msg) + binary.MaxVarintLen64)
|
|
|
|
n := binary.PutUvarint(buf, uint64(len(msg)))
|
|
|
|
n += copy(buf[n:], msg)
|
|
|
|
_, err := s.W.Write(buf[:n])
|
|
|
|
s.pool.Put(buf)
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintWriter) Close() error {
|
|
|
|
if c, ok := s.W.(io.Closer); ok {
|
|
|
|
return c.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// varintReader is the underlying type that implements the Reader interface.
|
|
|
|
type varintReader struct {
|
|
|
|
R io.Reader
|
|
|
|
br io.ByteReader // for reading varints.
|
|
|
|
|
|
|
|
next int
|
2019-06-09 07:24:20 +00:00
|
|
|
pool *pool.BufferPool
|
2019-10-04 15:21:24 +00:00
|
|
|
lock sync.Mutex
|
2018-07-04 10:51:47 +00:00
|
|
|
max int // the maximal message size (in bytes) this reader handles
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVarintReader wraps an io.Reader with a varint msgio framed reader.
|
|
|
|
// The msgio.Reader will read whole messages at a time (using the length).
|
|
|
|
// Varints read according to https://golang.org/pkg/encoding/binary/#ReadUvarint
|
|
|
|
// Assumes an equivalent writer on the other side.
|
|
|
|
func NewVarintReader(r io.Reader) ReadCloser {
|
2019-10-04 15:21:24 +00:00
|
|
|
return NewVarintReaderSize(r, defaultMaxSize)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
// NewVarintReaderSize is equivalent to NewVarintReader but allows one to
|
|
|
|
// specify a max message size.
|
|
|
|
func NewVarintReaderSize(r io.Reader, maxMessageSize int) ReadCloser {
|
|
|
|
return NewVarintReaderSizeWithPool(r, maxMessageSize, pool.GlobalPool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVarintReaderWithPool is the same as NewVarintReader but allows one to
|
|
|
|
// specify a buffer pool.
|
2019-06-09 07:24:20 +00:00
|
|
|
func NewVarintReaderWithPool(r io.Reader, p *pool.BufferPool) ReadCloser {
|
2019-10-04 15:21:24 +00:00
|
|
|
return NewVarintReaderSizeWithPool(r, defaultMaxSize, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVarintReaderWithPool is the same as NewVarintReader but allows one to
|
|
|
|
// specify a buffer pool and a max message size.
|
|
|
|
func NewVarintReaderSizeWithPool(r io.Reader, maxMessageSize int, p *pool.BufferPool) ReadCloser {
|
2018-07-04 10:51:47 +00:00
|
|
|
if p == nil {
|
|
|
|
panic("nil pool")
|
|
|
|
}
|
|
|
|
return &varintReader{
|
|
|
|
R: r,
|
|
|
|
br: &simpleByteReader{R: r},
|
|
|
|
next: -1,
|
|
|
|
pool: p,
|
2019-10-04 15:21:24 +00:00
|
|
|
max: maxMessageSize,
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextMsgLen reads the length of the next msg into s.lbuf, and returns it.
|
|
|
|
// WARNING: like Read, NextMsgLen is destructive. It reads from the internal
|
|
|
|
// reader.
|
|
|
|
func (s *varintReader) NextMsgLen() (int, error) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
return s.nextMsgLen()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintReader) nextMsgLen() (int, error) {
|
|
|
|
if s.next == -1 {
|
|
|
|
length, err := binary.ReadUvarint(s.br)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
s.next = int(length)
|
|
|
|
}
|
|
|
|
return s.next, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintReader) Read(msg []byte) (int, error) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
length, err := s.nextMsgLen()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if length > len(msg) {
|
|
|
|
return 0, io.ErrShortBuffer
|
|
|
|
}
|
|
|
|
_, err = io.ReadFull(s.R, msg[:length])
|
|
|
|
s.next = -1 // signal we've consumed this msg
|
|
|
|
return length, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintReader) ReadMsg() ([]byte, error) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
length, err := s.nextMsgLen()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
if length == 0 {
|
|
|
|
s.next = -1
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
if length > s.max {
|
|
|
|
return nil, ErrMsgTooLarge
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
msg := s.pool.Get(length)
|
2018-07-04 10:51:47 +00:00
|
|
|
_, err = io.ReadFull(s.R, msg)
|
|
|
|
s.next = -1 // signal we've consumed this msg
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintReader) ReleaseMsg(msg []byte) {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.pool.Put(msg)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *varintReader) Close() error {
|
|
|
|
if c, ok := s.R.(io.Closer); ok {
|
|
|
|
return c.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type simpleByteReader struct {
|
|
|
|
R io.Reader
|
2019-10-04 15:21:24 +00:00
|
|
|
buf [1]byte
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *simpleByteReader) ReadByte() (c byte, err error) {
|
2019-10-04 15:21:24 +00:00
|
|
|
if _, err := io.ReadFull(r.R, r.buf[:]); err != nil {
|
2018-07-04 10:51:47 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return r.buf[0], nil
|
|
|
|
}
|