codec

package
v0.0.0-...-bd521cb Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 18 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
	VerUpgradeReq     = 9
	VerUpgradeResp    = 10
	VerCallErrResp    = 11
	VerUpgradeErrResp = 12
)
View Source
const (
	HeaderSizeRaw = 6
	HeaderSize    = 16
	MaxBuffSize   = HeaderSize + math.MaxUint16
)

Variables

View Source
var (
	ErrUpgradeInProgress = errors.NewCode(0, -1, "upgrade is in progress")
	ErrUpgradeFailed     = errors.NewCode(0, -1, "upgrade failed")
	ErrUpgradeClosed     = errors.NewCode(0, -1, "upgrade closed")
)
View Source
var ErrHasBeenClosed = errors.NewCode(0, 0x1111, "has been closed")
View Source
var ErrHasBeenUpgraded = errors.NewCode(0, 0x1111, "has been upgraded")
View Source
var (
	ErrUnexpected = errors.NewCode(0, 1, "codec unexpected error")
)

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, rwc io.ReadWriteCloser, callers []Method, ctxPassKeys []string, needHeartbeat bool) (c *Codec, err error)

ctxPassKeys: 作为cli时, ctx 中需要透传给对方的key

func (*Codec) ClientCall

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

func (*Codec) Close

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

func (*Codec) Done

func (rpc *Codec) Done() <-chan struct{}

func (*Codec) GetUpgrade

func (c *Codec) GetUpgrade(ctx context.Context, callID uint16, caller Method) (upgrade *Upgrade, err error)

服务端通过 GetUpgrade() 获取 Upgrade 对象

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()

func (*Codec) IsClosed

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

func (*Codec) ReadLoop

func (c *Codec) ReadLoop()

func (*Codec) Send

func (c *Codec) Send(ctx context.Context, wbuf []byte, ver, callID, ctxlen 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, 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, callID uint16, callSN uint32, msg Msg) (err error)

func (*Codec) Stream

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

func (*Codec) StreamCall

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

func (*Codec) Upgrade

func (c *Codec) Upgrade(ctx context.Context, callID uint16, req, res Msg) (upgrade *Upgrade, err error)

client 端发起升级请求,服务端收到升级请求后,切换到 stream 模式,之后 client 和 server 都可以在 stream 上读写数据

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)

func (*Codec) VerUpgradeReq

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

服务端收到 Upgrade() 请求后调用此函数处理逻辑

func (*Codec) VerUpgradeResp

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

调用 Upgrade() 后的返回值

type Header struct {
	Len    uint16 // 版本号, 0:raw bytes, 1:CallReq, 2:CallResp; size: 2
	CtxLen uint16 // Ctx透传 body 长度, ctx 最长  65532
	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.ReadCloser, 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 LogidKey

type LogidKey struct{}

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 PbErr

type PbErr struct {
	base.Err
}

func (PbErr) Code

func (e PbErr) Code() int

func (PbErr) Error

func (e PbErr) Error() string

func (PbErr) Msg

func (e PbErr) Msg() string

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) 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)

type Upgrade

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

func GetUpgrade

func GetUpgrade(ctx context.Context) (upgrade *Upgrade)

func (*Upgrade) Close

func (s *Upgrade) Close() error

func (*Upgrade) Read

func (s *Upgrade) Read(p []byte) (n int, err error)

func (*Upgrade) Write

func (s *Upgrade) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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