Documentation ¶
Overview ¶
Package kcp - A Fast and Reliable ARQ Protocol
Index ¶
- Constants
- func Dial(raddr string) (net.Conn, error)
- func Listen(laddr string) (net.Listener, error)
- type BlockCrypt
- func NewAESBlockCrypt(key []byte) (BlockCrypt, error)
- func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error)
- func NewCast5BlockCrypt(key []byte) (BlockCrypt, error)
- func NewNoneBlockCrypt(key []byte) (BlockCrypt, error)
- func NewSalsa20BlockCrypt(key []byte) (BlockCrypt, error)
- func NewSimpleXORBlockCrypt(key []byte) (BlockCrypt, error)
- func NewTEABlockCrypt(key []byte) (BlockCrypt, error)
- func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error)
- func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error)
- func NewXTEABlockCrypt(key []byte) (BlockCrypt, error)
- type ConnectedUDPConn
- type FEC
- type KCP
- func (kcp *KCP) Check(current uint32) uint32
- func (kcp *KCP) Input(data []byte, update_ack bool) int
- func (kcp *KCP) NoDelay(nodelay, interval, resend, nc int) int
- func (kcp *KCP) PeekSize() (length int)
- func (kcp *KCP) Recv(buffer []byte) (n int)
- func (kcp *KCP) Send(buffer []byte) int
- func (kcp *KCP) SetMtu(mtu int) int
- func (kcp *KCP) Update(current uint32)
- func (kcp *KCP) WaitSnd() int
- func (kcp *KCP) WndSize(sndwnd, rcvwnd int) int
- type Listener
- func (l *Listener) Accept() (net.Conn, error)
- func (l *Listener) AcceptKCP() (*UDPSession, error)
- func (l *Listener) Addr() net.Addr
- func (l *Listener) Close() error
- func (l *Listener) SetDSCP(dscp int) error
- func (l *Listener) SetDeadline(t time.Time) error
- func (l *Listener) SetReadBuffer(bytes int) error
- func (l *Listener) SetReadDeadline(t time.Time) error
- func (l *Listener) SetWriteBuffer(bytes int) error
- func (l *Listener) SetWriteDeadline(t time.Time) error
- type Output
- type Segment
- type Snmp
- type UDPSession
- func (s *UDPSession) Close() error
- func (s *UDPSession) GetConv() uint32
- func (s *UDPSession) LocalAddr() net.Addr
- func (s *UDPSession) Read(b []byte) (n int, err error)
- func (s *UDPSession) RemoteAddr() net.Addr
- func (s *UDPSession) SetACKNoDelay(nodelay bool)
- func (s *UDPSession) SetDSCP(dscp int) error
- func (s *UDPSession) SetDeadline(t time.Time) error
- func (s *UDPSession) SetKeepAlive(interval int)
- func (s *UDPSession) SetMtu(mtu int)
- func (s *UDPSession) SetNoDelay(nodelay, interval, resend, nc int)
- func (s *UDPSession) SetReadBuffer(bytes int) error
- func (s *UDPSession) SetReadDeadline(t time.Time) error
- func (s *UDPSession) SetStreamMode(enable bool)
- func (s *UDPSession) SetWindowSize(sndwnd, rcvwnd int)
- func (s *UDPSession) SetWriteBuffer(bytes int) error
- func (s *UDPSession) SetWriteDeadline(t time.Time) error
- func (s *UDPSession) Write(b []byte) (n int, err error)
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 )
Variables ¶
This section is empty.
Functions ¶
func Dial ¶
Dial connects to the remote address "raddr" on the network "udp"
Types ¶
type BlockCrypt ¶
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 ¶
func NewAESBlockCrypt(key []byte) (BlockCrypt, error)
NewAESBlockCrypt https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
func NewBlowfishBlockCrypt ¶
func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error)
NewBlowfishBlockCrypt https://en.wikipedia.org/wiki/Blowfish_(cipher)
func NewCast5BlockCrypt ¶
func NewCast5BlockCrypt(key []byte) (BlockCrypt, error)
NewCast5BlockCrypt https://en.wikipedia.org/wiki/CAST-128
func NewNoneBlockCrypt ¶
func NewNoneBlockCrypt(key []byte) (BlockCrypt, error)
NewNoneBlockCrypt does nothing but copying
func NewSalsa20BlockCrypt ¶
func NewSalsa20BlockCrypt(key []byte) (BlockCrypt, error)
NewSalsa20BlockCrypt https://en.wikipedia.org/wiki/Salsa20
func NewSimpleXORBlockCrypt ¶
func NewSimpleXORBlockCrypt(key []byte) (BlockCrypt, error)
NewSimpleXORBlockCrypt simple xor with key expanding
func NewTEABlockCrypt ¶
func NewTEABlockCrypt(key []byte) (BlockCrypt, error)
NewTEABlockCrypt https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
func NewTripleDESBlockCrypt ¶
func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error)
NewTripleDESBlockCrypt https://en.wikipedia.org/wiki/Triple_DES
func NewTwofishBlockCrypt ¶
func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error)
NewTwofishBlockCrypt https://en.wikipedia.org/wiki/Twofish
func NewXTEABlockCrypt ¶
func NewXTEABlockCrypt(key []byte) (BlockCrypt, error)
NewXTEABlockCrypt https://en.wikipedia.org/wiki/XTEA
type ConnectedUDPConn ¶
ConnectedUDPConn is a wrapper for net.UDPConn which converts WriteTo syscalls to Write syscalls that are 4 times faster on some OS'es. This should only be used for connections that were produced by a net.Dial* call.
type FEC ¶
type FEC struct {
// contains filtered or unexported fields
}
FEC defines forward error correction for packets
type KCP ¶
type KCP struct {
// contains filtered or unexported fields
}
KCP defines a single KCP connection
func NewKCP ¶
NewKCP create a new kcp control object, 'conv' must equal in two endpoint from the same connection.
func (*KCP) Check ¶
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 ¶
Input when you received a low level packet (eg. UDP packet), call it
func (*KCP) NoDelay ¶
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 ¶
PeekSize checks the size of next message in the recv queue
func (*KCP) Recv ¶
Recv is user/upper level recv: returns size, returns below zero for EAGAIN
func (*KCP) Send ¶
Send is user/upper level send, returns below zero for error
func (*KCP) Update ¶
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 ¶
WaitSnd gets how many packet is waiting to be sent
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener defines a server listening for connections
func ListenWithOptions ¶
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, dataShards, parityShards defines Reed-Solomon Erasure Coding parameters
func ServeConn ¶
func ServeConn(block BlockCrypt, dataShards, parityShards int, conn net.PacketConn) (*Listener, error)
ServeConn serves KCP protocol for a single packet connection.
func (*Listener) Accept ¶
Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.
func (*Listener) AcceptKCP ¶
func (l *Listener) AcceptKCP() (*UDPSession, error)
AcceptKCP accepts a KCP connection
func (*Listener) 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 ¶
Close stops listening on the UDP address. Already Accepted connections are not closed.
func (*Listener) SetDSCP ¶
SetDSCP sets the 6bit DSCP field of IP header
func (*Listener) SetDeadline ¶
SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.
func (*Listener) SetReadBuffer ¶
SetReadBuffer sets the socket read buffer for the Listener
func (*Listener) SetReadDeadline ¶
SetReadDeadline implements the Conn SetReadDeadline method.
func (*Listener) SetWriteBuffer ¶
SetWriteBuffer sets the socket write buffer for the Listener
type Output ¶
Output is a closure which captures conn and calls conn.Write
type Segment ¶
type Segment struct {
// contains filtered or unexported fields
}
Segment defines a KCP segment
type Snmp ¶
type Snmp struct { BytesSent uint64 // payload bytes sent BytesReceived uint64 MaxConn uint64 ActiveOpens uint64 PassiveOpens uint64 CurrEstab uint64 InErrs uint64 InCsumErrors uint64 // checksum errors KCPInErrors uint64 // packet iput error count from kcp InSegs uint64 OutSegs uint64 OutBytes uint64 // udp bytes sent RetransSegs uint64 FastRetransSegs uint64 EarlyRetransSegs uint64 LostSegs uint64 RepeatSegs uint64 FECRecovered uint64 FECErrs uint64 FECSegs uint64 // fec segments received }
Snmp defines network statistics indicator
var DefaultSnmp *Snmp
DefaultSnmp is the global KCP connection statistics collector
type UDPSession ¶
type UDPSession struct {
// contains filtered or unexported fields
}
UDPSession defines a KCP session implemented by UDP
func DialWithOptions ¶
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
func NewConn ¶
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 (*UDPSession) GetConv ¶
func (s *UDPSession) GetConv() uint32
GetConv gets conversation id of a session
func (*UDPSession) LocalAddr ¶
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 ¶
func (s *UDPSession) Read(b []byte) (n int, err error)
Read implements the Conn Read method.
func (*UDPSession) RemoteAddr ¶
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 ¶
func (s *UDPSession) SetACKNoDelay(nodelay bool)
SetACKNoDelay changes ack flush option, set true to flush ack immediately,
func (*UDPSession) SetDSCP ¶
func (s *UDPSession) SetDSCP(dscp int) error
SetDSCP sets the 6bit DSCP field of IP header, no effect if it's accepted from Listener
func (*UDPSession) SetDeadline ¶
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) SetKeepAlive ¶
func (s *UDPSession) SetKeepAlive(interval int)
SetKeepAlive changes per-connection NAT keepalive interval; 0 to disable, default to 10s
func (*UDPSession) SetMtu ¶
func (s *UDPSession) SetMtu(mtu int)
SetMtu sets the maximum transmission unit
func (*UDPSession) SetNoDelay ¶
func (s *UDPSession) SetNoDelay(nodelay, interval, resend, nc int)
SetNoDelay calls nodelay() of kcp
func (*UDPSession) SetReadBuffer ¶
func (s *UDPSession) SetReadBuffer(bytes int) error
SetReadBuffer sets the socket read buffer, no effect if it's accepted from Listener
func (*UDPSession) SetReadDeadline ¶
func (s *UDPSession) SetReadDeadline(t time.Time) error
SetReadDeadline implements the Conn SetReadDeadline method.
func (*UDPSession) SetStreamMode ¶
func (s *UDPSession) SetStreamMode(enable bool)
SetStreamMode toggles the stream mode on/off
func (*UDPSession) SetWindowSize ¶
func (s *UDPSession) SetWindowSize(sndwnd, rcvwnd int)
SetWindowSize set maximum window size
func (*UDPSession) SetWriteBuffer ¶
func (s *UDPSession) SetWriteBuffer(bytes int) error
SetWriteBuffer sets the socket write buffer, no effect if it's accepted from Listener
func (*UDPSession) SetWriteDeadline ¶
func (s *UDPSession) SetWriteDeadline(t time.Time) error
SetWriteDeadline implements the Conn SetWriteDeadline method.
func (*UDPSession) Write ¶
func (s *UDPSession) Write(b []byte) (n int, err error)
Write implements the Conn Write method.