README ¶
Geph fork of KCP
This repo is a fork of KCP-go that tries to maintain compatibility, while making this into a better generic UDP streaming tool. More specifically, it seeks to avoid KCP's focus on "unfair" network flooding, decrease complexity, and improve performance. GephKCP is used to let Geph2 roam seamlessly in intermittent network conditions, NOT to abuse the network.
TODO:
- Remove all encryption/obfuscation features (handled at different layer)
- Saner "unfair" mode (ex: token-bucket limiting to 105% of rolling goodput)
- Smarter congestion control than the textbook Reno currently in use. More specifically cwnd goes up way too slow.
ORIGINAL README BELOW:
Introduction
kcp-go is a Production-Grade Reliable-UDP library for golang.
This library intents to provide a smooth, resilient, ordered, error-checked and anonymous delivery of streams over UDP packets, it has been battle-tested with opensource project kcptun. Millions of devices(from low-end MIPS routers to high-end servers) have deployed kcp-go powered program in a variety of forms like online games, live broadcasting, file synchronization and network acceleration.
Features
- Designed for Latency-sensitive scenarios.
- Cache friendly and Memory optimized design, offers extremely High Performance core.
- Handles >5K concurrent connections on a single commodity server.
- Compatible with net.Conn and net.Listener, a drop-in replacement for net.TCPConn.
- FEC(Forward Error Correction) Support with Reed-Solomon Codes
- Packet level encryption support with AES, TEA, 3DES, Blowfish, Cast5, Salsa20, etc. in CFB mode, which generates completely anonymous packet.
- Only A fixed number of goroutines will be created for the entire server application, costs in context switch between goroutines have been taken into consideration.
- Compatible with skywind3000's C version with various improvements.
- Platform-dependent optimizations: sendmmsg and recvmmsg were expoloited for linux.
Documentation
For complete documentation, see the associated Godoc.
Specification
NONCE:
16bytes cryptographically secure random number, nonce changes for every packet.
CRC32:
CRC-32 checksum of data using the IEEE polynomial
FEC TYPE:
typeData = 0xF1
typeParity = 0xF2
FEC SEQID:
monotonically increasing in range: [0, (0xffffffff/shardSize) * shardSize - 1]
SIZE:
The size of KCP frame plus 2
+-----------------+
| SESSION |
+-----------------+
| KCP(ARQ) |
+-----------------+
| FEC(OPTIONAL) |
+-----------------+
| CRYPTO(OPTIONAL)|
+-----------------+
| UDP(PACKET) |
+-----------------+
| IP |
+-----------------+
| LINK |
+-----------------+
| PHY |
+-----------------+
(LAYER MODEL OF KCP-GO)
Examples
Benchmark
Model Name: MacBook Pro
Model Identifier: MacBookPro14,1
Processor Name: Intel Core i5
Processor Speed: 3.1 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 8 GB
$ go test -v -run=^$ -bench .
beginning tests, encryption:salsa20, fec:10/3
goos: darwin
goarch: amd64
pkg: github.com/xtaci/kcp-go
BenchmarkSM4-4 50000 32180 ns/op 93.23 MB/s 0 B/op 0 allocs/op
BenchmarkAES128-4 500000 3285 ns/op 913.21 MB/s 0 B/op 0 allocs/op
BenchmarkAES192-4 300000 3623 ns/op 827.85 MB/s 0 B/op 0 allocs/op
BenchmarkAES256-4 300000 3874 ns/op 774.20 MB/s 0 B/op 0 allocs/op
BenchmarkTEA-4 100000 15384 ns/op 195.00 MB/s 0 B/op 0 allocs/op
BenchmarkXOR-4 20000000 89.9 ns/op 33372.00 MB/s 0 B/op 0 allocs/op
BenchmarkBlowfish-4 50000 26927 ns/op 111.41 MB/s 0 B/op 0 allocs/op
BenchmarkNone-4 30000000 45.7 ns/op 65597.94 MB/s 0 B/op 0 allocs/op
BenchmarkCast5-4 50000 34258 ns/op 87.57 MB/s 0 B/op 0 allocs/op
Benchmark3DES-4 10000 117149 ns/op 25.61 MB/s 0 B/op 0 allocs/op
BenchmarkTwofish-4 50000 33538 ns/op 89.45 MB/s 0 B/op 0 allocs/op
BenchmarkXTEA-4 30000 45666 ns/op 65.69 MB/s 0 B/op 0 allocs/op
BenchmarkSalsa20-4 500000 3308 ns/op 906.76 MB/s 0 B/op 0 allocs/op
BenchmarkCRC32-4 20000000 65.2 ns/op 15712.43 MB/s
BenchmarkCsprngSystem-4 1000000 1150 ns/op 13.91 MB/s
BenchmarkCsprngMD5-4 10000000 145 ns/op 110.26 MB/s
BenchmarkCsprngSHA1-4 10000000 158 ns/op 126.54 MB/s
BenchmarkCsprngNonceMD5-4 10000000 153 ns/op 104.22 MB/s
BenchmarkCsprngNonceAES128-4 100000000 19.1 ns/op 837.81 MB/s
BenchmarkFECDecode-4 1000000 1119 ns/op 1339.61 MB/s 1606 B/op 2 allocs/op
BenchmarkFECEncode-4 2000000 832 ns/op 1801.83 MB/s 17 B/op 0 allocs/op
BenchmarkFlush-4 5000000 272 ns/op 0 B/op 0 allocs/op
BenchmarkEchoSpeed4K-4 5000 259617 ns/op 15.78 MB/s 5451 B/op 149 allocs/op
BenchmarkEchoSpeed64K-4 1000 1706084 ns/op 38.41 MB/s 56002 B/op 1604 allocs/op
BenchmarkEchoSpeed512K-4 100 14345505 ns/op 36.55 MB/s 482597 B/op 13045 allocs/op
BenchmarkEchoSpeed1M-4 30 34859104 ns/op 30.08 MB/s 1143773 B/op 27186 allocs/op
BenchmarkSinkSpeed4K-4 50000 31369 ns/op 130.57 MB/s 1566 B/op 30 allocs/op
BenchmarkSinkSpeed64K-4 5000 329065 ns/op 199.16 MB/s 21529 B/op 453 allocs/op
BenchmarkSinkSpeed256K-4 500 2373354 ns/op 220.91 MB/s 166332 B/op 3554 allocs/op
BenchmarkSinkSpeed1M-4 300 5117927 ns/op 204.88 MB/s 310378 B/op 6988 allocs/op
PASS
ok github.com/xtaci/kcp-go 50.349s
Typical Flame Graph
Key Design Considerations
- slice vs. container/list
kcp.flush()
loops through the send queue for retransmission checking for every 20ms(interval).
I've wrote a benchmark for comparing sequential loop through slice and container/list here:
https://github.com/xtaci/notes/blob/master/golang/benchmark2/cachemiss_test.go
BenchmarkLoopSlice-4 2000000000 0.39 ns/op
BenchmarkLoopList-4 100000000 54.6 ns/op
List structure introduces heavy cache misses compared to slice which owns better locality, 5000 connections with 32 window size and 20ms interval will cost 6us/0.03%(cpu) using slice, and 8.7ms/43.5%(cpu) for list for each kcp.flush()
.
- Timing accuracy vs. syscall clock_gettime
Timing is critical to RTT estimator, inaccurate timing leads to false retransmissions in KCP, but calling time.Now()
costs 42 cycles(10.5ns on 4GHz CPU, 15.6ns on my MacBook Pro 2.7GHz).
The benchmark for time.Now() lies here:
https://github.com/xtaci/notes/blob/master/golang/benchmark2/syscall_test.go
BenchmarkNow-4 100000000 15.6 ns/op
In kcp-go, after each kcp.output()
function call, current clock time will be updated upon return, and for a single kcp.flush()
operation, current time will be queried from system once. For most of the time, 5000 connections costs 5000 * 15.6ns = 78us(a fixed cost while no packet needs to be sent), as for 10MB/s data transfering with 1400 MTU, kcp.output()
will be called around 7500 times and costs 117us for time.Now()
in every second.
- Memory management
Primary memory allocation are done from a global buffer pool xmit.Buf, in kcp-go, when we need to allocate some bytes, we can get from that pool, and a fixed-capacity 1500 bytes(mtuLimit) will be returned, the rx queue, tx queue and fec queue all receive bytes from there, and they will return the bytes to the pool after using to prevent unnecessary zer0ing of bytes. The pool mechanism maintained a high watermark for slice objects, these in-flight objects from the pool will survive from the perodical garbage collection, meanwhile the pool kept the ability to return the memory to runtime if in idle.
- Information security
kcp-go is shipped with builtin packet encryption powered by various block encryption algorithms and works in Cipher Feedback Mode, for each packet to be sent, the encryption process will start from encrypting a nonce from the system entropy, so encryption to same plaintexts never leads to a same ciphertexts thereafter.
The contents of the packets are completely anonymous with encryption, including the headers(FEC,KCP), checksums and contents. Note that, no matter which encryption method you choose on you upper layer, if you disable encryption, the transmit will be insecure somehow, since the header is PLAINTEXT to everyone it would be susceptible to header tampering, such as jamming the sliding window size, round-trip time, FEC property and checksums. AES-128
is suggested for minimal encryption since modern CPUs are shipped with AES-NI instructions and performs even better than salsa20
(check the table above).
Other possible attacks to kcp-go includes: a) traffic analysis, dataflow on specific websites may have pattern while interchanging data, but this type of eavesdropping has been mitigated by adapting smux to mix data streams so as to introduce noises, perfect solution to this has not appeared yet, theroretically by shuffling/mixing messages on larger scale network may mitigate this problem. b) replay attack, since the asymmetrical encryption has not been introduced into kcp-go for some reason, capturing the packets and replay them on a different machine is possible, (notice: hijacking the session and decrypting the contents is still impossible), so upper layers should contain a asymmetrical encryption system to guarantee the authenticity of each message(to process message exactly once), such as HTTPS/OpenSSL/LibreSSL, only by signing the requests with private keys can eliminate this type of attack.
Connection Termination
Control messages like SYN/FIN/RST in TCP are not defined in KCP, you need some keepalive/heartbeat mechanism in the application-level. A real world example is to use some multiplexing protocol over session, such as smux(with embedded keepalive mechanism), see kcptun for example.
FAQ
Q: I'm handling >5K connections on my server, the CPU utilization is so high.
A: A standalone agent
or gate
server for running kcp-go is suggested, not only for CPU utilization, but also important to the precision of RTT measurements(timing) which indirectly affects retransmission. By increasing update interval
with SetNoDelay
like conn.SetNoDelay(1, 40, 1, 1)
will dramatically reduce system load, but lower the performance.
Q: When should I enable FEC?
A: Forward error correction is critical to long-distance transmission, because a packet loss will lead to a huge penalty in time. And for the complicated packet routing network in modern world, round-trip time based loss check will not always be efficient, the big deviation of RTT samples in the long way usually leads to a larger RTO value in typical rtt estimator, which in other words, slows down the transmission.
Q: Should I enable encryption?
A: Yes, for the safety of protocol, even if the upper layer has encrypted.
Who is using this?
- https://github.com/xtaci/kcptun -- A Secure Tunnel Based On KCP over UDP.
- https://github.com/getlantern/lantern -- Lantern delivers fast access to the open Internet.
- https://github.com/smallnest/rpcx -- A RPC service framework based on net/rpc like alibaba Dubbo and weibo Motan.
- https://github.com/gonet2/agent -- A gateway for games with stream multiplexing.
- https://github.com/syncthing/syncthing -- Open Source Continuous File Synchronization.
Links
- https://github.com/xtaci/libkcp -- FEC enhanced KCP session library for iOS/Android in C++
- https://github.com/skywind3000/kcp -- A Fast and Reliable ARQ Protocol
- https://github.com/klauspost/reedsolomon -- Reed-Solomon Erasure Coding in Go
Documentation ¶
Overview ¶
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 ¶
- Constants
- Variables
- 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 NewSM4BlockCrypt(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 Entropy
- type KCP
- func (kcp *KCP) Input(data []byte, regular, ackNoDelay 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) ReserveBytes(n int) bool
- func (kcp *KCP) Send(buffer []byte) int
- func (kcp *KCP) SetMtu(mtu int) int
- 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 Snmp
- type UDPSession
- func DialWithOptions(raddr string, block BlockCrypt, dataShards, parityShards int) (*UDPSession, error)
- func NewConn(raddr string, block BlockCrypt, dataShards, parityShards int, ...) (*UDPSession, error)
- func NewConn2(raddr net.Addr, block BlockCrypt, dataShards, parityShards int, ...) (*UDPSession, error)
- func NewConn3(convid uint32, raddr net.Addr, block BlockCrypt, dataShards, parityShards int, ...) (*UDPSession, error)
- func (s *UDPSession) Close() error
- func (s *UDPSession) FlowStats() (btlBw float64, latency float64, lossFrac float64)
- 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) SetDUP(dup int)
- func (s *UDPSession) SetDeadline(t time.Time) error
- func (s *UDPSession) SetMtu(mtu int) bool
- 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) SetWriteDelay(delay bool)
- func (s *UDPSession) Write(b []byte) (n int, err error)
- func (s *UDPSession) WriteBuffers(v [][]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 ¶
var CongestionControl = "LOL"
Functions ¶
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 NewSM4BlockCrypt ¶
func NewSM4BlockCrypt(key []byte) (BlockCrypt, error)
NewSM4BlockCrypt https://github.com/tjfoc/gmsm/tree/master/sm4
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 Entropy ¶
type Entropy interface { Init() Fill(nonce []byte) }
Entropy defines a entropy source
type KCP ¶
type KCP struct { DRE struct { // contains filtered or unexported fields } LOL struct { // contains filtered or unexported fields } // contains filtered or unexported fields }
KCP defines a single KCP connection
func NewKCP ¶
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) Input ¶
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 ¶
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) Recv ¶
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) ReserveBytes ¶
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
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener defines a server which will be waiting to accept incoming 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.
'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
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) SetDSCP ¶
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 ¶
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 Snmp ¶
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 infered 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) RecentLoss ¶
RecentLoss computes the recent pkt loss.
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
'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
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 NewConn2 ¶
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 ¶
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) FlowStats ¶
func (s *UDPSession) FlowStats() (btlBw float64, latency float64, lossFrac float64)
FlowStats summarizes flow statistics
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 net.Conn
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 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 ¶
func (s *UDPSession) SetDUP(dup int)
(deprecated)
SetDUP duplicates udp packets for kcp output.
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) SetMtu ¶
func (s *UDPSession) SetMtu(mtu int) bool
SetMtu sets the maximum transmission unit(not including UDP header)
func (*UDPSession) SetNoDelay ¶
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 ¶
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) SetWriteDelay ¶
func (s *UDPSession) SetWriteDelay(delay bool)
SetWriteDelay delays write for bulk transfer until the next update interval
func (*UDPSession) Write ¶
func (s *UDPSession) Write(b []byte) (n int, err error)
Write implements net.Conn
func (*UDPSession) WriteBuffers ¶
func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error)
WriteBuffers write a vector of byte slices to the underlying connection