pack

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProtocolLength = 82
	PackagerLength = 8
)
View Source
const (
	MagicNumber = 0x80DFEC60
)

头信息

Variables

View Source
var (
	ProtocolJson    = Protocol{'J', 'S', 'O', 'N', '\000', 'Y', 'A', 'R'}
	ProtocolMsgpack = Protocol{'M', 'S', 'G', 'P', 'A', 'C', 'K'}
)

Functions

This section is empty.

Types

type EncoderJson

type EncoderJson struct {
}

JSON处理器

func (*EncoderJson) ContentType

func (p *EncoderJson) ContentType() string

func (*EncoderJson) Decode

func (p *EncoderJson) Decode(body []byte, response *Response) error

func (*EncoderJson) Encode

func (p *EncoderJson) Encode(request *Request) ([]byte, error)

func (*EncoderJson) ShowProtocol added in v0.1.14

func (p *EncoderJson) ShowProtocol() Protocol

type EncoderMsgpack

type EncoderMsgpack struct {
}

msgpack处理器,兼容json tag定义

func (*EncoderMsgpack) ContentType

func (p *EncoderMsgpack) ContentType() string

func (*EncoderMsgpack) Decode

func (p *EncoderMsgpack) Decode(body []byte, response *Response) error

func (*EncoderMsgpack) Encode

func (p *EncoderMsgpack) Encode(request *Request) ([]byte, error)

func (*EncoderMsgpack) ShowProtocol added in v0.1.14

func (p *EncoderMsgpack) ShowProtocol() Protocol

type Exception

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

供JSON解析及外部使用的异常结构体

func (*Exception) Error

func (e *Exception) Error() string

func (*Exception) GetCode

func (e *Exception) GetCode() int32

func (*Exception) GetMeta

func (e *Exception) GetMeta() (result map[string]interface{})

func (*Exception) UnmarshalJSON

func (e *Exception) UnmarshalJSON(b []byte) (err error)
type Header struct {
	Id          uint32
	Version     uint16
	MagicNumber uint32
	Reserved    uint32
	Provider    [28]byte
	Encrypt     uint32
	Token       [32]byte
	BodyLength  uint32
	Packager    Protocol
}

func NewHeader

func NewHeader(packager Protocol) *Header

func NewHeaderWithBody

func NewHeaderWithBody(body []byte, packager Protocol) *Header

func (*Header) Bytes

func (h *Header) Bytes() *bytes.Buffer

func (*Header) Init

func (h *Header) Init(payload *bytes.Buffer) bool

type Pack

type Pack interface {
	Encode(*Request) ([]byte, error)
	ContentType() string
	Decode([]byte, *Response) error
	ShowProtocol() Protocol
}

定义数据编码解码接口

func GetPackHandler

func GetPackHandler(protocol Protocol) Pack

根据协议获取编码、解码器

type Protocol

type Protocol [8]byte

请求协议

type Request

type Request struct {
	Protocol Protocol    `json:"-" msgpack:"-"`
	Addr     string      `json:"-" msgpack:"-"`
	Id       uint32      `json:"i" msgpack:"i"`
	Method   string      `json:"m" msgpack:"m"`
	Params   interface{} `json:"p" msgpack:"p"`
}

请求结构体

func NewRequest

func NewRequest(addr string, method string, params interface{}) (request *Request)

type Response

type Response struct {
	Protocol Protocol    `json:"-" msgpack:"-"`
	Id       uint32      `json:"i" msgpack:"i"`
	Except   *Exception  `json:"e" msgpack:"e"`
	Out      string      `json:"o" msgpack:"o"`
	Status   StatusType  `json:"s" msgpack:"s"`
	Retval   interface{} `json:"r" msgpack:"r"`
}

响应结构体

type StatusType

type StatusType int

响应

const (
	StatusOkey       StatusType = 0x0
	ErrPackager      StatusType = 0x1
	ErrProtocol      StatusType = 0x2
	ErrRequest       StatusType = 0x4
	ErrOutput        StatusType = 0x8
	ErrTransport     StatusType = 0x10
	ErrForbidden     StatusType = 0x20
	ErrException     StatusType = 0x40
	ErrEmptyResponse StatusType = 0x80
)

Jump to

Keyboard shortcuts

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