packet

package module
v0.0.0-...-f1cd0c2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

packet

Packet encoding/decoding without writing specialized code, uses struct field tag to help with processing:

  • length for length of the field, expecting a number with following specifiers
    • b for bits
    • B for bytes
  • lengthfor indicate the length for the attribute can be return from the object, which needs to provide the LengthFor interface
  • lengthtotal indicate the attribute value is for the whole message stucture

When an interface{} field is encounted, Unmarshal will check to see if the struct satisfies the InstanceFor interface, and call the InstanceFor(fieldname string) function to get a instance object for the field.

see fixture, and unittest for example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal encode object into binary bytes

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal parson the packet data and stores the result in value pointed by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

Types

type InstanceFor

type InstanceFor interface {
	InstanceFor(fieldname string) interface{}
}

InstanceFor interface helps the unmarshaller to figure out the right type base on message data, by returning the object reference for the attribute in question

type LengthFor

type LengthFor interface {
	LengthFor(fieldname string) uint64
}

LengthFor interface helps the unmarshaller to figure out the right length for bytes

type MarshalPACKET

type MarshalPACKET interface {
	MarshalPACKET() ([]byte, error)
}

MarshalPACKET allows custom marshaller to return the right bytes

type UnmarshalBitfieldOverflowError

type UnmarshalBitfieldOverflowError struct {
	Struct string
	Field  reflect.StructField
}

An UnmarshalBitfieldOverflowError describes a condition where bit reading overflows uint64 holder

func (*UnmarshalBitfieldOverflowError) Error

type UnmarshalPACKET

type UnmarshalPACKET interface {
	UnmarshalPACKET(b []byte) error
}

UnmarshalPACKET interface for custome unmarshaller

type UnmarshalPtrError

type UnmarshalPtrError struct {
	Type reflect.Type
}

UnmarshalPtrError error from expected pointer not found

func (*UnmarshalPtrError) Error

func (e *UnmarshalPtrError) Error() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value  string       // description of packet value - "bool", "array", "number -5"
	Type   reflect.Type // type of Go value it could not be assigned to
	Offset int64        // error occurred after reading Offset bytes
	Struct string       // name of the struct type containing the field
	Field  string       // name of the field holding the Go value
}

An UnmarshalTypeError describes a packet value that was not appropriate for a value of a specific Go type.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

type UnmarshalUnexpectedEnd

type UnmarshalUnexpectedEnd struct {
	Struct string
	Field  string
	Offset int64
	End    int64
}

UnmarshalUnexpectedEnd unexpected end of data

func (*UnmarshalUnexpectedEnd) Error

func (e *UnmarshalUnexpectedEnd) Error() string

Directories

Path Synopsis
Package fixture serve as unittest sample for basic packet encode/decode
Package fixture serve as unittest sample for basic packet encode/decode
bgp

Jump to

Keyboard shortcuts

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