gfsmux

package module
v0.0.0-...-5a8b407 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 10 Imported by: 2

README

gfsmux

GRC Codacy Badge Maintainability


gfsmux: Efficient stream multiplexing for Go


Availability


License


Documentation

Overview

Package gfsmux is a multiplexing library for Golang.

It relies on an underlying connection to provide reliability and ordering, such as GFCP, and provides stream-oriented multiplexing over a single channel.

Index

Constants

View Source
const (

	// CmdSyn ... stream open
	CmdSyn byte = iota
	// CmdFin ... stream close, EOF
	CmdFin
	// CmdPsh ... push data
	CmdPsh
	// CmdNop ... noop
	CmdNop

	// CmdUpd ... notify bytes consumed by remote peer-end
	CmdUpd
)
View Source
const (

	// HeaderSize ...
	HeaderSize = sizeOfVer + sizeOfCmd + sizeOfSid + sizeOfLength
)

Variables

View Source
var (
	// ErrInvalidProtocol version or bag negotiation.
	ErrInvalidProtocol = errors.New(
		"invalid protocol",
	)
	// ErrConsumed protocol error, indicates desync
	ErrConsumed = errors.New(
		"peer consumed more than sent",
	)
	// ErrGoAway overflow condition, restart it all.
	ErrGoAway = errors.New(
		"stream id overflows, should start a new Connection",
	)
	// ErrTimeout ...
	ErrTimeout = &timeoutError{}
	// ErrWouldBlock error for invalid blocking I/O operating
	ErrWouldBlock = errors.New(
		"operation would block on IO",
	)
)

Functions

func Smsb

func Smsb(
	size int,
) byte

Smsb returns the position of most significiant bit http://supertech.csail.mit.edu/papers/debruijn.pdf

func VerifyConfig

func VerifyConfig(
	Config *Config,
) error

VerifyConfig is used to verify the sanity of Configuration

Types

type Allocator

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

Allocator for incoming frames, optimized to prevent overwriting after zeroing

func NewAllocator

func NewAllocator() *Allocator

NewAllocator initiates a []byte allocator for frames less than 65536 bytes, the waste(memory fragmentation) of space allocation is guaranteed to be no more than 50%.

func (*Allocator) Get

func (
	alloc *Allocator,
) Get(
	size int,
) []byte

Get a []byte from pool with most appropriate cap

func (*Allocator) Put

func (
	alloc *Allocator,
) Put(
	buf []byte,
) error

Put returns a []byte to pool for future use, which the cap must be exactly 2^n

type Config

type Config struct {
	// SMUX Protocol version, support 1,2
	Version int

	// Disabled keepalive
	KeepAliveDisabled bool

	// KeepAliveInterval is how often to send a NOP command to the remote
	KeepAliveInterval time.Duration

	// KeepAliveTimeout is how long the session
	// will be closed if no data has arrived
	KeepAliveTimeout time.Duration

	// MaxFrameSize is used to control the maximum
	// frame size to sent to the remote
	MaxFrameSize int

	// MaxReceiveBuffer is used to control the maximum
	// number of data in the buffer pool
	MaxReceiveBuffer int

	// MaxStreamBuffer is used to control the maximum
	// number of data per stream
	MaxStreamBuffer int
}

Config is used to tune the Smux session

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig is used to return a default Configuration

type Frame

type Frame struct {
	Ver  byte
	Cmd  byte
	Sid  uint32
	Data []byte
}

Frame defines a packet from or to be multiplexed into a single Connection

func NewFrame

func NewFrame(
	Version,
	Cmd byte,
	Sid uint32,
) Frame

NewFrame ...

type Session

type Session struct {
	Conn io.ReadWriteCloser

	Config *Config
	// contains filtered or unexported fields
}

Session defines a multiplexed Connection for streams

func Client

func Client(
	Conn io.ReadWriteCloser,
	Config *Config,
) (
	*Session,
	error,
)

Client is used to initialize a new client-side Connection.

func Server

func Server(
	Conn io.ReadWriteCloser,
	Config *Config,
) (
	*Session,
	error,
)

Server is used to initialize a new server-side Connection.

func (*Session) Accept

func (
	s *Session,
) Accept() (
	io.ReadWriteCloser,
	error,
)

Accept Returns a generic ReadWriteCloser instead of smux.Stream

func (*Session) AcceptStream

func (
	s *Session,
) AcceptStream() (
	*Stream,
	error,
)

AcceptStream is used to block until the next available stream is ready to be accepted.

func (*Session) Close

func (
	s *Session,
) Close() error

Close is used to close the session and all streams.

func (*Session) IsClosed

func (
	s *Session,
) IsClosed() bool

IsClosed does a safe check to see if we have shutdown

func (*Session) LocalAddr

func (
	s *Session,
) LocalAddr() net.Addr

LocalAddr satisfies net.Conn interface

func (*Session) NumStreams

func (
	s *Session,
) NumStreams() int

NumStreams returns the number of currently open streams

func (*Session) Open

func (
	s *Session,
) Open() (
	io.ReadWriteCloser,
	error,
)

Open returns a generic ReadWriteCloser

func (*Session) OpenStream

func (
	s *Session,
) OpenStream() (
	*Stream,
	error,
)

OpenStream is used to create a new stream

func (*Session) RemoteAddr

func (
	s *Session,
) RemoteAddr() net.Addr

RemoteAddr satisfies net.Conn interface

func (*Session) SetDeadline

func (
	s *Session,
) SetDeadline(
	t time.Time,
) error

SetDeadline sets a deadline used by Accept* calls. A zero time value disables the deadline.

func (*Session) WriteFrame

func (
	s *Session,
) WriteFrame(
	f Frame,
) (
	n int,
	err error,
)

WriteFrame writes the frame to the underlying Connection and returns the number of bytes written if successful

func (*Session) WriteFrameInternal

func (
	s *Session,
) WriteFrameInternal(
	f Frame,
	deadline <-chan time.Time,
	Prio uint64,
) (
	int,
	error,
)

WriteFrameInternal is to support deadline used in keepalive

type ShaperHeap

type ShaperHeap []WriteRequest

ShaperHeap ...

func (ShaperHeap) Len

func (
	h ShaperHeap,
) Len() int

Len ...

func (ShaperHeap) Less

func (
	h ShaperHeap,
) Less(
	i,
	j int,
) bool

Less ...

func (*ShaperHeap) Pop

func (
	h *ShaperHeap,
) Pop() interface{}

Pop ...

func (*ShaperHeap) Push

func (
	h *ShaperHeap,
) Push(
	x interface{},
)

Push ...

func (ShaperHeap) Swap

func (
	h ShaperHeap,
) Swap(
	i,
	j int,
)

Swap ...

type Stream

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

Stream implements net.Conn

func (*Stream) Close

func (
	s *Stream,
) Close() error

Close implements net.Conn

func (*Stream) GetDieCh

func (
	s *Stream,
) GetDieCh() <-chan struct{}

GetDieCh returns a readonly chan which can be readable when the stream is to be closed.

func (*Stream) ID

func (
	s *Stream,
) ID() uint32

ID returns the unique stream ID.

func (*Stream) LocalAddr

func (
	s *Stream,
) LocalAddr() net.Addr

LocalAddr satisfies net.Conn interface

func (*Stream) Read

func (
	s *Stream,
) Read(
	b []byte,
) (
	n int,
	err error,
)

Read implements net.Conn

func (*Stream) RemoteAddr

func (s *Stream) RemoteAddr() net.Addr

RemoteAddr satisfies net.Conn interface

func (*Stream) SetDeadline

func (
	s *Stream,
) SetDeadline(
	t time.Time,
) error

SetDeadline sets both read and write deadlines as defined by net.Conn.SetDeadline. A zero time value disables the deadlines.

func (*Stream) SetReadDeadline

func (
	s *Stream,
) SetReadDeadline(
	t time.Time,
) error

SetReadDeadline sets the read deadline as defined by net.Conn.SetReadDeadline. A zero time value disables the deadline.

func (*Stream) SetWriteDeadline

func (
	s *Stream,
) SetWriteDeadline(
	t time.Time,
) error

SetWriteDeadline sets the write deadline as defined by net.Conn.SetWriteDeadline. A zero time value disables the deadline.

func (*Stream) Write

func (
	s *Stream,
) Write(
	b []byte,
) (
	n int,
	err error,
)

Write implements net.Conn

Behavior when multiple concurrent goroutines write is not deterministic, so the frames will interleave in random ways.

func (*Stream) WriteTo

func (
	s *Stream,
) WriteTo(
	w io.Writer,
) (
	n int64,
	err error,
)

WriteTo implements io.WriteTo

type WriteRequest

type WriteRequest struct {
	Prio uint64
	// contains filtered or unexported fields
}

WriteRequest ...

Notes

Jump to

Keyboard shortcuts

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