codec

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package codec implements readers and writers for https://github.com/dominictarr/packet-stream-codec

Packet structure:

(
	[flags (1byte), length (4 bytes, UInt32BE), req (4 bytes, Int32BE)] # Header
	[body (length bytes)]
) *
[zeros (9 bytes)]

Flags:

[ignored (4 bits), stream (1 bit), end/err (1 bit), type (2 bits)]
type = {0 => Buffer, 1 => String, 2 => JSON} # PacketType

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body []byte

func (Body) String

func (b Body) String() string

type Flag

type Flag byte

Flag is the first byte of the Header

const (
	FlagString Flag = 1 << iota // type
	FlagJSON                    // bits
	FlagEndErr
	FlagStream
)

Flag bitmasks

func (Flag) Clear

func (f Flag) Clear(g Flag) Flag

func (Flag) Get

func (f Flag) Get(g Flag) bool

func (Flag) Set

func (f Flag) Set(g Flag) Flag

func (Flag) String

func (f Flag) String() string
type Header struct {
	Flag Flag
	Len  uint32
	Req  int32
}

Header is the wire representation of a packet header

type Packet

type Packet struct {
	Flag Flag
	Req  int32
	Body Body
}

Packet is the decoded high-level representation

type Reader

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

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) ReadPacket

func (r *Reader) ReadPacket() (*Packet, error)

ReadPacket decodes the header from the underlying reader, and reads as many bytes as specified in it TODO: pass in packet pointer as arg to reduce allocations

type Writer

type Writer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new packet-stream writer

func (*Writer) Close

func (w *Writer) Close() error

Close sends 9 zero bytes and also closes it's underlying writer if it is also an io.Closer

func (*Writer) WritePacket

func (w *Writer) WritePacket(r *Packet) error

WritePacket creates an header for the Packet and writes it and the body to the underlying writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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