codec

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: GPL-3.0 Imports: 4 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON     = 1
	ContentTypeProtobuf = 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	// Pack encode source message by *Packet object
	Pack(packet *Packet, data any) ([]byte, error)

	// Unpack decode source message into *Packet object
	Unpack(packet *Packet, msg []byte) error
}

Codec represents a interface that pack/unpack source message

func Default

func Default(opts ...Option) Codec

Default returns the default codec implementation,the packet is composed by : |-------------- header ------------- |-------- body --------| |packetLength|operate|contentType|seq|-------- body --------| | 4 | 2 | 2 | 2 | n |

type DefaultCodec

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

func (DefaultCodec) Pack

func (codec DefaultCodec) Pack(packet *Packet, data any) ([]byte, error)

func (DefaultCodec) Unpack

func (codec DefaultCodec) Unpack(packet *Packet, msg []byte) error

type Option

type Option func(options *Options)

type Options

type Options struct {
	// ContentType is data content type
	ContentType int
	// contains filtered or unexported fields
}

Options represents codec options

type Packet

type Packet struct {
	Operate     int16
	ContentType int16
	Seq         int16
	Body        []byte
}

func (*Packet) Marshal

func (packet *Packet) Marshal(data any) ([]byte, error)

Marshal marshals the given data into body by content type

func (*Packet) Reset

func (packet *Packet) Reset()

func (*Packet) Unmarshal

func (packet *Packet) Unmarshal(data any) error

Unmarshal parses the body by content type and stores the result

Jump to

Keyboard shortcuts

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