• Stars
    star
    1,320
  • Rank 35,625 (Top 0.8 %)
  • Language
    Go
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Stream Multiplexing Library for golang with least memory usage(TDMA)

smux

GoDoc MIT licensed Build Status Go Report Card Coverage Statusd Sourcegraph

smux

Introduction

Smux ( Simple MUltipleXing) 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. The original intention of this library is to power the connection management for kcp-go.

Features

  1. Token bucket controlled receiving, which provides smoother bandwidth graph(see picture below).
  2. Session-wide receive buffer, shared among streams, fully controlled overall memory usage.
  3. Minimized header(8Bytes), maximized payload.
  4. Well-tested on millions of devices in kcptun.
  5. Builtin fair queue traffic shaping.
  6. Per-stream sliding window to control congestion.(protocol version 2+).

smooth bandwidth curve

Documentation

For complete documentation, see the associated Godoc.

Benchmark

$ go test -v -run=^$ -bench .
goos: darwin
goarch: amd64
pkg: github.com/xtaci/smux
BenchmarkMSB-4           	30000000	        51.8 ns/op
BenchmarkAcceptClose-4   	   50000	     36783 ns/op
BenchmarkConnSmux-4      	   30000	     58335 ns/op	2246.88 MB/s	    1208 B/op	      19 allocs/op
BenchmarkConnTCP-4       	   50000	     25579 ns/op	5124.04 MB/s	       0 B/op	       0 allocs/op
PASS
ok  	github.com/xtaci/smux	7.811s

Specification

VERSION(1B) | CMD(1B) | LENGTH(2B) | STREAMID(4B) | DATA(LENGTH)  

VALUES FOR LATEST VERSION:
VERSION:
    1/2
    
CMD:
    cmdSYN(0)
    cmdFIN(1)
    cmdPSH(2)
    cmdNOP(3)
    cmdUPD(4)	// only supported on version 2
    
STREAMID:
    client use odd numbers starts from 1
    server use even numbers starts from 0
    
cmdUPD:
    | CONSUMED(4B) | WINDOW(4B) |

Usage

func client() {
    // Get a TCP connection
    conn, err := net.Dial(...)
    if err != nil {
        panic(err)
    }

    // Setup client side of smux
    session, err := smux.Client(conn, nil)
    if err != nil {
        panic(err)
    }

    // Open a new stream
    stream, err := session.OpenStream()
    if err != nil {
        panic(err)
    }

    // Stream implements io.ReadWriteCloser
    stream.Write([]byte("ping"))
    stream.Close()
    session.Close()
}

func server() {
    // Accept a TCP connection
    conn, err := listener.Accept()
    if err != nil {
        panic(err)
    }

    // Setup server side of smux
    session, err := smux.Server(conn, nil)
    if err != nil {
        panic(err)
    }

    // Accept a stream
    stream, err := session.AcceptStream()
    if err != nil {
        panic(err)
    }

    // Listen for a message
    buf := make([]byte, 4)
    stream.Read(buf)
    stream.Close()
    session.Close()
}

Status

Stable

More Repositories

1

kcptun

A Quantum-Safe Secure Tunnel based on QPP, KCP, FEC, and N:M multiplexing.
Go
13,904
star
2

algorithms

Algorithms & Data structures in C++.
C++
5,261
star
3

kcp-go

A Crypto-Secure Reliable-UDP Library for golang with FEC
Go
4,086
star
4

gonet

A Game Server Skeleton in golang.
Go
1,264
star
5

gaio

High performance minimalism async-io(proactor) networking for Golang.
Go
808
star
6

libkcp

FEC enhanced KCP session library for iOS/Android in C++
C
308
star
7

tcpraw

Sending packets through TCP
Go
137
star
8

buddha

佛教资料汇集
63
star
9

safebox

One key to derive all
Go
56
star
10

navmesh

navigation mesh in golang
Go
41
star
11

sp

Stream Processors on Kafka in Golang
Go
29
star
12

chat

pub/sub based chat server
Go
27
star
13

lossyconn

lossy connection simulator
Go
24
star
14

rank

ranking server
Go
23
star
15

sstable

bigdata processing in golang
Go
23
star
16

rewind

Text-Based UI for Kafka
Go
20
star
17

qpp

Quantum Permutation Pad (QPP)
Go
18
star
18

goeval

eval golang code on the fly
Go
15
star
19

log_analysis

Practical Log Analysis
15
star
20

fibernet

Message Queue/C++/Lua based game server
C
15
star
21

wsl-best-practice

best practice for development environment in WSL
14
star
22

notes

personal notes
Go
12
star
23

hppk

Homomorphic Polynomial Public Key
Go
11
star
24

gogw

Go
9
star
25

auth

auth service
Go
9
star
26

bgsave

background save process of redis
Go
7
star
27

goperf

golang performance benchmarks
Go
7
star
28

reorg

A simulated LFN network to mitigate network jitter, reorg trade latency in exchange for smoothness, so as to behave like a long fat but stable network.
Go
6
star
29

serialpacket

net.PacketConn over RS232/LoRa
Go
6
star
30

easenet

Automatically exported from code.google.com/p/easenet
C
4
star
31

xtaci

4
star
32

json2hive

generate hive schema from a json document
Go
4
star
33

chacha20

an exposed version of https://godoc.org/golang.org/x/crypto/internal/chacha20
Go
4
star
34

tmach

turing machine game
Go
4
star
35

dppk

A Deterministic Polynomial Public Key Algorithm over a Prime Galois Field GF(p)
Go
4
star
36

kidsmath

a simple program to generate math quizs for my kid.
Go
3
star
37

archiver

redolog archive and replay
Go
3
star
38

serial2tun

Serial To Tun Device
3
star
39

zturn

(zturn:折腾) a free game brings you back to 1980s
3
star
40

goscm

simple scheme interpreter
Go
1
star
41

poly2tri.as3

Automatically exported from code.google.com/p/poly2tri.as3
ActionScript
1
star
42

logrushooks

hooks for logrus
Go
1
star
43

ssh-kvr

lex/yacc learning
C
1
star
44

poly2tri

Automatically exported from code.google.com/p/poly2tri
C++
1
star
45

godeep

machine learning algorithms
1
star
46

deadlocks

deadlock code snippets in C
C
1
star
47

algebra

notes on algebra learning
1
star
48

log4go

Automatically exported from code.google.com/p/log4go
Go
1
star
49

debris

Shamir's Secret Sharing
1
star
50

ethereum_indexer

A project for indexing and querying ethereum accounts
Go
1
star