framestream

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 8 Imported by: 2

README

Go version

go-framestream

Frame Streams implementation in Go with compression support (gzip, lz4, snappy and zstd).

Installation

go get -u github.com/dmachard/go-framestream

Usage example

Example to use the framestream library with Net.Pipe

client, server := net.Pipe()
handshake := true

// init framestream sender
go func() {
    fs_server := NewFstrm(bufio.NewReader(server), bufio.NewWriter(server), server, 5*time.Second, []byte("frstrm"), handshake)
    if err := fs_server.InitSender(); err != nil {
        t.Errorf("error to init framestream sender: %s", err)
    }

    // send frame
    frame := &Frame{}
    if err := frame.Write([]byte{1, 2, 3, 4}); err != nil {
        t.Errorf("error to init frame: %s", err)
    }
    if err := fs_server.SendFrame(frame); err != nil {
        t.Errorf("error to send frame: %s", err)
    }
}()

// init framestream receiver
fs_client := NewFstrm(bufio.NewReader(client), bufio.NewWriter(client), client, 5*time.Second, []byte("frstrm"), handshake)
if err := fs_client.InitReceiver(); err != nil {
    t.Errorf("error to init framestream receiver: %s", err)
}

// receive frame, timeout 5s
_, err := fs_client.RecvFrame(true)
if err != nil {
    t.Errorf("error to receive frame: %s", err)
}

Usage example with compression

if err := fs_server.SendCompressedFrame(&compress.GzipCodec, frame); err != nil {
    t.Errorf("error to send frame: %s", err)
}
...
// receive frame, timeout 5s
frame, err := fs_client.RecvCompressedFrame(&compress.GzipCodec, true)
if err != nil {
    t.Errorf("error to receive frame: %s", err)
}

Testing

$ go test -v
=== RUN   TestControlEncode
--- PASS: TestControlEncode (0.00s)
=== RUN   TestControlDecode
--- PASS: TestControlDecode (0.00s)
=== RUN   TestControlDecodeError
--- PASS: TestControlDecodeError (0.00s)
=== RUN   TestFrameWrite
--- PASS: TestFrameWrite (0.00s)
=== RUN   TestFramestreamHandshake
--- PASS: TestFramestreamHandshake (0.00s)
=== RUN   TestFramestreamData
--- PASS: TestFramestreamData (0.00s)
PASS
ok      github.com/dmachard/go-framestream
Frame format

Data Frame

Headers Bytes
Data length 4 bytes
Payload xx bytes

Control Frame

Headers Bytes
Control frame length 4 bytes
Control frame type 4 bytes
Control frame content type 4 bytes (optional)
Control frame content type length 4 bytes (optional)
Content type payload xx bytes

Documentation

Index

Constants

View Source
const CONTROL_ACCEPT = 0x01
View Source
const CONTROL_FIELD_CONTENT_TYPE = 0x01
View Source
const CONTROL_FINISH = 0x05
View Source
const CONTROL_FRAME_LENGTH_MAX = 4064
View Source
const CONTROL_READY = 0x04
View Source
const CONTROL_START = 0x02
View Source
const CONTROL_STOP = 0x03
View Source
const DATA_FRAME_LENGTH_MAX = 65536

Variables

View Source
var ErrControlFrameContentTypeUnsupported = errors.New("control frame with unsupported content type")
View Source
var ErrControlFrameExpected = errors.New("control frame expected")
View Source
var ErrControlFrameMalformed = errors.New("control frame malformed")
View Source
var ErrControlFrameTooLarge = errors.New("control frame too large error")
View Source
var ErrControlFrameUnexpected = errors.New("control frame unexpected")
View Source
var ErrControlFrameUnsupported = errors.New("control frame unsupported")
View Source
var ErrFrameTooLarge = errors.New("frame too large error")
View Source
var ErrReaderNotReady = errors.New("reader not ready")

Functions

This section is empty.

Types

type ControlFrame

type ControlFrame struct {
	// contains filtered or unexported fields
}
Control Frame struct

|------------------------------------|----------------------| | Control frame length | 4 bytes | |------------------------------------|----------------------| | Control frame type | 4 bytes | |------------------------------------|----------------------| | Control frame content type | 4 bytes (optional) | |------------------------------------|----------------------| | Control frame content type length | 4 bytes (optional) | |------------------------------------|----------------------| | Content type payload | xx bytes | |------------------------------------|----------------------|

func (*ControlFrame) CheckContentType

func (ctrl *ControlFrame) CheckContentType(ctype []byte) bool

func (*ControlFrame) Decode

func (ctrl *ControlFrame) Decode() error

func (*ControlFrame) Encode

func (ctrl *ControlFrame) Encode() error

type Frame

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

|------------------------------------|----------------------| | Data length | 4 bytes | |------------------------------------|----------------------| | Payload | xx bytes | |------------------------------------|----------------------|

If the data length is equal to zero then it's a control frame otherwise we have a data frame.

func (*Frame) AppendData added in v0.9.0

func (frame *Frame) AppendData(payload []byte) error

func (Frame) Data

func (frame Frame) Data() []byte

func (*Frame) Encode added in v0.10.0

func (frame *Frame) Encode() error

func (Frame) IsControl added in v0.7.0

func (frame Frame) IsControl() bool

func (Frame) Len

func (frame Frame) Len() int

func (*Frame) Write

func (frame *Frame) Write(payload []byte) error

type Fstrm

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

Framestream

func NewFstrm

func NewFstrm(reader *bufio.Reader, writer *bufio.Writer, conn net.Conn, readtimeout time.Duration, ctype []byte, handshake bool) *Fstrm

func (Fstrm) InitReceiver

func (fs Fstrm) InitReceiver() error

func (Fstrm) InitSender

func (fs Fstrm) InitSender() error

func (Fstrm) ProcessFrame

func (fs Fstrm) ProcessFrame(ch chan []byte) error

func (Fstrm) RecvCompressedFrame added in v0.8.0

func (fs Fstrm) RecvCompressedFrame(codec compress.Codec, timeout bool) (*Frame, error)

func (Fstrm) RecvControl

func (fs Fstrm) RecvControl() (*ControlFrame, error)

func (Fstrm) RecvFrame

func (fs Fstrm) RecvFrame(timeout bool) (*Frame, error)

func (Fstrm) ResetReceiver

func (fs Fstrm) ResetReceiver(frame *Frame) error

func (Fstrm) ResetSender

func (fs Fstrm) ResetSender() error

func (Fstrm) SendCompressedFrame added in v0.8.0

func (fs Fstrm) SendCompressedFrame(codec compress.Codec, frame *Frame) (err error)

func (Fstrm) SendControl

func (fs Fstrm) SendControl(control *ControlFrame) (err error)

func (Fstrm) SendFrame

func (fs Fstrm) SendFrame(frame *Frame) (err error)

Jump to

Keyboard shortcuts

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