kcp package - github.com/xtaci/kcp-go - Go Packages (original) (raw)

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.

View Source

const ( IKCP_RTO_NDL = 30
IKCP_RTO_MIN = 100 IKCP_RTO_DEF = 200 IKCP_RTO_MAX = 60000 IKCP_CMD_PUSH = 81 IKCP_CMD_ACK = 82 IKCP_CMD_WASK = 83 IKCP_CMD_WINS = 84 IKCP_ASK_SEND = 1
IKCP_ASK_TELL = 2
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
IKCP_PROBE_LIMIT = 120000 IKCP_SN_OFFSET = 12 )

This section is empty.

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

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

type BlockCrypt interface {

Encrypt(dst, src [][byte](/builtin#byte))


Decrypt(dst, src [][byte](/builtin#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

NewNoneBlockCrypt does nothing but copying

NewSimpleXORBlockCrypt simple xor with key expanding

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

Entropy defines a entropy source

KCP defines a single KCP connection

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.

(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 *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 *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 *KCP) PeekSize() (length int)

PeekSize checks the size of next message in the recv queue

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 *KCP) ReleaseTX()

Release all cached outgoing segments

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

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

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

SetMtu changes MTU size, default is 1400

(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 *KCP) WaitSnd() int

WaitSnd gets how many packet is waiting to be sent

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

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

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

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' specifiy how many parity packets will be generated following the data packets.

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

ServeConn serves KCP protocol for a single packet connection.

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

AcceptKCP accepts a KCP connection

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

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

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.

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

SetReadBuffer sets the socket read buffer for the Listener

SetReadDeadline implements the Conn SetReadDeadline method.

SetWriteBuffer sets the socket write buffer for the Listener

SetWriteDeadline implements the Conn SetWriteDeadline method.

Snmp defines network statistics indicator

DefaultSnmp is the global KCP connection statistics collector

func (s *Snmp) Copy() *Snmp

Copy make a copy of current snmp snapshot

Header returns all field names

ToSlice returns current snmp info as slice

type UDPSession struct {

}

UDPSession defines a KCP session implemented by UDP

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' specifiy how many parity packets will be generated following the data packets.

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

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

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

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

Close closes the connection.

GetConv gets conversation id of a session

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

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

func (s *UDPSession) SetACKNoDelay(nodelay bool)

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

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 (s *UDPSession) SetDUP(dup int)

(deprecated)

SetDUP duplicates udp packets for kcp output.

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

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

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

SetReadDeadline implements the Conn SetReadDeadline method.

func (s *UDPSession) SetStreamMode(enable bool)

SetStreamMode toggles the stream mode on/off

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

SetWindowSize set maximum window size

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

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (s *UDPSession) SetWriteDelay(delay bool)

SetWriteDelay delays write for bulk transfer until the next update interval

Write implements net.Conn

WriteBuffers write a vector of byte slices to the underlying connection