github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/codec.go (about)

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