smux

package module
v0.0.0-...-79be6b8 Latest Latest
Warning

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

Go to latest
Published: May 4, 2018 License: MIT Imports: 8 Imported by: 1

README

smux Build Status

smux is a socket multiplexer. smux multiplexes one connection with a virtual channel called a stream. It behaves like a very simple HTTP/2 binary framing layer, but it reduces protocol overhead.

smux sends and receives multiple requests and responses in parallel using a single connection. Therefore, our application will be fast and simple.

Usage

smux provides simple server and client.

// smux server
server := smux.Server{
	Network: "tcp", // or "unix"
	Address: "localhost:3000", // or "sockfile"
        Handler: smux.HandlerFunc(func(w io.Writer, r io.Reader) {
                io.Copy(ioutil.Discard, r)
		fmt.Fprint(w, "Hello, smux client!")
        }),
}

server.ListenAndServe()
// smux client
client := smux.Client{
	Network: "tcp", // or "unix"
	Address: "localhost:3000", // or "sockfile"
}

body, _ := client.Post([]byte("Hello, smux server!"))
fmt.Printf("%s\n", body) // "Hello, smux client!"

And smux provides raw level interface (stream.Write and Read). You can learn from client and server code.

Performance

Benchmark for HTTP, smux, and similar implementation.

benchmark

Benchmark script is here. It runs on MacBook Pro (15-inch, 2017), CPU 2.8 GHz Intel Core i7, memory 16 GB. Go version is go1.10.2 darwin/amd64.

Currently, xtaci/smux (ssmux) is fast. I am currently speeding up my smux !

License

MIT

Author

monochromegane

Documentation

Index

Constants

View Source
const (
	START_STREAM_ID_OF_CLIENT = 1
	START_STREAM_ID_OF_SERVER = 2
	MAX_STREAM_ID             = 1 << 31 // 2,147,483,648

	NUM_BYTES_HEADER      = 8
	NUM_BYTES_MAX_PAYLOAD = 1 << 14 // 16,384 bytes = 16kb

	TYPE_DATA = 0

	FLAG_DATA_NONE       = 0
	FLAG_DATA_END_STREAM = 1
)

Variables

View Source
var ExceedError = errors.New("Exceeded max stream id")

Functions

This section is empty.

Types

type Client

type Client struct {
	sync.Mutex

	Network string
	Address string
	// contains filtered or unexported fields
}

func (*Client) Post

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

type Conn

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

func Dial

func Dial(network, address string) (Conn, error)

func (*Conn) Accept

func (c *Conn) Accept() (Stream, error)

func (*Conn) Listen

func (c *Conn) Listen()

func (*Conn) Stream

func (c *Conn) Stream() (Stream, error)

type Counter

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

func NewCounter

func NewCounter(init uint32) *Counter

func (*Counter) Get

func (c *Counter) Get() (uint32, error)

type Frame

type Frame []byte

type Handler

type Handler interface {
	Serve(io.Writer, io.Reader)
}

type HandlerFunc

type HandlerFunc func(io.Writer, io.Reader)

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(w io.Writer, r io.Reader)

type Listener

type Listener struct {
	net.Listener
}

func Listen

func Listen(network, address string) (Listener, error)

func (Listener) Accept

func (l Listener) Accept() (Conn, error)

type Server

type Server struct {
	Network string
	Address string
	Handler Handler
}

func (Server) ListenAndServe

func (s Server) ListenAndServe() error

type Stream

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

func NewStream

func NewStream(id uint32, in chan []byte, c *Conn) Stream

func (Stream) Close

func (s Stream) Close() error

func (Stream) Poll

func (s Stream) Poll()

func (Stream) Read

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

func (Stream) Write

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

func (Stream) WriteOnce

func (s Stream) WriteOnce(b []byte) (int, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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