diff --git a/autotune.go b/autotune.go index 8794826e..f5d25ddc 100644 --- a/autotune.go +++ b/autotune.go @@ -30,7 +30,7 @@ type pulse struct { seq uint32 // sequence of the signal } -// autoTune object +// autoTune object to detect pulses in a signal type autoTune struct { pulses [maxAutoTuneSamples]pulse } diff --git a/batchconn.go b/batchconn.go index 16eb66ca..fc1a5bee 100644 --- a/batchconn.go +++ b/batchconn.go @@ -28,6 +28,7 @@ const ( batchSize = 16 ) +// batchConn defines the interface used in batch IO type batchConn interface { WriteBatch(ms []ipv4.Message, flags int) (int, error) ReadBatch(ms []ipv4.Message, flags int) (int, error) diff --git a/crypt.go b/crypt.go index 8e6c9634..c1e4cb5d 100644 --- a/crypt.go +++ b/crypt.go @@ -42,6 +42,11 @@ import ( ) var ( + // a defined initial vector + // https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Initialization_vector_.28IV.29 + // https://en.wikipedia.org/wiki/Initialization_vector + // actually initial vector is not used in this package, we prepend a random nonce to each outgoing packets. + // though IV is fixed, the first 8 bytes of the encrypted data is always random. initialVector = []byte{167, 115, 79, 156, 18, 172, 27, 1, 164, 21, 242, 193, 252, 120, 230, 107} saltxor = `sH3CIVoF#rWLtJo6` ) @@ -459,6 +464,7 @@ func decrypt8(block cipher.Block, dst, src, buf []byte) { ptr_tbl := (*uint64)(unsafe.Pointer(&tbl[0])) ptr_next := (*uint64)(unsafe.Pointer(&next[0])) + // loop unrolling to relieve data dependency for i := 0; i < repeat; i++ { s := src[base:][0:64] d := dst[base:][0:64] @@ -545,6 +551,8 @@ func decrypt16(block cipher.Block, dst, src, buf []byte) { base := 0 repeat := n / 8 left := n % 8 + + // loop unrolling to relieve data dependency for i := 0; i < repeat; i++ { s := src[base:][0:128] d := dst[base:][0:128] diff --git a/entropy.go b/entropy.go index 9f050f7a..e36ec435 100644 --- a/entropy.go +++ b/entropy.go @@ -31,6 +31,8 @@ import ( ) // Entropy defines a entropy source +// +// Entropy in this file generate random bits for the first 16-bytes of each packets. type Entropy interface { Init() Fill(nonce []byte) diff --git a/fec.go b/fec.go index ebeeee96..3437d563 100644 --- a/fec.go +++ b/fec.go @@ -112,6 +112,7 @@ func (dec *fecDecoder) decode(in fecPacket) (recovered [][]byte) { } } + // if signal is out-of-sync, try to detect the pattern in the signal if dec.shouldTune { autoDS := dec.autoTune.FindPeriod(true) autoPS := dec.autoTune.FindPeriod(false) diff --git a/readloop.go b/readloop.go index 9825a269..de4c1a5b 100644 --- a/readloop.go +++ b/readloop.go @@ -28,6 +28,7 @@ import ( "github.com/pkg/errors" ) +// defaultReadLoop is the standard procedure for reading from a connection func (s *UDPSession) defaultReadLoop() { buf := make([]byte, mtuLimit) var src string @@ -48,6 +49,7 @@ func (s *UDPSession) defaultReadLoop() { } } +// defaultReadLoop is the standard procedure for reading and accepting connections on a listener func (l *Listener) defaultMonitor() { buf := make([]byte, mtuLimit) for { diff --git a/readloop_generic.go b/readloop_generic.go index c6e0cbac..977a0dba 100644 --- a/readloop_generic.go +++ b/readloop_generic.go @@ -21,7 +21,6 @@ // SOFTWARE. //go:build !linux -// +build !linux package kcp diff --git a/readloop_linux.go b/readloop_linux.go index 6ef305cc..bcc30c9f 100644 --- a/readloop_linux.go +++ b/readloop_linux.go @@ -35,7 +35,7 @@ import ( "golang.org/x/net/ipv6" ) -// the read loop for a client session +// readLoop is the optimized version of readLoop for linux utilizing recvmmsg syscall func (s *UDPSession) readLoop() { // default version if s.xconn == nil { @@ -83,7 +83,7 @@ func (s *UDPSession) readLoop() { } } -// monitor incoming data for all connections of server +// monitor is the optimized version of monitor for linux utilizing recvmmsg syscall func (l *Listener) monitor() { var xconn batchConn if _, ok := l.conn.(*net.UDPConn); ok { diff --git a/timedsched.go b/timedsched.go index f73cc793..187ba7d4 100644 --- a/timedsched.go +++ b/timedsched.go @@ -81,6 +81,7 @@ func NewTimedSched(parallel int) *TimedSched { return ts } +// sched is a goroutine to schedule and execute timed tasks. func (ts *TimedSched) sched() { var tasks timedFuncHeap timer := time.NewTimer(0) @@ -119,6 +120,7 @@ func (ts *TimedSched) sched() { } } +// prepend is the front desk goroutine to register tasks func (ts *TimedSched) prepend() { var tasks []timedFunc for { diff --git a/tx.go b/tx.go index 9779f04d..b2351178 100644 --- a/tx.go +++ b/tx.go @@ -29,6 +29,7 @@ import ( "golang.org/x/net/ipv4" ) +// defaultTx is the default procedure to transmit data func (s *UDPSession) defaultTx(txqueue []ipv4.Message) { nbytes := 0 npkts := 0 diff --git a/tx_generic.go b/tx_generic.go index 12e23946..f85d2e20 100644 --- a/tx_generic.go +++ b/tx_generic.go @@ -21,7 +21,6 @@ // SOFTWARE. //go:build !linux -// +build !linux package kcp diff --git a/tx_linux.go b/tx_linux.go index 210ad65b..19be8fc2 100644 --- a/tx_linux.go +++ b/tx_linux.go @@ -21,7 +21,6 @@ // SOFTWARE. //go:build linux -// +build linux package kcp @@ -34,6 +33,7 @@ import ( "golang.org/x/net/ipv4" ) +// tx is the optimized procedure to transmit packets utilizing the linux sendmmsg system call func (s *UDPSession) tx(txqueue []ipv4.Message) { // default version if s.xconn == nil || s.xconnWriteError != nil {