kcp

package
v0.0.0-...-022078a Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package kcp - A Fast and Reliable ARQ Protocol

Index

Constants

View Source
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    = 10
	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
)
View Source
const (
	MODE_DEFAULT Mode = iota
	MODE_NORMAL
	MODE_FAST
	BASE_PORT        = 20000
	MAX_PORT         = 65535
	DEFAULT_WND_SIZE = 128
	XOR_TABLE_SIZE   = 16384
)

Variables

View Source
var (
	ERR_TIMEOUT     = errors.New("i/o timeout")
	ERR_BROKEN_PIPE = errors.New("broken pipe")
	IV              = []byte{167, 115, 79, 156, 18, 172, 27, 1, 164, 21, 242, 193, 252, 120, 230, 107}
)

Functions

This section is empty.

Types

type KCP

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

KCP Connection Definition

func NewKCP

func NewKCP(conv uint32, output Output) *KCP

NewKCP create a new kcp control object, 'conv' must equal in two endpoint from the same connection.

func (*KCP) Check

func (kcp *KCP) Check(current uint32) uint32

Check Determine 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

func (kcp *KCP) Input(data []byte) int

Input when you received a low level packet (eg. UDP packet), call it

func (*KCP) Interval

func (kcp *KCP) Interval(interval int) int

func (*KCP) NoDelay

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

NoDelay 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

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

PeekSize check the size of next message in the recv queue

func (*KCP) Recv

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

Recv user/upper level recv: returns size, returns below zero for EAGAIN

func (*KCP) Send

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

Send user/upper level send, returns below zero for error

func (*KCP) SetMtu

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

SetMtu change MTU size, default is 1400

func (*KCP) Update

func (kcp *KCP) Update(current uint32)

Update 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

func (kcp *KCP) WaitSnd() int

WaitSnd get how many packet is waiting to be sent

func (*KCP) WndSize

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

WndSize set maximum window size: sndwnd=32, rcvwnd=32 by default

type Listener

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

func Listen

func Listen(mode Mode, laddr string) (*Listener, error)

Listen listens for incoming KCP packets addressed to the local address laddr on the network "udp", mode must be one of: MODE_DEFAULT,MODE_NORMAL,MODE_FAST

func ListenEncrypted

func ListenEncrypted(mode Mode, laddr string, key string) (*Listener, error)

Listen listens for incoming KCP packets addressed to the local address laddr on the network "udp" with packet encryption, mode must be one of: MODE_DEFAULT,MODE_NORMAL,MODE_FAST

func (*Listener) Accept

func (l *Listener) Accept() (*UDPSession, error)

Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.

func (*Listener) Addr

func (l *Listener) Addr() net.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

func (l *Listener) Close() error

Close stops listening on the TCP address. Already Accepted connections are not closed.

func (*Listener) SessionCount

func (l *Listener) SessionCount() int

type Mode

type Mode int

type Output

type Output func(buf []byte, size int)

Output In general, Output is a closure which captures conn and calls conn.Write

type Segment

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

Segment KCP Segment Definition

func NewSegment

func NewSegment(size int) *Segment

type UDPSession

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

func Dial

func Dial(mode Mode, raddr string) (*UDPSession, error)

Dial connects to the remote address raddr on the network "udp", mode is same as Listen

func DialEncrypted

func DialEncrypted(mode Mode, raddr string, key string) (*UDPSession, error)

Dial connects to the remote address raddr on the network "udp" with packet encryption, mode is same as Listen

func (*UDPSession) Close

func (s *UDPSession) Close() error

Close closes the connection.

func (*UDPSession) GetConv

func (s *UDPSession) GetConv() uint32

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) 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) SetReadDeadline

func (s *UDPSession) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*UDPSession) SetWindowSize

func (s *UDPSession) SetWindowSize(sndwnd, rcvwnd int)

SetWindowSize set maximum window size

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.

Jump to

Keyboard shortcuts

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