Documentation

Overview

Package frame provides a framed reader and writer.

Message frames are prefixed with a 4-byte big-endian encoded integer holding the length of the rest of the frame.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client provides bidirectional outgoing framed communication.

It allows sending framed requests where each request has a corresponding response. Only one active request is allowed at a time. Other requests are blocked while a request is ongoing.

func NewClient

func NewClient(w io.Writer, r io.Reader) *Client

NewClient builds a new Client which uses the given writer to send requests and the given reader to read their responses.

func (*Client) Send

func (c *Client) Send(b []byte) ([]byte, error)

Send sends the given frame and returns its response.

type Handler

type Handler interface {
	// Receives the given framed request and responds to it.
	Handle([]byte) ([]byte, error)
}

Handler handles incoming framed requests.

type Reader

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

Reader is a reader for framed messages.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader builds a new Reader which reads frames from the given io.Reader.

If the io.Reader is a ReadCloser, its Close method will be called when the frame.Reader is closed.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the given Reader.

func (*Reader) Read

func (r *Reader) Read() ([]byte, error)

Read reads the next frame from the Reader.

type Server

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

Server provides bidirectional incoming framed communication.

It allows receiving framed requests and responding to them.

func NewServer

func NewServer(r io.Reader, w io.Writer) *Server

NewServer builds a new server which reads requests from the given Reader and writes responses to the given Writer.

func (*Server) Serve

func (s *Server) Serve(h Handler) (err error)

Serve serves the given Handler with the Server.

Only one request is served at a time. The server stops handling requests if there is an IO error or an unhandled error is received from the Handler.

This blocks until the server is stopped using Stop.

func (*Server) Stop

func (s *Server) Stop() error

Stop tells the Server that it's okay to stop Serve.

This is a no-op if the server wasn't already running.

type Writer

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

Writer is a writer for framed messages.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter builds a new Writer which writes frames to the given io.Writer.

If the io.Writer is a WriteCloser, its Close method will be called when the frame.Writer is closed.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the given Writeer.

func (*Writer) Write

func (w *Writer) Write(b []byte) error

Write writes the given frame to the Writer.