codec

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2021 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//type of Encoder and Decoder
	//the type can be gob,json,protobuf.
	//the protobuf is the most efficient
	GobType  string = "application/gob"
	JsonType string = "application/json"
)
View Source
const MagicNumber = 0x3bef5c

Variables

View Source
var DefaultOption = &Option{
	MagicNumber:    MagicNumber,
	CodecType:      "application/gob",
	ConnectTimeOut: time.Second * 10,
}

if not diffine Opiton will use defaultOption

View Source
var NewCodecFuncMap map[string]NewCodecFunc

Functions

func Init

func Init()

init NewCodecFuncMap

Types

type Codec

type Codec interface {
	io.Closer
	ReadHeader(*Header) error
	ReadBody(interface{}) error
	Write(*Header, interface{}) error
}

interface for coding Message

func NewGobCodec

func NewGobCodec(conn io.ReadWriteCloser) Codec

func NewJsonCodec

func NewJsonCodec(conn io.ReadWriteCloser) Codec

type GobCodec

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

Gob is less efficient than json

func (*GobCodec) Close

func (g *GobCodec) Close() error

func (*GobCodec) ReadBody

func (g *GobCodec) ReadBody(body interface{}) error

func (*GobCodec) ReadHeader

func (g *GobCodec) ReadHeader(h *Header) error

func (*GobCodec) Write

func (g *GobCodec) Write(h *Header, body interface{}) error
type Header struct {
	ServiceMethod string //format "Service.Method" call mathordName
	SeqNum        uint64 //sequence number chosen by client
	Error         string
}

head means server name and method name outher info in body such as args,reply,error

type JsonCodec

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

func (*JsonCodec) Close

func (j *JsonCodec) Close() error

func (*JsonCodec) ReadBody

func (j *JsonCodec) ReadBody(body interface{}) error

func (*JsonCodec) ReadHeader

func (j *JsonCodec) ReadHeader(h *Header) error

func (*JsonCodec) Write

func (j *JsonCodec) Write(h *Header, body interface{}) error

type NewCodecFunc

type NewCodecFunc func(io.ReadWriteCloser) Codec

type Option

type Option struct {
	MagicNumber    int           // MagicNum marks this's a geerpc request
	CodecType      string        // client may choose different Codec to encode body such as json or gob
	ConnectTimeOut time.Duration // client connect to server time out
	HandleTimeOut  time.Duration // server handle time out
}

Jump to

Keyboard shortcuts

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