func (c *defaultCodec) Encode(ctx context.Context, message remote.Message, out remote.ByteBuffer) error {
	defer func() {
		ri := message.RPCInfo()
		rpcinfo.AsMutableRPCStats(ri.Stats()).SetSendSize(uint64(out.MallocLen()))
	}()
	var err error
	var totalLenField []byte
	var framedLenField []byte
	var headerLen int
	tp := message.ProtocolInfo().TransProto
	if tp&transport.TTHeader == transport.TTHeader {
		if totalLenField, err = ttHeaderCodec.encode(ctx, message, out); err != nil {
			return err
		}
		headerLen = out.MallocLen()
	}
	if tp&transport.Framed == transport.Framed {
		if framedLenField, err = out.Malloc(Size32); err != nil {
			return err
		}
		headerLen += Size32
	}
	if err := c.encodePayload(ctx, message, out); err != nil {
		return err
	}
	if tp&transport.Framed == transport.Framed {
		if framedLenField == nil {
			return perrors.NewProtocolErrorWithMsg("no buffer allocated for the framed length field")
		}
		binary.BigEndian.PutUint32(framedLenField, uint32(out.MallocLen()-headerLen))
	} else if message.ServiceInfo().PayloadCodec == serviceinfo.Protobuf {
		return perrors.NewProtocolErrorWithMsg("protobuf just support 'framed' trans proto")
	}
	if tp&transport.TTHeader == transport.TTHeader {
		if totalLenField == nil {
			return perrors.NewProtocolErrorWithMsg("no buffer allocated for the header length field")
		}
		binary.BigEndian.PutUint32(totalLenField, uint32(out.MallocLen()-Size32))
	}
	return nil
}
Encode implements the remote.Codec interface, it does complete message encode include header and payload.
package codec
import (
	"context"
	"encoding/binary"
	"fmt"
	"sync/atomic"
	"github.com/cloudwego/kitex/pkg/kerrors"
	"github.com/cloudwego/kitex/pkg/remote"
	"github.com/cloudwego/kitex/pkg/remote/codec/perrors"
	"github.com/cloudwego/kitex/pkg/retry"
	"github.com/cloudwego/kitex/pkg/rpcinfo"
	"github.com/cloudwego/kitex/pkg/serviceinfo"
	"github.com/cloudwego/kitex/transport"
)
const (
	Size32 = 4
	Size16 = 2
)
const (
	ThriftV1Magic = 0x80010000
	ProtobufV1Magic = 0x90010000
	MagicMask = 0xffff0000
)
var (
	ttHeaderCodec   = ttHeader{}
	meshHeaderCodec = meshHeader{}
)
func NewDefaultCodec() remote.Codec {
	return &defaultCodec{}
}
type defaultCodec struct{}