codec

package
v0.0.0-...-3b1ec4e Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MarshalTypePB = MarshalType(iota)
	MarshalTypeJSON
)
View Source
const (
	CompressGZip = CompressType(iota)
)

Variables

View Source
var MarshalInvalidPBErr = errors.New("pkg isn't proto.Message")

Functions

func ContextWithSession

func ContextWithSession(ctx context.Context, session Session) context.Context

ContextWithSession return new context carrying value `session`

func DeregisterMarshaler

func DeregisterMarshaler(typ MarshalType)

DeregisterMarshaler deregisters the marshaler of specific MarshalType, this is concurrent-safe.

func RegisterCodec

func RegisterCodec(proto string, server, client Codec)

register the client and server codec to framework

func RegisterMarshaler

func RegisterMarshaler(typ MarshalType, marshaler Marshaler)

RegisterMarshaler registers a new marshaler implemention of specific MarshalType, this is concurrent-safe.

func RegisterSessionBuilder

func RegisterSessionBuilder(proto string, builder SessionBuilder)

RegisterSessionBuilder register extended SessionBuilder for protocol `proto`

Types

type BaseSession

type BaseSession struct {
	ReqHead interface{}
	RspHead interface{}
}

BaseSession implements some basic methods defined in `Session`

func (*BaseSession) Request

func (r *BaseSession) Request() interface{}

func (*BaseSession) Response

func (r *BaseSession) Response() interface{}

func (*BaseSession) SetResponse

func (r *BaseSession) SetResponse(rsp interface{})

type Codec

type Codec interface {
	// codec name
	Name() string

	// Encode encode 'pkg' into []byte, here 'pkg' must be []byte,
	// which is marshaled and compressed data.
	Encode(pkg interface{}) (data []byte, err error)

	// Decode decode []byte, return decoded body data and length,
	// which is marshaled and compressed data.
	Decode(data []byte) (req interface{}, n int, err error)
}

encode * decode client and server side should impl the codec

func ClientCodec

func ClientCodec(proto string) Codec

return client side's codec

func ServerCodec

func ServerCodec(proto string) Codec

retrun server side's codec

type CompressType

type CompressType int

compress type

type Compressor

type Compressor interface {
	// compress data
	Compress(data []byte) ([]byte, error)
	// decomress data
	Decompress(data []byte) ([]byte, error)
}

type GZipCompressor

type GZipCompressor struct{}

func (*GZipCompressor) Compress

func (r *GZipCompressor) Compress(data []byte) ([]byte, error)

func (*GZipCompressor) Decompress

func (r *GZipCompressor) Decompress(data []byte) ([]byte, error)

type MarshalType

type MarshalType int

type Marshaler

type Marshaler interface {
	// Marshal serializes 'pkg' to []byte, return the data,
	// return error if any error encountered.
	Marshal(pkg interface{}) (data []byte, err error)

	// Unmarhshal deserializes []byte to pkg, which must be a pointer,
	// return error if any error encountered.
	Unmarshal(data []byte, pkg interface{}) error
}

Marshaler defines how to serialize/deserialize

type PBMarshaler

type PBMarshaler struct{}

PBMarshaler google protocolbuffers marshaling

func (*PBMarshaler) Marshal

func (m *PBMarshaler) Marshal(pkg interface{}) (data []byte, err error)

Marshal marshal pkg in protocolbuffers manner

func (*PBMarshaler) Unmarshal

func (m *PBMarshaler) Unmarshal(data []byte, pkg interface{}) error

Unmarshal unmarshal data into pkg, which must be proto.Message

type Session

type Session interface {
	RPCName() string

	Request() interface{}

	Response() interface{}

	SetResponse(rsp interface{})

	SetError(err error)
}

define the rpc context for a request the session init instally in client side when server side receive the requset, session init in server side maybe call `RpcContext`

func SessionFromContext

func SessionFromContext(ctx context.Context) Session

SessionFromContext return Session carried by `ctx`

type SessionBuilder

type SessionBuilder interface {
	Build(reqHead interface{}) (Session, error)
}

SessionBuilder when extending protocols, SessionBuilder should be implemented and registered to help build the `Session`.

func GetSessionBuilder

func GetSessionBuilder(proto string) SessionBuilder

GetSessionBuilder return SessionBuilder for protocol `proto`

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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