proto

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: MIT Imports: 6 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")
	ErrUnrecognizedType = errors.New("proto: unrecognized type")
)

Functions

func Encode

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

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

          |<-- body -->|
	|body.len|type|content|

func EncodeAppend

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

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 FreeBuffer

func FreeBuffer(b *Buffer)

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

func Marshal

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

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

func Nameof

func Nameof(m Message) string

Nameof returns the name of message

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(module 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 Sizeof

func Sizeof(m Message) int

Sizeof calculates the exact size of marshaled bytes

func Unmarshal

func Unmarshal(b []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 Body

type Body interface {
	io.ByteReader
	io.Reader

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

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

func (*Buffer) Len

func (b *Buffer) Len() int

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 Message

type Message interface {
	proto.Message

	// Type returns the message type
	Type() Type
}

Message represents a message interface

func Decode

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

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

func DecodeBody

func DecodeBody(b []byte) (Message, error)

Decode decodes one message that contains type from buf and returns unmarshaled message.

func New

func New(typ Type) Message

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

type Peeker

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

Peeker peeks n bytes

type Type

type Type = uint32

Type represents message type

func Lookup

func Lookup(module string) []Type

Lookup lookups all registered types by module

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

Jump to

Keyboard shortcuts

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