kcp-go: github.com/xtaci/kcp-go Index | Files | Directories

package kcp

import "github.com/xtaci/kcp-go"

Package kcp-go is a Reliable-UDP library for golang.

This library intents to provide a smooth, resilient, ordered, error-checked and anonymous delivery of streams over UDP packets.

The interfaces of this package aims to be compatible with net.Conn in standard library, but offers powerful features for advanced users.

Index

Package Files

autotune.go batchconn.go crypt.go entropy.go fec.go kcp.go readloop.go readloop_linux.go sess.go snmp.go timedsched.go tx.go tx_linux.go

Constants

const (
    IKCP_RTO_NDL     = 30  // no delay min rto
    IKCP_RTO_MIN     = 100 // normal min rto
    IKCP_RTO_DEF     = 200
    IKCP_RTO_MAX     = 60000
    IKCP_CMD_PUSH    = 81 // cmd: push data
    IKCP_CMD_ACK     = 82 // cmd: ack
    IKCP_CMD_WASK    = 83 // cmd: window probe (ask)
    IKCP_CMD_WINS    = 84 // cmd: window size (tell)
    IKCP_ASK_SEND    = 1  // need to send IKCP_CMD_WASK
    IKCP_ASK_TELL    = 2  // need to send IKCP_CMD_WINS
    IKCP_WND_SND     = 32
    IKCP_WND_RCV     = 32
    IKCP_MTU_DEF     = 1400
    IKCP_ACK_FAST    = 3
    IKCP_INTERVAL    = 100
    IKCP_OVERHEAD    = 24
    IKCP_DEADLINK    = 20
    IKCP_THRESH_INIT = 2
    IKCP_THRESH_MIN  = 2
    IKCP_PROBE_INIT  = 7000   // 7 secs to probe window size
    IKCP_PROBE_LIMIT = 120000 // up to 120 secs to probe window
    IKCP_SN_OFFSET   = 12
)

func Dial Uses

func Dial(raddr string) (net.Conn, error)

Dial connects to the remote address "raddr" on the network "udp" without encryption and FEC

func Listen Uses

func Listen(laddr string) (net.Listener, error)

Listen listens for incoming KCP packets addressed to the local address laddr on the network "udp",

type BlockCrypt Uses

type BlockCrypt interface {
    // Encrypt encrypts the whole block in src into dst.
    // Dst and src may point at the same memory.
    Encrypt(dst, src []byte)

    // Decrypt decrypts the whole block in src into dst.
    // Dst and src may point at the same memory.
    Decrypt(dst, src []byte)
}

BlockCrypt defines encryption/decryption methods for a given byte slice. Notes on implementing: the data to be encrypted contains a builtin nonce at the first 16 bytes

func NewAESBlockCrypt Uses

func NewAESBlockCrypt(key []byte) (BlockCrypt, error)

NewAESBlockCrypt https://en.wikipedia.org/wiki/Advanced_Encryption_Standard

func NewBlowfishBlockCrypt Uses

func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error)

NewBlowfishBlockCrypt https://en.wikipedia.org/wiki/Blowfish_(cipher)

func NewCast5BlockCrypt Uses

func NewCast5BlockCrypt(key []byte) (BlockCrypt, error)

NewCast5BlockCrypt https://en.wikipedia.org/wiki/CAST-128

func NewNoneBlockCrypt Uses

func NewNoneBlockCrypt(key []byte) (BlockCrypt, error)

NewNoneBlockCrypt does nothing but copying

func NewSM4BlockCrypt Uses

func NewSM4BlockCrypt(key []byte) (BlockCrypt, error)

NewSM4BlockCrypt https://github.com/tjfoc/gmsm/tree/master/sm4

func NewSalsa20BlockCrypt Uses

func NewSalsa20BlockCrypt(key []byte) (BlockCrypt, error)

NewSalsa20BlockCrypt https://en.wikipedia.org/wiki/Salsa20

func NewSimpleXORBlockCrypt Uses

func NewSimpleXORBlockCrypt(key []byte) (BlockCrypt, error)

NewSimpleXORBlockCrypt simple xor with key expanding

func NewTEABlockCrypt Uses

func NewTEABlockCrypt(key []byte) (BlockCrypt, error)

NewTEABlockCrypt https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm

func NewTripleDESBlockCrypt Uses

func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error)

NewTripleDESBlockCrypt https://en.wikipedia.org/wiki/Triple_DES

func NewTwofishBlockCrypt Uses

