framing

package
v0.9.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2016 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package framing provides a prefix length framed net.Conn connection.

It implements a wrapper around net.Conn that frames each packet with a size prefix of length 1, 2 or 4 bytes of either little or big endianess.

Original author: Adam Lindberg

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPrefixLength is returned when an invalid prefix length is given.
	ErrPrefixLength = errors.New("Invalid frame prefix length")
	// ErrFrameTooLarge is returned from Write(b []byte) when b is larger than
	// MaxFrameSize.
	ErrFrameTooLarge = errors.New("Frame too large for buffer size")
)

Errors returned by framed connections.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// The inner net.Conn
	net.Conn
	// The frame size: 1, 2 or 4.
	PrefixLength byte
	// The endianess of the length prefix.
	Endianess binary.ByteOrder
	// The maximum size of a frame for the current prefix size.
	MaxFrameSize uint
	// contains filtered or unexported fields
}

Conn wraps a net.Conn making it aware about frames and their size.

func NewConn

func NewConn(inner net.Conn, prefixLength byte, endianess binary.ByteOrder) (conn *Conn, err error)

NewConn returns a framing.Conn that acts like a net.Conn. prefixLength should be one of 1, 2 or 4. endianess should be one of binary.BigEndian or binary.LittleEndian.

The difference to a normal net.Conn is that Write(b []byte) prefixes each packet with a size and Read(b []byte) reads that size and waits for that many bytes.

A complementary ReadFrame() function can be used to read and wait for a full frame.

func (*Conn) Read

func (f *Conn) Read(b []byte) (n int, err error)

Read implements net.Conn.Read(b []byte)

func (*Conn) ReadFrame

func (f *Conn) ReadFrame() (frame []byte, err error)

ReadFrame returns the next full frame in the stream.

func (*Conn) Write

func (f *Conn) Write(b []byte) (n int, err error)

Write implements net.Conn.Write(b []byte)

Jump to

Keyboard shortcuts

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