codec

package
v0.0.0-...-ace9c2d Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VerHeartbeat  = 0
	VerRaw        = 1
	VerCallReq    = 2
	VerCallResp   = 3
	VerClose      = 4
	VerCmdReq     = 5 // 发送控制命令
	VerCmdResp    = 6 // 响应控制命令
	VerStreamReq  = 7
	VerStreamResp = 8
)
View Source
const (
	HeaderSizeRaw = 6
	HeaderSize    = 16
	MaxBuffSize   = HeaderSize + math.MaxUint16
)

Variables

View Source
var ErrHasBeenClosed = errors.NewCode(0, 0x1111, "has been closed")

Functions

func ParseHeaderLen

func ParseHeaderLen(bs []byte) (l uint16)

Types

type Codec

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

func NewCodec

func NewCodec(ctx context.Context, cancel context.CancelFunc, rwc io.ReadWriteCloser, callers []Method, needHeartbeat bool) (c *Codec, err error)

func (*Codec) ClientCall

func (c *Codec) ClientCall(ctx context.Context, channel, callID uint16, req, res Msg) (done <-chan error, err error)

func (*Codec) Close

func (c *Codec) Close() (err error)

func (*Codec) Handler

func (c *Codec) Handler(ctx context.Context, caller Method, header Header, req Msg) (resp Msg, err error)

func (*Codec) Heartbeat

func (c *Codec) Heartbeat(ctx context.Context, cancel context.CancelFunc)

func (*Codec) IsClosed

func (c *Codec) IsClosed() (yes bool)

func (*Codec) ReadLoop

func (c *Codec) ReadLoop(ctx context.Context, cancel context.CancelFunc)

func (*Codec) Send

func (c *Codec) Send(ctx context.Context, wbuf []byte, ver, channel, callID uint16, callSN uint32) (err error)

func (*Codec) SendCloseMsg

func (c *Codec) SendCloseMsg(ctx context.Context) (err error)

func (*Codec) SendCmd

func (c *Codec) SendCmd(ctx context.Context, channel uint16, req *base.CmdReq) (res *base.CmdRsp, err error)

func (*Codec) SendHeartbeatMsg

func (c *Codec) SendHeartbeatMsg(ctx context.Context) (err error)

func (*Codec) SendMsg

func (c *Codec) SendMsg(ctx context.Context, ver, channel, callID uint16, callSN uint32, msg Msg) (err error)

func (*Codec) Stream

func (c *Codec) Stream(ctx context.Context, channel uint16, callID uint16, caller Method, sync bool) (stream *Stream, err error)

func (*Codec) StreamCall

func (c *Codec) StreamCall(ctx context.Context, ver, channel, callID uint16, callSN uint32, req Msg) (err error)

func (*Codec) VerCallReq

func (c *Codec) VerCallReq(ctx context.Context, header Header, bsBody []byte) (err error)

收到 Req

func (*Codec) VerCallResp

func (c *Codec) VerCallResp(ctx context.Context, header Header, bsBody []byte) (err error)

收到 Resp

func (*Codec) VerCmdReq

func (c *Codec) VerCmdReq(ctx context.Context, header Header, bsBody []byte) (err error)

收到 Req

func (*Codec) VerStreamReq

func (c *Codec) VerStreamReq(ctx context.Context, header Header, bsBody []byte) (err error)

func (*Codec) VerStreamResp

func (c *Codec) VerStreamResp(ctx context.Context, header Header, bsBody []byte) (err error)
type Header struct {
	Len     uint16 // 版本号, 0:raw bytes, 1:CallReq, 2:CallResp; size: 2
	Channel uint16 // 通道,通道见彼此独立, size: 2
	Ver     uint16 // 消息长度, size: 2

	// Len != 0 才需要
	SegmentCount uint16 // 大包拆包总数量, size: 2
	SegmentIdx   uint16 // 大包拆包编号, size: 2
	CallID       uint16 // 调用ID, size: 4; 连接建立后,将 caller func name 与id映射一次
	CallSN       uint32 // 调用序列号, size: 4; stream 的 callSN 和 Call 要保持独立, 因为stream回头有可能会碰撞
}

func ParseHeader

func ParseHeader(bs []byte) (h Header, l int)

func ReadPack

func ReadPack(ctx context.Context, r io.Reader, buf []byte) (header Header, bsBody []byte, err error)

ReadPack 读一个裸消息

func (*Header) Format

func (h *Header) Format(bs []byte) (out []byte)

func (*Header) FormatCall

func (h *Header) FormatCall(bs []byte) (out []byte)

func (*Header) FormatRaw

func (h *Header) FormatRaw(bs []byte) (out []byte)

type Method

type Method interface {
	ReqType() reflect.Type
	RespType() reflect.Type
	NewReq() Msg
	NewResp() Msg
	SvcInvoke(ctx context.Context, req Msg) (resp Msg, err error)
	FuncName() string
}

Method 表示一个 struct 的可以调用的方法

type Msg

type Msg interface {
	proto.Message
}

type Stream

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

func GetStream

func GetStream(ctx context.Context) (stream *Stream)

func (*Stream) Clear

func (s *Stream) Clear(ctx context.Context)

Clear 清空以前接收的数据(准备好接收下一个数据包)

func (*Stream) Close

func (s *Stream) Close(ctx context.Context)

func (*Stream) Method

func (s *Stream) Method() (method string)

func (*Stream) Recv

func (s *Stream) Recv(ctx context.Context) (resp Msg, err error)

func (*Stream) Send

func (s *Stream) Send(ctx context.Context, req Msg) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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