Documentation
¶
Index ¶
- Constants
- Variables
- func Command2Str(cmd int) string
- type KCP
- func (kcp *KCP) ConversationID() uint32
- func (kcp *KCP) Input(data []byte, ackNoDelay bool) error
- func (kcp *KCP) LastInputTime() time.Time
- func (kcp *KCP) LastOutputTime() time.Time
- func (kcp *KCP) MSS() uint32
- func (kcp *KCP) NoDelay(nodelay, interval, resend uint32, nc bool)
- func (kcp *KCP) Output(ackOnly bool) uint32
- func (kcp *KCP) PeekSize() (length int)
- func (kcp *KCP) RXRTO() uint32
- func (kcp *KCP) RXRTTvar() int32
- func (kcp *KCP) RXSRTT() int32
- func (kcp *KCP) Recv(buffer []byte) (n int, err error)
- func (kcp *KCP) RecvWindow() uint32
- func (kcp *KCP) ReleaseTX()
- func (kcp *KCP) RemoteWindow() uint32
- func (kcp *KCP) ReserveBytes(n int) bool
- func (kcp *KCP) Send(buffer []byte) error
- func (kcp *KCP) SendHeartbeat()
- func (kcp *KCP) SendWindow() uint32
- func (kcp *KCP) SetMtu(mtu int) error
- func (kcp *KCP) SetStreamMode(mode bool)
- func (kcp *KCP) SetWindowSize(sndwnd, rcvwnd int) int
- func (kcp *KCP) StreamMode() bool
- func (kcp *KCP) WaitSendSize() int
Constants ¶
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 ¶
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 ¶
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 ¶
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) Input ¶
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) LastOutputTime ¶
func (*KCP) NoDelay ¶
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 ¶
Output sends our accumulated data to the remote. Returns the time duration (in milliseconds) that next `Output` should be called.
func (*KCP) PeekSize ¶
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) Recv ¶
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) ReleaseTX ¶
func (kcp *KCP) ReleaseTX()
ReleaseTX releases all cached outgoing segments.
func (*KCP) RemoteWindow ¶
func (*KCP) ReserveBytes ¶
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) 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) SetStreamMode ¶
func (*KCP) SetWindowSize ¶
SetWindowSize sets send and receive window size.
func (*KCP) StreamMode ¶
func (*KCP) WaitSendSize ¶
WaitSendSize gets how many packet is waiting to be sent.