func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error)

NewTwofishBlockCrypt https://en.wikipedia.org/wiki/Twofish

func NewXTEABlockCrypt Uses

func NewXTEABlockCrypt(key []byte) (BlockCrypt, error)

NewXTEABlockCrypt https://en.wikipedia.org/wiki/XTEA

type Entropy Uses

type Entropy interface {
    Init()
    Fill(nonce []byte)
}

Entropy defines a entropy source

type KCP Uses

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

KCP defines a single KCP connection

func NewKCP Uses

func NewKCP(conv uint32, output output_callback) *KCP

NewKCP create a new kcp state machine

'conv' must be equal in the connection peers, or else data will be silently rejected.

'output' function will be called whenever these is data to be sent on wire.

func (*KCP) Check Uses

func (kcp *KCP) Check() uint32

(deprecated)

Check determines when should you invoke ikcp_update: returns when you should invoke ikcp_update in millisec, if there is no ikcp_input/_send calling. you can call ikcp_update in that time, instead of call update repeatly. Important to reduce unnacessary ikcp_update invoking. use it to schedule ikcp_update (eg. implementing an epoll-like mechanism, or optimize ikcp_update when handling massive kcp connections)

func (*KCP) Input Uses

func (kcp *KCP) Input(data []byte, regular, ackNoDelay bool) int

Input a packet into kcp state machine.

'regular' indicates it's a real data packet from remote, and it means it's not generated from ReedSolomon codecs.

'ackNoDelay' will trigger immediate ACK, but surely it will not be efficient in bandwidth

func (*KCP) NoDelay Uses

func (kcp *KCP) NoDelay(nodelay, interval, resend, nc int) int

NoDelay options fastest: ikcp_nodelay(kcp, 1, 20, 2, 1) nodelay: 0:disable(default), 1:enable interval: internal update timer interval in millisec, default is 100ms resend: 0:disable fast resend(default), 1:enable fast resend nc: 0:normal congestion control(default), 1:disable congestion control

func (*KCP) PeekSize Uses

func (kcp *KCP) PeekSize() (length int)

PeekSize checks the size of next message in the recv queue

func (*KCP) Recv Uses

func (kcp *KCP) Recv(buffer []byte) (n int)

Receive data from kcp state machine

Return number of bytes read.

Return -1 when there is no readable data.

Return -2 if len(buffer) is smaller than kcp.PeekSize().

func (*KCP) ReleaseTX Uses

func (kcp *KCP) ReleaseTX()

Release all cached outgoing segments

func (*KCP) ReserveBytes Uses

func (kcp *KCP) ReserveBytes(n int) bool

ReserveBytes keeps n bytes untouched from the beginning of the buffer, the output_callback function should be aware of this.

Return false if n >= mss

func (*KCP) Send Uses

func (kcp *KCP) Send(buffer []byte) int

Send is user/upper level send, returns below zero for error

func (*KCP) SetMtu Uses

func (kcp *KCP) SetMtu(mtu int) int

SetMtu changes MTU size, default is 1400

func (*KCP) Update Uses

func (kcp *KCP) Update()

(deprecated)

Update updates state (call it repeatedly, every 10ms-100ms), or you can ask ikcp_check when to call it again (without ikcp_input/_send calling). 'current' - current timestamp in millisec.

func (*KCP) WaitSnd Uses

func (kcp *KCP) WaitSnd() int

WaitSnd gets how many packet is waiting to be sent

func (*KCP) WndSize Uses

func (kcp *KCP) WndSize(sndwnd, rcvwnd int) int

WndSize sets maximum window size: sndwnd=32, rcvwnd=32 by default

type Listener Uses

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

Listener defines a server which will be waiting to accept incoming connections

func ListenWithOptions Uses

func ListenWithOptions(laddr string, block BlockCrypt, dataShards, parityShards int) (*Listener, error)

ListenWithOptions listens for incoming KCP packets addressed to the local address laddr on the network "udp" with packet encryption.

'block' is the block encryption algorithm to encrypt packets.

'dataShards', 'parityShards' specify how many parity packets will be generated following the data packets.

Check https://github.com/klauspost/reedsolomon for details

func ServeConn Uses

func ServeConn(block BlockCrypt, dataShards, parityShards int, conn net.PacketConn) (*Listener, error)

ServeConn serves KCP protocol for a single packet connection.

func (*Listener) Accept Uses

func (l *Listener) Accept() (net.Conn, error)

Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.

