github.com/koko1123/flow-go-1@v0.29.6/network/codec.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package network
     4  
     5  import (
     6  	"io"
     7  )
     8  
     9  // Codec provides factory functions for encoders and decoders.
    10  type Codec interface {
    11  	NewEncoder(w io.Writer) Encoder
    12  	NewDecoder(r io.Reader) Decoder
    13  	Encode(v interface{}) ([]byte, error)
    14  
    15  	// Decode decodes a message.
    16  	// Expected error returns during normal operations:
    17  	//  - codec.ErrInvalidEncoding if message encoding is invalid.
    18  	//  - codec.ErrUnknownMsgCode if message code byte does not match any of the configured message codes.
    19  	//  - codec.ErrMsgUnmarshal if the codec fails to unmarshal the data to the message type denoted by the message code.
    20  	Decode(data []byte) (interface{}, error)
    21  }
    22  
    23  // Encoder encodes the given message into the underlying writer.
    24  type Encoder interface {
    25  	Encode(v interface{}) error
    26  }
    27  
    28  // Decoder decodes from the underlying reader into the given message.
    29  // Expected error returns during normal operations:
    30  //   - codec.ErrInvalidEncoding if message encoding is invalid.
    31  //   - codec.ErrUnknownMsgCode if message code byte does not match any of the configured message codes.
    32  //   - codec.ErrMsgUnmarshal if the codec fails to unmarshal the data to the message type denoted by the message code.
    33  type Decoder interface {
    34  	Decode() (interface{}, error)
    35  }