github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/protocol/codec.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package protocol
     4  
     5  // Codec wraps some other interfaces to act an object as a codec.
     6  // Differences:
     7  // - Marshal() don't think about any other parts and make a byte slice and serialize data to it.
     8  // - MarshalTo() care about the fact that serialized data must wrap with other data and serialize data in the given byte slice.
     9  // - Encode() like MarshalTo() but encode to a buffer not a byte slice by Buffer interface methods. buf can be a temp or final write location.
    10  type Codec interface {
    11  	MediaType() MediaType
    12  	CompressType() CompressType
    13  
    14  	Decoder
    15  	Encoder
    16  
    17  	Unmarshaler
    18  	Marshaler
    19  }
    20  
    21  // Decoder is the interface that wraps the Decode method.
    22  type Decoder interface {
    23  	// Decode read and decode data until end of needed data or occur error.
    24  	// Unlike Buffer.ReadFrom() it isn't read until EOF and just read needed data.
    25  	Decode(reader Codec) (n int, err Error)
    26  }
    27  
    28  // Encoder is the interface that wraps the Encode & Len methods.
    29  type Encoder interface {
    30  	// Encode writes serialized(encoded) data to writer until there's no more data to write.
    31  	// It like Buffer.WriteTo() with a very tiny difference that this method know about the serialized data but WriteTo just know marshaled data is a binary data.
    32  	// It use in old OSs fashion or stream writing in large data length. Old OSs do some logic in kernel e.g. IP/TCP packeting, ... that need heavy context switching logic.
    33  	// It will care about how to write data to respect performance. Usually by make temp fixed size buffer like bufio package.
    34  	Encode(writer Codec) (n int, err Error)
    35  	// Len return value n is the number of bytes that will written as encode data. 0 means no data and -1 means can't tell until full write.
    36  	Len() (ln int)
    37  }
    38  
    39  // Unmarshaler is the interface that wraps the Unmarshal method.
    40  type Unmarshaler interface {
    41  	// Unmarshal reads and decode data from given slice until end of data or occur error
    42  	Unmarshal(data []byte) (n int, err Error)
    43  	UnmarshalFrom(data []byte) (remaining []byte, err Error)
    44  }
    45  
    46  // Marshaler is the interface that wraps the Marshal methods.
    47  // Return any error that occur in logic e.g. timeout error in socket, ...
    48  type Marshaler interface {
    49  	// Marshal serialized(encoded) data and return the byte slice hold serialized data.
    50  	Marshal() (data []byte, err Error)
    51  	// MarshalTo serialized(encoded) data to given slice from len to max cap and save marshal state for future call.
    52  	// It is very similar to Read() in Reader interface but with one difference behavior that this method don't need temporary buffer
    53  	MarshalTo(data []byte) (added []byte, err Error)
    54  	// Len return value n that is the number of bytes that will written by Marshal()||MarshalTo()
    55  	Len() (ln int)
    56  }
    57  
    58  type SerializeLen interface {
    59  	ComputeLen() (ln int)
    60  	Len() (ln int)
    61  }