func (*Listener) AcceptKCP Uses

func (l *Listener) AcceptKCP() (*UDPSession, error)

AcceptKCP accepts a KCP connection

func (*Listener) Addr Uses

func (l *Listener) Addr() net.Addr

Addr returns the listener's network address, The Addr returned is shared by all invocations of Addr, so do not modify it.

func (*Listener) Close Uses

func (l *Listener) Close() error

Close stops listening on the UDP address, and closes the socket

func (*Listener) SetDSCP Uses

func (l *Listener) SetDSCP(dscp int) error

SetDSCP sets the 6bit DSCP field in IPv4 header, or 8bit Traffic Class in IPv6 header.

if the underlying connection has implemented `func SetDSCP(int) error`, SetDSCP() will invoke this function instead.

func (*Listener) SetDeadline Uses

func (l *Listener) SetDeadline(t time.Time) error

SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.

func (*Listener) SetReadBuffer Uses

func (l *Listener) SetReadBuffer(bytes int) error

SetReadBuffer sets the socket read buffer for the Listener

func (*Listener) SetReadDeadline Uses

func (l *Listener) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*Listener) SetWriteBuffer Uses

func (l *Listener) SetWriteBuffer(bytes int) error

SetWriteBuffer sets the socket write buffer for the Listener

func (*Listener) SetWriteDeadline Uses

func (l *Listener) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

type Snmp Uses

type Snmp struct {
    BytesSent        uint64 // bytes sent from upper level
    BytesReceived    uint64 // bytes received to upper level
    MaxConn          uint64 // max number of connections ever reached
    ActiveOpens      uint64 // accumulated active open connections
    PassiveOpens     uint64 // accumulated passive open connections
    CurrEstab        uint64 // current number of established connections
    InErrs           uint64 // UDP read errors reported from net.PacketConn
    InCsumErrors     uint64 // checksum errors from CRC32
    KCPInErrors      uint64 // packet iput errors reported from KCP
    InPkts           uint64 // incoming packets count
    OutPkts          uint64 // outgoing packets count
    InSegs           uint64 // incoming KCP segments
    OutSegs          uint64 // outgoing KCP segments
    InBytes          uint64 // UDP bytes received
    OutBytes         uint64 // UDP bytes sent
    RetransSegs      uint64 // accmulated retransmited segments
    FastRetransSegs  uint64 // accmulated fast retransmitted segments
    EarlyRetransSegs uint64 // accmulated early retransmitted segments
    LostSegs         uint64 // number of segs inferred as lost
    RepeatSegs       uint64 // number of segs duplicated
    FECRecovered     uint64 // correct packets recovered from FEC
    FECErrs          uint64 // incorrect packets recovered from FEC
    FECParityShards  uint64 // FEC segments received
    FECShortShards   uint64 // number of data shards that's not enough for recovery
}

Snmp defines network statistics indicator

var DefaultSnmp *Snmp

DefaultSnmp is the global KCP connection statistics collector

func (*Snmp) Copy Uses

func (s *Snmp) Copy() *Snmp

Copy make a copy of current snmp snapshot

func (*Snmp) Header Uses

func (s *Snmp) Header() []string

Header returns all field names

func (*Snmp) Reset Uses

func (s *Snmp) Reset()

Reset values to zero

func (*Snmp) ToSlice Uses

func (s *Snmp) ToSlice() []string

ToSlice returns current snmp info as slice

type TimedSched Uses

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

TimedSched represents the control struct for timed parallel scheduler

var SystemTimedSched *TimedSched = NewTimedSched(runtime.NumCPU())

SystemTimedSched is the library level timed-scheduler

func NewTimedSched Uses

func NewTimedSched(parallel int) *TimedSched

NewTimedSched creates a parallel-scheduler with given parallelization

func (*TimedSched) Close Uses

func (ts *TimedSched) Close()

Close terminates this scheduler

func (*TimedSched) Put Uses

func (ts *TimedSched) Put(f func(), deadline time.Time)

Put a function 'f' awaiting to be executed at 'deadline'

type UDPSession Uses

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

UDPSession defines a KCP session implemented by UDP

func DialWithOptions Uses

func DialWithOptions(raddr string, block BlockCrypt, dataShards, parityShards int) (*UDPSession, error)

DialWithOptions connects to the remote address "raddr" on the network "udp" with packet encryption

'block' is the block encryption algorithm to encrypt packets.

