codec

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2020 License: GPL-3.0 Imports: 12 Imported by: 1

Documentation

Overview

codec package for encoding/decoding messages over the wire this exists so that we use networking through easy-to-use interfaces, and that when this file changes

Index

Constants

View Source
const DefaultMaxSize = 1024

DefaultMaxSize is what new codecs have

Variables

View Source
var Debug = os.Getenv("DEBUG") != ""

Debug ...

View Source
var ErrConn = errors.New("nil conn")

ErrConn no connection

View Source
var ErrShort = errors.New("short error")

ErrShort short or small

View Source
var ErrSizeBig = errors.New("big error")

ErrSizebig too big

View Source
var ErrZeroType = errors.New("zero type")
View Source
var LogTypeStringer func(b byte) string

Functions

func Decode

func Decode(buf []byte, vptr interface{}) (err error)

Decode an object using gob

func Encode

func Encode(buf *bytes.Buffer, b []byte, vptr interface{}) (n int, err error)

Encode an object using gob

Types

type Codec

type Codec struct {
	Version byte
	MaxSize int
	// contains filtered or unexported fields
}

Codec to encode/decode network packets uniformly across clients and servers One codec per connection One codec should be writing to a single connection at a time One codec should be reading from a single connection at a time This should be easy enough to work with

func NewCodec

func NewCodec(endian binary.ByteOrder, conn io.ReadWriteCloser) *Codec

NewCodec starts everything.. If server, conn can be nil, in which case we use New() for each conn

func (*Codec) Close

func (c *Codec) Close() error

Close closes the network connection. If there is no conn, ErrConn will be returned.

func (*Codec) Decode

func (c *Codec) Decode(b []byte, vptr interface{}) (err error)

Decode vptr from b

func (*Codec) DecodeFloat

func (c *Codec) DecodeFloat(b []byte) float64

func (*Codec) DecodeUint64

func (c *Codec) DecodeUint64(b []byte) uint64

func (*Codec) EncodeFloat

func (c *Codec) EncodeFloat(i float64) []byte

func (*Codec) EncodeUint64

func (c *Codec) EncodeUint64(i uint64) []byte

func (*Codec) New

func (c *Codec) New(conn net.Conn) *Codec

New copies a codec but with a new connection. Server uses this for each new connection.

func (*Codec) Read

func (c *Codec) Read(b []byte) (typ types.Type, reqid uint64, n int, err error)

Read skips 2 byte and returns as typ, with request id req, and how many bytes to read (n) or an error.

func (*Codec) Write

func (c *Codec) Write(v Typer) (n int, err error)

type Encodable

type Encodable interface {
	Encode(b []byte) (int, error) // should return an error if buffer is too small
	//Decode(b []byte) error        // should return first error encountered along the way
	Type() types.Type // returns 0-255, sent as prefix
	EncodeTo(w io.Writer) (int, error)
}

Encodable is an easy to implement interface that can be sent over the wire

type Typer

type Typer interface {
	Type() types.Type
}

Jump to

Keyboard shortcuts

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