packet

package
v0.0.0-...-f683eec Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetPacketCodec

func SetPacketCodec(codec Codec)

func SetPacketEncoder

func SetPacketEncoder(coder Encoder)

func SetPacketPool

func SetPacketPool(p Pool)

SetPacketPool set default packet pool

func SetPacketWraper

func SetPacketWraper(p ProtocolWraper)

SetPacketPool set default packet pool

Types

type Codec

type Codec interface {
	Marshal(p interface{}) ([]byte, error)
	Unmarshal(data []byte, p interface{}) error
}

Codec Use for marshal/unmarshal packet.Packet from bytes

var BytesMIDCodec Codec = codecMID{}
var BytesURICodec Codec = codecURI{}

func GetCodec

func GetCodec() Codec

type Encoder

type Encoder interface {
	Encode(buf []byte) []byte
	Decode(buf []byte) []byte
}

Encoder use for encode and decode source packet

var EmtpyPacketEncoder Encoder = emtpyPacketCoder{}

EmtpyPacketEncoder not encode and decode source packet

func GetEncoder

func GetEncoder() Encoder

func NewTeeCoder

func NewTeeCoder(coders ...Encoder) Encoder

type Packet

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

Packet define a network packet struct

func NewPacket

func NewPacket() *Packet

func NewTestPacket

func NewTestPacket(cmd PacketCmd, payload []byte, md metadata.MD) *Packet

func (*Packet) CleanForTest

func (p *Packet) CleanForTest()

func (*Packet) Cmd

func (p *Packet) Cmd() PacketCmd

func (*Packet) CustomFlag

func (p *Packet) CustomFlag(i byte) bool

func (*Packet) GetMD

func (p *Packet) GetMD() metadata.MD

func (*Packet) HasFlag

func (p *Packet) HasFlag(f PacketFlag) bool

func (*Packet) MarshalLogObject

func (p *Packet) MarshalLogObject(enc zapcore.ObjectEncoder) (err error)

func (*Packet) MsgID

func (p *Packet) MsgID() uint32

func (*Packet) Payload

func (p *Packet) Payload() []byte

func (*Packet) SessionID

func (p *Packet) SessionID() uint64

func (*Packet) SetCmd

func (p *Packet) SetCmd(cmd PacketCmd)

func (*Packet) SetCustomFlag

func (p *Packet) SetCustomFlag(i byte, set bool)

i = 1-8

func (*Packet) SetFlag

func (p *Packet) SetFlag(f PacketFlag, set bool)

func (*Packet) SetMD

func (p *Packet) SetMD(md metadata.MD)

func (*Packet) SetMsgID

func (p *Packet) SetMsgID(id uint32)

func (*Packet) SetSeesonID

func (p *Packet) SetSeesonID(id uint64)

func (*Packet) SetURI

func (p *Packet) SetURI(uri string)

func (*Packet) URI

func (p *Packet) URI() string

type PacketCmd

type PacketCmd byte

PacketCmd first byte,

const (
	// CmdNotify notify,push,oneway
	CmdNotify PacketCmd = iota
	CmdRequest
	CmdResponse
)

type PacketFlag

type PacketFlag byte

PacketFlag second byte,internal message flag.

const (
	// FlagError message is an error response
	FlagError PacketFlag = 0x01
)

type Pool

type Pool interface {
	Get() interface{}
	Put(interface{})
}

Pool reuse packet pool

var NoPacketPool Pool = noopPacketPool{}

NoPacketPool not use packet pools

var SyncPacketPool Pool = &syncPacketPool{
	Pool: sync.Pool{
		New: func() interface{} {
			return NewPacket()
		},
	},
}

SyncPacketPool use sync.Pool

func GetPool

func GetPool() Pool

GetPacketPool get default packet pool

type ProtocolWraper

type ProtocolWraper interface {
	// unmarshal packet's payload by msg codec
	PayloadUnmarshal(pkg interface{}, codec message.Codec, obj interface{}) error
	// marshal packet's payload by msg codec,then set payload binary data into message buf.
	PayloadMarshal(pkg interface{}, codec message.Codec, payload interface{}) (err error)
	// new response packet
	NewResponse(inPkg, outPkg interface{}, md metadata.MD) (err error)
	// new request packet
	NewPacket(inPkg interface{}, cmd PacketCmd, uri interface{}, md metadata.MD) (err error)
	// get pkg metadata
	GetMetadata(pkg interface{}) (md metadata.MD, err error)
}

ProtocolWraper wrap all packet operate, use for custom packet struct.

func GetProtocolWraper

func GetProtocolWraper() ProtocolWraper

GetPacketPool get default packet pool

func NewPacketWraper

func NewPacketWraper() ProtocolWraper

Jump to

Keyboard shortcuts

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