github.com/m3db/m3@v1.5.0/src/msg/protocol/proto/decoder.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package proto
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  
    27  	xio "github.com/m3db/m3/src/x/io"
    28  	"github.com/m3db/m3/src/x/pool"
    29  )
    30  
    31  type decoder struct {
    32  	reader           io.Reader
    33  	rOpts            xio.ResettableReaderOptions
    34  	resettableReader xio.ResettableReader
    35  	buffer           []byte
    36  	bytesPool        pool.BytesPool
    37  	maxMessageSize   int
    38  	opts             Options
    39  }
    40  
    41  // NewDecoder decodes a new decoder, the implementation is not thread safe.
    42  func NewDecoder(r io.Reader, opts Options, bufferSize int) Decoder {
    43  	if opts == nil {
    44  		opts = NewOptions()
    45  	}
    46  	pool := opts.BytesPool()
    47  	rOpts := xio.ResettableReaderOptions{ReadBufferSize: bufferSize}
    48  	return &decoder{
    49  		reader:           r,
    50  		resettableReader: opts.RWOptions().ResettableReaderFn()(r, rOpts),
    51  		buffer:           getByteSliceWithLength(sizeEncodingLength, pool),
    52  		bytesPool:        pool,
    53  		maxMessageSize:   opts.MaxMessageSize(),
    54  		rOpts:            rOpts,
    55  		opts:             opts,
    56  	}
    57  }
    58  
    59  func (d *decoder) Decode(m Unmarshaler) error {
    60  	size, err := d.decodeSize()
    61  	if err != nil {
    62  		return err
    63  	}
    64  	if size > d.maxMessageSize {
    65  		d.resettableReader.Reset(d.reader)
    66  		return fmt.Errorf(
    67  			"proto decoded message size %d is larger than maximum supported size %d",
    68  			size, d.maxMessageSize)
    69  	}
    70  	d.buffer = growDataBufferIfNeeded(d.buffer, sizeEncodingLength+size, d.bytesPool)
    71  	return d.decodeData(d.buffer[sizeEncodingLength:sizeEncodingLength+size], m)
    72  }
    73  
    74  func (d *decoder) decodeSize() (int, error) {
    75  	_, err := io.ReadFull(d.resettableReader, d.buffer[:sizeEncodingLength])
    76  	if err != nil {
    77  		return 0, err
    78  	}
    79  	size := sizeEncodeDecoder.Uint32(d.buffer[:sizeEncodingLength])
    80  	return int(size), nil
    81  }
    82  
    83  func (d *decoder) decodeData(buffer []byte, m Unmarshaler) error {
    84  	_, err := io.ReadFull(d.resettableReader, buffer)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	return m.Unmarshal(buffer)
    89  }
    90  
    91  func (d *decoder) ResetReader(r io.Reader) {
    92  	d.reader = r
    93  	d.resettableReader.Reset(r)
    94  }