proto

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2022 License: MIT Imports: 10 Imported by: 5

Documentation

Index

Constants

View Source
const (
	// max size of content: 1G
	MaxSize = 1 << 30
	// max message type
	MaxType = 1 << 31
)

Variables

View Source
var (
	ErrVarintOverflow         = errors.New("proto: varint overflow")
	ErrSizeOverflow           = errors.New("proto: size overflow")
	ErrTypeOverflow           = errors.New("proto: type overflow")
	ErrOutOfRange             = errors.New("proto: out of range")
	ErrUnsupportedContentType = errors.New("proto: unsupported content type")
)

Functions

func Encode

func Encode(m Message, reservedHeadLen int) ([]byte, error)

Encode returns the wire-format encoding of m with type and size.

|type|body.size|body|

func EncodeAppend

func EncodeAppend(buf []byte, m Message) ([]byte, error)

EncodeAppend encodes m to buf

func EncodeSize

func EncodeSize(buf []byte, size int) int

EncodeSize encodes type as varint to buf and returns number of bytes written.

func EncodeType

func EncodeType(buf []byte, typ Type) int

EncodeType encodes type as varint to buf and returns number of bytes written.

func ErrUnrecognizedType

func ErrUnrecognizedType(typ Type) error

func FreeBuffer

func FreeBuffer(b *Buffer)

FreeBuffer puts buffer to pool if cap of buffer less than 64k

func IsTextproto added in v0.0.18

func IsTextproto(contentType ContentType) bool

IsTextproto reports whether the contentType is a textproto type

func Marshal

func Marshal(m Message) ([]byte, error)

Marshal returns the wire-format encoding of m without type or size.

func Moduleof added in v0.0.18

func Moduleof(typ Type) string

Moduleof returns the module name of message by type

func PeekSize

func PeekSize(peeker Peeker) (n int, size int, err error)

PeekSize reads message size without advancing underlying reader offset

func ReadSize

func ReadSize(r io.ByteReader) (size int, err error)

ReadSize reads message size from reader

func Register

func Register(mod string, typ Type, creator func() Message)

Register registers a message creator by type. Register is not concurrent-safe, it is recommended to call in `init` function.

e.g.

package foo

import "github.com/gopherd/doge/proto"

func init() {
	proto.Register("foo", BarType, func() proto.Message { return new(Bar) })
}

func Unmarshal

func Unmarshal(buf []byte, m Message) error

Unmarshal parses the wire-format message in b and places the result in m. The provided message must be mutable (e.g., a non-nil pointer to a message).

Types

type Arena added in v0.0.18

type Arena interface {
	Get(typ Type) Message
	Put(m Message)
}

Arena represents a message factory

type ArenaFunc added in v0.0.18

type ArenaFunc func(Type) Message

ArenaFunc wraps function as an Arena

func (ArenaFunc) Get added in v0.0.18

func (fn ArenaFunc) Get(typ Type) Message

Get implements Arena Get method

func (ArenaFunc) Put added in v0.0.18

func (fn ArenaFunc) Put(_ Message)

Put implements Arena Put method

type Body

type Body interface {
	io.Reader
	io.ByteReader

	// Len returns remain length of body
	Len() int

	// Peek returns the next n bytes without advancing the reader. The bytes stop
	// being valid at the next read call. If Peek returns fewer than n bytes, it
	// also returns an error explaining why the read is short.
	Peek(n int) ([]byte, error)

	// Discard skips the next n bytes, returning the number of bytes discarded.
	// If Discard skips fewer than n bytes, it also returns an error.
	Discard(n int) (discarded int, err error)
}

Body represents message body

func Text added in v0.0.18

func Text(b []byte) Body

Text creates a textproto body

type Buffer

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

func AllocBuffer

func AllocBuffer() *Buffer

AllocBuffer gets buffer from pool

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

func (*Buffer) Cap

func (b *Buffer) Cap() int

func (*Buffer) Encode

func (b *Buffer) Encode(m Message, contentType ContentType) error

func (*Buffer) Len

func (b *Buffer) Len() int

func (*Buffer) Marshal added in v0.0.18

func (b *Buffer) Marshal(m Message) error

func (*Buffer) Reserve

func (b *Buffer) Reserve(n int)

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) Unmarshal

func (b *Buffer) Unmarshal(m Message) error

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

type ContentType added in v0.0.18

type ContentType int

ContentType represents encoding type of content

const (
	ContentTypeProtobuf ContentType = iota
	ContentTypeText
)

type Message

type Message interface {
	// Type of message
	Typeof() Type
	// Size of message
	Sizeof() int
	// Name of message
	Nameof() string

	// MarshalAppend marshals message to buf
	MarshalAppend(buf []byte, useCachedSize bool) ([]byte, error)
	// Unmarshal unmarshals message from buf
	Unmarshal(buf []byte) error
}

Message represents a message interface

func Decode

func Decode(buf []byte, arena Arena) (int, Message, error)

Decode decodes one message with type and size from buf and returns number of bytes read and unmarshaled message.

func New

func New(typ Type) Message

New creates a message by type, nil returned if type not found

type MessageInfo added in v0.0.18

type MessageInfo struct {
	Type   Type
	Name   string
	Module string
}

func Messages added in v0.0.18

func Messages() []MessageInfo

Messages returns all registered message informations

type Peeker

type Peeker interface {
	Peek(n int) ([]byte, error)
}

Peeker peeks n bytes

type Pool added in v0.0.18

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

Pool implements Arena interface to reuse message objects

func (*Pool) Get added in v0.0.18

func (pp *Pool) Get(typ Type) Message

Get selects an message object from the Pool by type, removes it from the Pool, and returns it to the caller.

func (*Pool) Put added in v0.0.18

func (pp *Pool) Put(m Message)

Put adds x to the pool.

type Type

type Type = uint32

Type represents message type

func Lookup

func Lookup(module string) []Type

Lookup lookups all registered types by module

func ParseType added in v0.0.18

func ParseType(s string) (Type, error)

ParseType parses type from string

func PeekType

func PeekType(peeker Peeker) (n int, typ Type, err error)

PeekType reads message type without advancing underlying reader offset

func ReadType

func ReadType(r io.ByteReader) (typ Type, err error)

ReadType reads message type from reader

type UnrecognizedTypeError added in v0.0.18

type UnrecognizedTypeError struct {
	Type Type
}

func (*UnrecognizedTypeError) Error added in v0.0.18

func (err *UnrecognizedTypeError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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