codec

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2025 License: Apache-2.0 Imports: 11 Imported by: 4

Documentation

Index

Constants

View Source
const (
	DefaultMaxMessageLength     int = 8 * 1024
	DefaultWarningEncodedMsgLen int = 4 * 1024
)

Variables

View Source
var (
	DefaultLengthByteOrder = binary.LittleEndian
	DefaultLengthSize      = 2
	MaxLengthSize          = 4
)

Functions

This section is empty.

Types

type Codec

type Codec interface {

	// Encode
	//	@Description: 消息编码
	//	@param msg 输入消息
	//	@return []bytes 编码结果输出
	//	@return error
	//
	Encode(msg any) ([]byte, error)

	// Decode
	//	@Description: 消息解码
	//	@param []byte 输入
	//	@return []Msg 解码消息输出
	//	@return totalLen 消息所占字节大小
	//	@return error
	//
	Decode(reader gnet.Reader) ([]any, int, error)
}

Codec

@Description: 编解码器

func NewLengthFieldCodec

func NewLengthFieldCodec(opts ...Option[*LengthOptions]) (Codec, error)

type LengthOptions

type LengthOptions struct {
	OptionsBase
	// 字节序
	ByteOrder binary.ByteOrder
	// 消息长度所占字节数
	LengthSize int
	// 大消息编码函数
	EncodeLargeMessage func(largeMsg []byte, maxLen int) (out []byte, err error)
}

func (*LengthOptions) Complete

func (opts *LengthOptions) Complete() error

type Option

type Option[T Options] func(o T)

func WithByteOrder

func WithByteOrder(value binary.ByteOrder) Option[*LengthOptions]

func WithEncodeLargeMessage added in v0.0.4

func WithEncodeLargeMessage(function func(largeMsg []byte, maxLen int) (out []byte, err error)) Option[*LengthOptions]

func WithLengthSize

func WithLengthSize(value int) Option[*LengthOptions]

func WithMagic

func WithMagic[T Options](magicBytes []byte) Option[T]

func WithMaxDecodedLength

func WithMaxDecodedLength[T Options](maxDecodedLength int) Option[T]

func WithMaxEncodedLength

func WithMaxEncodedLength[T Options](maxEncodedLength int) Option[T]

func WithMessageCodec

func WithMessageCodec[T Options](messageCodec message.Codec) Option[T]

func WithWarningEncodedLength

func WithWarningEncodedLength[T Options](warningEncodedLength int) Option[T]

type Options

type Options interface {
	GetMagicBytes() []byte
	SetMagicBytes([]byte)
	GetMagicSize() int

	GetMaxDecodedLength() int
	SetMaxDecodedLength(int)

	GetMaxEncodedLength() int
	SetMaxEncodedLength(int)

	GetWarningEncodedLength() int
	SetWarningEncodedLength(int)

	GetMessageCodec() message.Codec
	SetMessageCodec(message.Codec)
}

type OptionsBase

type OptionsBase struct {
	// contains filtered or unexported fields
}

func (*OptionsBase) Complete

func (opts *OptionsBase) Complete() error

func (*OptionsBase) GetMagicBytes

func (opts *OptionsBase) GetMagicBytes() []byte

func (*OptionsBase) GetMagicSize

func (opts *OptionsBase) GetMagicSize() int

func (*OptionsBase) GetMaxDecodedLength

func (opts *OptionsBase) GetMaxDecodedLength() int

func (*OptionsBase) GetMaxEncodedLength

func (opts *OptionsBase) GetMaxEncodedLength() int

func (*OptionsBase) GetMessageCodec

func (opts *OptionsBase) GetMessageCodec() message.Codec

func (*OptionsBase) GetWarningEncodedLength

func (opts *OptionsBase) GetWarningEncodedLength() int

func (*OptionsBase) SetMagicBytes

func (opts *OptionsBase) SetMagicBytes(value []byte)

func (*OptionsBase) SetMaxDecodedLength

func (opts *OptionsBase) SetMaxDecodedLength(value int)

func (*OptionsBase) SetMaxEncodedLength

func (opts *OptionsBase) SetMaxEncodedLength(value int)

func (*OptionsBase) SetMessageCodec

func (opts *OptionsBase) SetMessageCodec(value message.Codec)

func (*OptionsBase) SetWarningEncodedLength

func (opts *OptionsBase) SetWarningEncodedLength(value int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL