kcp

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

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

View Source
const (
	ConnEnetSyn = "ConnEnetSyn"
	ConnEnetEst = "ConnEnetEst"
	ConnEnetFin = "ConnEnetFin"
)

Enet连接状态类型

View Source
const (
	EnetTimeout                = 0
	EnetClientClose            = 1
	EnetClientRebindFail       = 2
	EnetClientShutdown         = 3
	EnetServerRelogin          = 4
	EnetServerKick             = 5
	EnetServerShutdown         = 6
	EnetNotFoundSession        = 7
	EnetLoginUnfinished        = 8
	EnetPacketFreqTooHigh      = 9
	EnetPingTimeout            = 10
	EnetTransferFailed         = 11
	EnetServerKillClient       = 12
	EnetCheckMoveSpeed         = 13
	EnetAccountPasswordChange  = 14
	EnetSecurityKick           = 15
	EnetLuaShellTimeout        = 16
	EnetSDKFailKick            = 17
	EnetPacketCostTime         = 18
	EnetPacketUnionFreq        = 19
	EnetWaitSndMax             = 20
	EnetClientEditorConnectKey = 987654321
	EnetClientConnectKey       = 1234567890
)

Enet事件类型

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 + 4 + 4 // KCP组合会话id是8个字节
	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
	IKCP_SN_OFFSET   = 12 + 4 // KCP组合会话id是8个字节
)

Variables

View Source
var ErrConnAlreadyClose = errors.New("conn already close")
View Source
var MagicEnetEstHead, _ = hex.DecodeString("00000145")
View Source
var MagicEnetEstTail, _ = hex.DecodeString("14514545")
View Source
var MagicEnetFinHead, _ = hex.DecodeString("00000194")
View Source
var MagicEnetFinTail, _ = hex.DecodeString("19419494")
View Source
var MagicEnetSynHead, _ = hex.DecodeString("000000ff")

Enet连接状态类型幻数

View Source
var MagicEnetSynTail, _ = hex.DecodeString("ffffffff")
View Source
var SystemTimedSched = NewTimedSched(runtime.NumCPU())

SystemTimedSched is the library level timed-scheduler

Functions

func BuildEnet

func BuildEnet(connType string, enetType uint32, sessionId uint32, conv uint32) []byte

func ParseEnet

func ParseEnet(data []byte) (connType string, enetType uint32, sessionId uint32, conv uint32, rawConv uint64, err error)

Types

type Conn added in v1.0.1

type Conn struct {
	RxChan  chan []byte
	TxChan  chan []byte
	IsClose bool
}

func (*Conn) Close added in v1.0.1

func (c *Conn) Close() error

func (*Conn) ReadFrom added in v1.0.1

func (c *Conn) ReadFrom(buf []byte) (int, error)

func (*Conn) WriteTo added in v1.0.1

func (c *Conn) WriteTo(buf []byte) (int, error)

type Enet

type Enet struct {
	SessionId uint32
	Conv      uint32
	ConnType  string
	EnetType  uint32
}

Enet Enet协议上报结构体

type KCP

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

KCP defines a single KCP connection

func NewKCP

func NewKCP(conv uint64, output output_callback) *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) Check

func (kcp *KCP) Check() uint32

(deprecated)

Check determines 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, regular, ackNoDelay bool) int

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

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

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

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

PeekSize checks the size of next message in the recv queue

func (*KCP) Recv

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

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

func (kcp *KCP) ReleaseTX()

Release all cached outgoing segments

func (*KCP) ReserveBytes

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

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

func (*KCP) Send

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

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

func (*KCP) SetMtu

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

SetMtu changes MTU size, default is 1400

func (*KCP) Update

func (kcp *KCP) Update()

(deprecated)

Update updates 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 gets how many packet is waiting to be sent

func (*KCP) WndSize

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

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

type Listener

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

Listener defines a server which will be waiting to accept incoming connections

func Listen added in v1.0.1

func Listen(conn *Conn) (*Listener, error)

Listen 监听

func (*Listener) AcceptKCP

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

AcceptKCP accepts a KCP connection

func (*Listener) Close

func (l *Listener) Close() error

Close stops listening on the UDP address, and closes the socket

func (*Listener) GetEnetNotifyChan added in v1.0.1

func (l *Listener) GetEnetNotifyChan() chan *Enet

func (*Listener) SendEnetNotifyToPeer

func (l *Listener) SendEnetNotifyToPeer(enet *Enet)

func (*Listener) SetDeadline

func (l *Listener) SetDeadline(t time.Time) error

SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.

func (*Listener) SetReadDeadline

func (l *Listener) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*Listener) SetWriteDeadline

func (l *Listener) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

type Message

type Message struct {
	Buffers [][]byte
}

type TimedSched

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

TimedSched represents the control struct for timed parallel scheduler

func NewTimedSched

func NewTimedSched(parallel int) *TimedSched

NewTimedSched creates a parallel-scheduler with given parallelization

func (*TimedSched) Close

func (ts *TimedSched) Close()

Close terminates this scheduler

func (*TimedSched) Put

func (ts *TimedSched) Put(f func(), deadline time.Time)

Put a function 'f' awaiting to be executed at 'deadline'

type UDPSession

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

UDPSession defines a KCP session implemented by UDP

func Dial added in v1.0.1

func Dial(conn *Conn) (*UDPSession, error)

Dial 连接

func (*UDPSession) Close

func (s *UDPSession) Close() error

Close closes the connection.

func (*UDPSession) GetConv

func (s *UDPSession) GetConv() uint32

GetConv 获取KCP会话id

func (*UDPSession) GetMaxPayloadLen added in v1.0.1

func (s *UDPSession) GetMaxPayloadLen() int

GetMaxPayloadLen 获取应用层最大包长度

func (*UDPSession) GetRTO

func (s *UDPSession) GetRTO() uint32

GetRTO gets current rto of the session

func (*UDPSession) GetRawConv

func (s *UDPSession) GetRawConv() uint64

GetRawConv gets conversation id of a session 获取KCP组合会话id

func (*UDPSession) GetSRTT

func (s *UDPSession) GetSRTT() int32

GetSRTT gets current srtt of the session

func (*UDPSession) GetSRTTVar added in v1.0.1

func (s *UDPSession) GetSRTTVar() int32

GetSRTTVar gets current rtt variance of the session

func (*UDPSession) GetSessionId

func (s *UDPSession) GetSessionId() uint32

GetSessionId 获取会话id

func (*UDPSession) Read

func (s *UDPSession) Read(b []byte) (n int, err error)

Read implements net.Conn

func (*UDPSession) SendEnetNotifyToPeer

func (s *UDPSession) SendEnetNotifyToPeer(enet *Enet)

func (*UDPSession) SetACKNoDelay

func (s *UDPSession) SetACKNoDelay(nodelay bool)

SetACKNoDelay changes ack flush option, set true to flush ack immediately,

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

Jump to

Keyboard shortcuts

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