Documentation ¶
Overview ¶
Package smux is a multiplexing library for Golang.
It relies on an underlying connection to provide reliability and ordering, such as TCP or KCP, and provides stream-oriented multiplexing over a single channel.
Index ¶
- Constants
- Variables
- func IsSmux(conn net.Conn, timeout time.Duration) bool
- func Listen(address string, callback func(session *MuxSession)) error
- func NewIDBitmap() *idBitmap
- func VerifyConfig(config *Config) error
- type AIOService
- type Allocator
- type Buffer
- type Config
- type Event
- type Frame
- type MuxConn
- func (this *MuxConn) Close() error
- func (this *MuxConn) ID() uint16
- func (this *MuxConn) LocalAddr() net.Addr
- func (this *MuxConn) Read(b []byte) (n int, err error)
- func (this *MuxConn) Readable() (int, bool)
- func (this *MuxConn) RemoteAddr() net.Addr
- func (this *MuxConn) SetDeadline(t time.Time) error
- func (this *MuxConn) SetNonblock(nonblocking bool)
- func (this *MuxConn) SetReadDeadline(t time.Time) error
- func (this *MuxConn) SetWriteDeadline(t time.Time) error
- func (this *MuxConn) Writable() (int, bool)
- func (this *MuxConn) Write(b []byte) (n int, err error)
- type MuxSession
- func (this *MuxSession) Accept() (*MuxConn, error)
- func (this *MuxSession) Addr() net.Addr
- func (this *MuxSession) Close()
- func (this *MuxSession) GetMuxConn(connID uint16) *MuxConn
- func (this *MuxSession) IsClosed() bool
- func (this *MuxSession) NumMuxConn() int
- func (this *MuxSession) Open() (*MuxConn, error)
- type Session
- func (s *Session) Accept() (io.ReadWriteCloser, error)
- func (s *Session) AcceptStream() (*Stream, error)
- func (s *Session) CheckRTT(sendTimeout time.Duration) uint32
- func (s *Session) Close() error
- func (s *Session) GetCustomFrame() (*Frame, error)
- func (s *Session) GetRTT() time.Duration
- func (s *Session) GetRTTCh() <-chan uint32
- func (s *Session) IsClosed() bool
- func (s *Session) LocalAddr() net.Addr
- func (s *Session) NumStreams() int
- func (s *Session) Open() (io.ReadWriteCloser, error)
- func (s *Session) OpenStream() (*Stream, error)
- func (s *Session) RemoteAddr() net.Addr
- func (s *Session) SetDeadline(t time.Time) error
- func (s *Session) WriteCustomFrame(cmd byte, sid uint32, data []byte) (n int, err error)
- func (s *Session) WriteCustomFrameTimeout(cmd byte, sid uint32, data []byte, deadline <-chan time.Time) (n int, err error)
- type Stream
- func (s *Stream) Close() error
- func (s *Stream) GetDieCh() <-chan struct{}
- func (s *Stream) ID() uint32
- func (s *Stream) LocalAddr() net.Addr
- func (s *Stream) Read(b []byte) (n int, err error)
- func (s *Stream) RemoteAddr() net.Addr
- func (s *Stream) SetDeadline(t time.Time) error
- func (s *Stream) SetReadDeadline(t time.Time) error
- func (s *Stream) SetWriteDeadline(t time.Time) error
- func (s *Stream) Write(b []byte) (n int, err error)
- func (s *Stream) WriteTo(w io.Writer) (n int64, err error)
- Bugs
Constants ¶
const (
CmdMax byte // for custom ID start
)
Variables ¶
var ( ErrInvalidProtocol = errors.New("invalid protocol") ErrConsumed = errors.New("peer consumed more than sent") ErrGoAway = errors.New("stream id overflows, should start a new connection") ErrTimeout = errors.New("timeout") ErrWouldBlock = errors.New("operation would block on IO") )
var ( ErrTimeout = errors.New("timeout. ") ErrClosedPipe = errors.New("the stream has closed. ") ErrBrokenPipe = errors.New("write on closed stream. ") )
var ErrNonblock = errors.New("nonblock none. ")
只有在非堵塞模式下才有的返回错误 读情况下,没有数据可读;写情况下,不能写且已发送为0
var (
ErrServiceClosed = errors.New("aioService is closed. ")
)
Functions ¶
func Listen ¶
func Listen(address string, callback func(session *MuxSession)) error
func NewIDBitmap ¶
func NewIDBitmap() *idBitmap
func VerifyConfig ¶
VerifyConfig is used to verify the sanity of configuration
Types ¶
type AIOService ¶
type AIOService struct {
// contains filtered or unexported fields
}
func OpenAIOService ¶
func OpenAIOService(mux *MuxSession, worker int) *AIOService
func (*AIOService) Close ¶
func (this *AIOService) Close()
func (*AIOService) Unwatch ¶
func (this *AIOService) Unwatch(fd uint16)
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%.
type Config ¶
type Config struct { // SMUX Protocol version, support 1,2 Version int // Disabled keepalive KeepAliveDisabled bool // Disabled keepalive KeepAliveCloseDisabled 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 // v2 can smaller than keep-alive interval // and will closed if RTT test > KeepAliveTimeout 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
func DefaultConfigV1 ¶
func DefaultConfigV1() *Config
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame defines a packet from or to be multiplexed into a single connection
type MuxConn ¶
type MuxConn struct {
// contains filtered or unexported fields
}
func (*MuxConn) RemoteAddr ¶
RemoteAddr satisfies net.Conn interface
func (*MuxConn) SetDeadline ¶
SetDeadline sets both read and write deadlines as defined by net.Conn.SetDeadline. A zero time value disables the deadlines.
func (*MuxConn) SetNonblock ¶
func (*MuxConn) SetReadDeadline ¶
SetReadDeadline sets the read deadline as defined by net.Conn.SetReadDeadline. A zero time value disables the deadline.
func (*MuxConn) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline as defined by net.Conn.SetWriteDeadline. A zero time value disables the deadline.
type MuxSession ¶
type MuxSession struct {
// contains filtered or unexported fields
}
func NewMuxSession ¶
func NewMuxSession(conn net.Conn) *MuxSession
func (*MuxSession) Accept ¶
func (this *MuxSession) Accept() (*MuxConn, error)
func (*MuxSession) Addr ¶
func (this *MuxSession) Addr() net.Addr
func (*MuxSession) Close ¶
func (this *MuxSession) Close()
func (*MuxSession) GetMuxConn ¶
func (this *MuxSession) GetMuxConn(connID uint16) *MuxConn
func (*MuxSession) IsClosed ¶
func (this *MuxSession) IsClosed() bool
IsClosed does a safe check to see if we have shutdown
func (*MuxSession) NumMuxConn ¶
func (this *MuxSession) NumMuxConn() int
func (*MuxSession) Open ¶
func (this *MuxSession) Open() (*MuxConn, error)
type Session ¶
type Session struct {
// 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 ¶
AcceptStream is used to block until the next available stream is ready to be accepted.
func (*Session) GetCustomFrame ¶
GetCustomFrame is used to block until the next available Custom Frame exist TODO: put buffer back
func (*Session) NumStreams ¶
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 ¶
OpenStream is used to create a new stream
func (*Session) RemoteAddr ¶
RemoteAddr satisfies net.Conn interface
func (*Session) SetDeadline ¶
SetDeadline sets a deadline used by Accept* calls. A zero time value disables the deadline.
func (*Session) WriteCustomFrame ¶
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream 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) RemoteAddr ¶
RemoteAddr satisfies net.Conn interface
func (*Stream) SetDeadline ¶
SetDeadline sets both read and write deadlines as defined by net.Conn.SetDeadline. A zero time value disables the deadlines.
func (*Stream) SetReadDeadline ¶
SetReadDeadline sets the read deadline as defined by net.Conn.SetReadDeadline. A zero time value disables the deadline.
func (*Stream) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline as defined by net.Conn.SetWriteDeadline. A zero time value disables the deadline.