'dataShards', 'parityShards' specify how many parity packets will be generated following the data packets.

Check https://github.com/klauspost/reedsolomon for details

func NewConn Uses

func NewConn(raddr string, block BlockCrypt, dataShards, parityShards int, conn net.PacketConn) (*UDPSession, error)

NewConn establishes a session and talks KCP protocol over a packet connection.

func NewConn2 Uses

func NewConn2(raddr net.Addr, block BlockCrypt, dataShards, parityShards int, conn net.PacketConn) (*UDPSession, error)

NewConn2 establishes a session and talks KCP protocol over a packet connection.

func NewConn3 Uses

func NewConn3(convid uint32, raddr net.Addr, block BlockCrypt, dataShards, parityShards int, conn net.PacketConn) (*UDPSession, error)

NewConn3 establishes a session and talks KCP protocol over a packet connection.

func (*UDPSession) Close Uses

func (s *UDPSession) Close() error

Close closes the connection.

func (*UDPSession) GetConv Uses

func (s *UDPSession) GetConv() uint32

GetConv gets conversation id of a session

func (*UDPSession) GetRTO Uses

func (s *UDPSession) GetRTO() uint32

GetRTO gets current rto of the session

func (*UDPSession) GetSRTT Uses

func (s *UDPSession) GetSRTT() int32

GetSRTT gets current srtt of the session

func (*UDPSession) GetSRTTVar Uses

func (s *UDPSession) GetSRTTVar() int32

GetRTTVar gets current rtt variance of the session

func (*UDPSession) LocalAddr Uses

func (s *UDPSession) LocalAddr() net.Addr

LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.

func (*UDPSession) Read Uses

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

Read implements net.Conn

func (*UDPSession) RemoteAddr Uses

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

RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.

func (*UDPSession) SetACKNoDelay Uses

func (s *UDPSession) SetACKNoDelay(nodelay bool)

SetACKNoDelay changes ack flush option, set true to flush ack immediately,

func (*UDPSession) SetDSCP Uses

func (s *UDPSession) SetDSCP(dscp int) error

SetDSCP sets the 6bit DSCP field in IPv4 header, or 8bit Traffic Class in IPv6 header.

if the underlying connection has implemented `func SetDSCP(int) error`, SetDSCP() will invoke this function instead.

It has no effect if it's accepted from Listener.

func (*UDPSession) SetDUP Uses

func (s *UDPSession) SetDUP(dup int)

(deprecated)

SetDUP duplicates udp packets for kcp output.

func (*UDPSession) SetDeadline Uses

func (s *UDPSession) SetDeadline(t time.Time) error

SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.

func (*UDPSession) SetMtu Uses

func (s *UDPSession) SetMtu(mtu int) bool

SetMtu sets the maximum transmission unit(not including UDP header)

func (*UDPSession) SetNoDelay Uses

func (s *UDPSession) SetNoDelay(nodelay, interval, resend, nc int)

SetNoDelay calls nodelay() of kcp https://github.com/skywind3000/kcp/blob/master/README.en.md#protocol-configuration

func (*UDPSession) SetReadBuffer Uses

func (s *UDPSession) SetReadBuffer(bytes int) error

SetReadBuffer sets the socket read buffer, no effect if it's accepted from Listener

func (*UDPSession) SetReadDeadline Uses

func (s *UDPSession) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*UDPSession) SetStreamMode Uses

func (s *UDPSession) SetStreamMode(enable bool)

SetStreamMode toggles the stream mode on/off

func (*UDPSession) SetWindowSize Uses

func (s *UDPSession) SetWindowSize(sndwnd, rcvwnd int)

SetWindowSize set maximum window size

func (*UDPSession) SetWriteBuffer Uses

func (s *UDPSession) SetWriteBuffer(bytes int) error

SetWriteBuffer sets the socket write buffer, no effect if it's accepted from Listener

func (*UDPSession) SetWriteDeadline Uses

func (s *UDPSession) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (*UDPSession) SetWriteDelay Uses

func (s *UDPSession) SetWriteDelay(delay bool)

SetWriteDelay delays write for bulk transfer until the next update interval

func (*UDPSession) Write Uses

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

Write implements net.Conn

func (*UDPSession) WriteBuffers Uses

func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error)

WriteBuffers write a vector of byte slices to the underlying connection

Directories

PathSynopsis
examples

Package kcp imports 31 packages (graph) and is imported by 112 packages. Updated 2020-10-10. Refresh now. Tools for package owners.