codec

package
v0.0.0-...-6c1d445 Latest Latest
Warning

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

Go to latest
Published: May 3, 2017 License: GPL-3.0 Imports: 6 Imported by: 8

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

func Wrap

Wrap decodes every packet that passes through it and logs it

Types

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

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

Header is the wire representation of a packet header

type Packet

type Packet struct {
	Stream bool
	EndErr bool
	Type   PacketType
	Req    int32
	Body   []byte
}

Packet is the decoded high-level representation

func (Packet) String

func (p Packet) String() string

type PacketType

type PacketType uint

PacketType are the 2 bits of type in the packet header

const (
	Buffer PacketType = iota
	String
	JSON
)

Enumeration of the possible body types of a packet

func (PacketType) Flag

func (pt PacketType) Flag() Flag

func (PacketType) String

func (i PacketType) String() string

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 writer, and reads as many bytes as specified in it TODO: pass in packet pointer as arg to reduce allocations

type Writer

type Writer struct {
	// 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

Jump to

Keyboard shortcuts

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