kcp

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2021 License: GPL-3.0, MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// Overall outer header size needed by data encryption.
	OuterHeaderSize = cipher.DefaultOverhead + cipher.DefaultNonceSize

	// Maximum packet buffer size.
	MaxBufSize = 1500

	// Maximum MTU of UDP packet. UDP overhead is 8 bytes, IP overhead is maximum 40 bytes.
	MaxMTU = MaxBufSize - 48

	IKCP_RTO_NDL = 40    // no delay min retransmission timeout
	IKCP_RTO_MIN = 100   // normal min retransmission timeout
	IKCP_RTO_DEF = 200   // initial retransmission timeout
	IKCP_RTO_MAX = 60000 // max retransmission timeout

	IKCP_CMD_VER     = 0                   // version of command set
	IKCP_CMD_MAX_VER = 7                   // maximum version of command set
	IKCP_CMD_MAX_NUM = 15                  // maximum command number
	IKCP_CMD_PUSH    = 1 + IKCP_CMD_VER<<5 // cmd: send data
	IKCP_CMD_ACK     = 2 + IKCP_CMD_VER<<5 // cmd: acknowledge of a received packet
	IKCP_CMD_WASK    = 3 + IKCP_CMD_VER<<5 // cmd: ask remote window size
	IKCP_CMD_WINS    = 4 + IKCP_CMD_VER<<5 // cmd: reply my window size

	IKCP_ASK_SEND = 1 // need to send IKCP_CMD_WASK
	IKCP_ASK_TELL = 2 // need to send IKCP_CMD_WINS

	IKCP_WND_SND = 1024 // send window size (number of packets)
	IKCP_WND_RCV = 1024 // receive window size (number of packets)

	IKCP_MTU_DEF = MaxMTU - OuterHeaderSize // KCP MTU

	IKCP_ACK_FAST         = 3      // do retransmission after receiving the number of out of order ACK
	IKCP_INTERVAL         = 20     // event loop interval
	IKCP_OVERHEAD         = 24     // size of KCP header
	IKCP_DEADLINK         = 20     // retransmission times before link is dead
	IKCP_THRESH_INIT      = 16     // initial slow start threshold (number of packets)
	IKCP_THRESH_MIN       = 4      // minimum slow start threshold (number of packets)
	IKCP_PROBE_INIT       = 5000   // initial window probe timeout
	IKCP_PROBE_LIMIT      = 120000 // maxinum window probe timeout
	IKCP_SN_OFFSET        = 12     // offset to get sequence number in KCP header
	IKCP_TOTAL_LEN_OFFSET = 22     // offset to get segment total length (data + padding)
)

Variables

View Source
var (
	// PktCachePool is a system-wide packet buffer shared among sending, receiving to mitigate
	// high-frequency memory allocation for packets.
	PktCachePool sync.Pool

	// For testing purpose only, drop some percentage of input segments.
	// If set, this value must be parsible to int and in range of [0, 100).
	TestOnlySegmentDropRate string
)

Functions

func Command2Str

func Command2Str(cmd int) string

Command2Str returns the display name of the KCP command.

Types

type KCP

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

KCP defines a single KCP connection.

func NewKCP

func NewKCP(conv uint32, output outputCallback) *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) ConversationID

func (kcp *KCP) ConversationID() uint32

func (*KCP) Input

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

Input a packet into kcp state machine, by underlay protocol.

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

func (*KCP) LastInputTime

func (kcp *KCP) LastInputTime() time.Time

func (*KCP) LastOutputTime

func (kcp *KCP) LastOutputTime() time.Time

func (*KCP) MSS

func (kcp *KCP) MSS() uint32

func (*KCP) NoDelay

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

NoDelay options. fastest: ikcp_nodelay(kcp, 1, 20, 2, true) 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: disable congestion control

func (*KCP) Output

func (kcp *KCP) Output(ackOnly bool) uint32

Output sends our accumulated data to the remote. Returns the time duration (in milliseconds) that next `Output` should be called.

func (*KCP) PeekSize

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

PeekSize checks the size of next message in the recv queue. It includes all the fragments of the message. Return 0 if the message has length 0 but recv queue is not empty. Return -1 if recv queue is empty.

func (*KCP) RXRTO

func (kcp *KCP) RXRTO() uint32

func (*KCP) RXRTTvar

func (kcp *KCP) RXRTTvar() int32

func (*KCP) RXSRTT

func (kcp *KCP) RXSRTT() int32

func (*KCP) Recv

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

Upper layer receives data from kcp state machine. The received data is copied into the buffer provided by caller. The buffer must be big enough to hold input KCP message (which may have multiple fragments).

Return number of bytes read, or -1 on error.

func (*KCP) RecvWindow

func (kcp *KCP) RecvWindow() uint32

func (*KCP) ReleaseTX

func (kcp *KCP) ReleaseTX()

ReleaseTX releases all cached outgoing segments.

func (*KCP) RemoteWindow

func (kcp *KCP) RemoteWindow() uint32

func (*KCP) ReserveBytes

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

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

Return false if n >= mss

func (*KCP) Send

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

Upper layer sends data to kcp state machine.

func (*KCP) SendHeartbeat

func (kcp *KCP) SendHeartbeat()

Send a heartbeat packet to remote to update remote's lastInputTime. This is implemented by asking KCP to probe the remote window size.

func (*KCP) SendWindow

func (kcp *KCP) SendWindow() uint32

func (*KCP) SetMtu

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

SetMtu changes MTU size.

func (*KCP) SetStreamMode

func (kcp *KCP) SetStreamMode(mode bool)

func (*KCP) SetWindowSize

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

SetWindowSize sets send and receive window size.

func (*KCP) StreamMode

func (kcp *KCP) StreamMode() bool

func (*KCP) WaitSendSize

func (kcp *KCP) WaitSendSize() int

WaitSendSize gets how many packet is waiting to be sent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL