kcp

package
v1.24.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2016 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package kcp - A Fast and Reliable ARQ Protocol

Acknowledgement:

skywind3000@github for inventing the KCP protocol
xtaci@github for translating to Golang

Index

Constants

View Source
const (
	NumDistro  = 5
	DistroSize = 1600
)
View Source
const (
	DataSegmentOverhead = 18
)

Variables

View Source
var (
	ErrIOTimeout        = errors.New("Read/Write timeout")
	ErrClosedListener   = errors.New("Listener closed.")
	ErrClosedConnection = errors.New("Connection closed.")
)

Functions

func AllocateBuffer added in v1.23.1

func AllocateBuffer() *alloc.Buffer

func DialKCP

func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, error)

func ListenKCP

func ListenKCP(address v2net.Address, port v2net.Port) (internet.Listener, error)

func NewSimpleAuthenticator

func NewSimpleAuthenticator() internet.Authenticator

Types

type AckList added in v1.18.1

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

func NewAckList added in v1.18.1

func NewAckList(writer SegmentWriter) *AckList

func (*AckList) Add added in v1.18.1

func (this *AckList) Add(number uint32, timestamp uint32)

func (*AckList) Clear added in v1.18.1

func (this *AckList) Clear(una uint32)

func (*AckList) Flush added in v1.18.1

func (this *AckList) Flush(current uint32, rto uint32)

type AckSegment added in v1.18.1

type AckSegment struct {
	Conv            uint16
	Option          SegmentOption
	ReceivingWindow uint32
	ReceivingNext   uint32
	Timestamp       uint32
	Count           byte
	NumberList      []uint32
}

func NewAckSegment added in v1.19.1

func NewAckSegment() *AckSegment

func (*AckSegment) ByteSize added in v1.18.1

func (this *AckSegment) ByteSize() int

func (*AckSegment) Bytes added in v1.18.1

func (this *AckSegment) Bytes(b []byte) []byte

func (*AckSegment) IsFull added in v1.19.1

func (this *AckSegment) IsFull() bool

func (*AckSegment) PutNumber added in v1.19.1

func (this *AckSegment) PutNumber(number uint32)

func (*AckSegment) PutTimestamp added in v1.23.1

func (this *AckSegment) PutTimestamp(timestamp uint32)

func (*AckSegment) Release added in v1.18.1

func (this *AckSegment) Release()

type AuthenticationWriter added in v1.18.1

type AuthenticationWriter struct {
	Authenticator internet.Authenticator
	Writer        io.Writer
}

func (*AuthenticationWriter) Mtu added in v1.18.1

func (this *AuthenticationWriter) Mtu() uint32

func (*AuthenticationWriter) Release added in v1.18.1

func (this *AuthenticationWriter) Release()

func (*AuthenticationWriter) Write added in v1.18.1

func (this *AuthenticationWriter) Write(payload *alloc.Buffer) error

type Buffer added in v1.23.1

type Buffer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewBuffer added in v1.23.1

func NewBuffer() *Buffer

func (*Buffer) Allocate added in v1.23.1

func (this *Buffer) Allocate() *alloc.Buffer

func (*Buffer) Free added in v1.23.1

func (this *Buffer) Free(b *alloc.Buffer)

func (*Buffer) IsEmpty added in v1.23.1

func (this *Buffer) IsEmpty() bool

func (*Buffer) Release added in v1.23.1

func (this *Buffer) Release()

func (*Buffer) ReleaseBuffer added in v1.23.1

func (this *Buffer) ReleaseBuffer()

type BufferedSegmentWriter added in v1.18.1

type BufferedSegmentWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewSegmentWriter added in v1.18.1

func NewSegmentWriter(writer *AuthenticationWriter) *BufferedSegmentWriter

func (*BufferedSegmentWriter) Flush added in v1.18.1

func (this *BufferedSegmentWriter) Flush()

func (*BufferedSegmentWriter) FlushWithoutLock added in v1.18.1

func (this *BufferedSegmentWriter) FlushWithoutLock()

func (*BufferedSegmentWriter) Write added in v1.18.1

func (this *BufferedSegmentWriter) Write(seg Segment)

type CmdOnlySegment added in v1.18.1

type CmdOnlySegment struct {
	Conv         uint16
	Command      Command
	Option       SegmentOption
	SendingNext  uint32
	ReceivinNext uint32
	PeerRTO      uint32
}

func NewCmdOnlySegment added in v1.19.1

func NewCmdOnlySegment() *CmdOnlySegment

func (*CmdOnlySegment) ByteSize added in v1.18.1

func (this *CmdOnlySegment) ByteSize() int

func (*CmdOnlySegment) Bytes added in v1.18.1

func (this *CmdOnlySegment) Bytes(b []byte) []byte

func (*CmdOnlySegment) Release added in v1.18.1

func (this *CmdOnlySegment) Release()

type Command

type Command byte
const (
	CommandACK       Command = 0
	CommandData      Command = 1
	CommandTerminate Command = 2
	CommandPing      Command = 3
)

type Config

type Config struct {
	Mtu              uint32 // Maximum transmission unit
	Tti              uint32
	UplinkCapacity   uint32
	DownlinkCapacity uint32
	Congestion       bool
	WriteBuffer      uint32
	ReadBuffer       uint32
	HeaderType       string
	HeaderConfig     internet.AuthenticatorConfig
}

func DefaultConfig

func DefaultConfig() Config

func (*Config) Apply

func (this *Config) Apply()

func (*Config) GetAuthenticator added in v1.23.1

func (this *Config) GetAuthenticator() (internet.Authenticator, error)

func (*Config) GetReceivingQueueSize added in v1.18.1

func (this *Config) GetReceivingQueueSize() uint32

func (*Config) GetReceivingWindowSize

func (this *Config) GetReceivingWindowSize() uint32

func (*Config) GetSendingInFlightSize added in v1.18.2

func (this *Config) GetSendingInFlightSize() uint32

func (*Config) GetSendingQueueSize added in v1.18.1

func (this *Config) GetSendingQueueSize() uint32

func (*Config) GetSendingWindowSize

func (this *Config) GetSendingWindowSize() uint32

type Connection

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

Connection is a KCP connection over UDP.

func NewConnection

func NewConnection(conv uint16, writerCloser io.WriteCloser, local *net.UDPAddr, remote *net.UDPAddr, block internet.Authenticator) *Connection

NewConnection create a new KCP connection between local and remote.

func (*Connection) Close

func (this *Connection) Close() error

Close closes the connection.

func (*Connection) Elapsed

func (this *Connection) Elapsed() uint32

func (*Connection) FetchInputFrom

func (this *Connection) FetchInputFrom(conn io.Reader)

func (*Connection) HandleOption added in v1.19.1

func (this *Connection) HandleOption(opt SegmentOption)

func (*Connection) Input added in v1.19.1

func (this *Connection) Input(data []byte) int

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

func (*Connection) LocalAddr

func (this *Connection) 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 (*Connection) OnPeerClosed added in v1.19.1

func (this *Connection) OnPeerClosed()

func (*Connection) Read

func (this *Connection) Read(b []byte) (int, error)

Read implements the Conn Read method.

func (*Connection) RemoteAddr

func (this *Connection) 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 (*Connection) Reusable

func (this *Connection) Reusable() bool

func (*Connection) SetDeadline

func (this *Connection) SetDeadline(t time.Time) error

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

func (*Connection) SetReadDeadline

func (this *Connection) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*Connection) SetReusable

func (this *Connection) SetReusable(b bool)

func (*Connection) SetState added in v1.19.1

func (this *Connection) SetState(state State)

func (*Connection) SetWriteDeadline

func (this *Connection) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (*Connection) State added in v1.19.1

func (this *Connection) State() State

func (*Connection) Terminate

func (this *Connection) Terminate()

func (*Connection) Write

func (this *Connection) Write(b []byte) (int, error)

Write implements the Conn Write method.

type DataSegment added in v1.18.1

type DataSegment struct {
	Conv        uint16
	Option      SegmentOption
	Timestamp   uint32
	Number      uint32
	SendingNext uint32
	Data        *alloc.Buffer
	// contains filtered or unexported fields
}

func NewDataSegment added in v1.19.1

func NewDataSegment() *DataSegment

func (*DataSegment) ByteSize added in v1.18.1

func (this *DataSegment) ByteSize() int

func (*DataSegment) Bytes added in v1.18.1

func (this *DataSegment) Bytes(b []byte) []byte

func (*DataSegment) Release added in v1.18.1

func (this *DataSegment) Release()

type Listener

type Listener struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Listener defines a server listening for connections

func NewListener

func NewListener(address v2net.Address, port v2net.Port) (*Listener, error)

func (*Listener) Accept

func (this *Listener) Accept() (internet.Connection, error)

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

func (*Listener) ActiveConnections added in v1.19.2

func (this *Listener) ActiveConnections() int

func (*Listener) Addr

func (this *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 (this *Listener) Close() error

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

func (*Listener) OnReceive

func (this *Listener) OnReceive(payload *alloc.Buffer, src v2net.Destination)

func (*Listener) Remove

func (this *Listener) Remove(dest string)

type ReceivingQueue added in v1.18.1

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

func NewReceivingQueue added in v1.18.1

func NewReceivingQueue(size uint32) *ReceivingQueue

func (*ReceivingQueue) Close added in v1.18.1

func (this *ReceivingQueue) Close()

func (*ReceivingQueue) IsEmpty added in v1.19.1

func (this *ReceivingQueue) IsEmpty() bool

func (*ReceivingQueue) IsFull added in v1.19.1

func (this *ReceivingQueue) IsFull() bool

func (*ReceivingQueue) Put added in v1.18.1

func (this *ReceivingQueue) Put(payload *alloc.Buffer)

func (*ReceivingQueue) Read added in v1.18.1

func (this *ReceivingQueue) Read(buf []byte) int

type ReceivingWindow added in v1.17.3

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

func NewReceivingWindow added in v1.17.3

func NewReceivingWindow(size uint32) *ReceivingWindow

func (*ReceivingWindow) Advance added in v1.17.3

func (this *ReceivingWindow) Advance()

func (*ReceivingWindow) Position added in v1.17.3

func (this *ReceivingWindow) Position(idx uint32) uint32

func (*ReceivingWindow) Remove added in v1.17.3

func (this *ReceivingWindow) Remove(idx uint32) *DataSegment

func (*ReceivingWindow) RemoveFirst added in v1.17.3

func (this *ReceivingWindow) RemoveFirst() *DataSegment

func (*ReceivingWindow) Set added in v1.17.3

func (this *ReceivingWindow) Set(idx uint32, value *DataSegment) bool

func (*ReceivingWindow) Size added in v1.17.3

func (this *ReceivingWindow) Size() uint32

type ReceivingWorker added in v1.18.1

type ReceivingWorker struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewReceivingWorker added in v1.18.1

func NewReceivingWorker(kcp *Connection) *ReceivingWorker

func (*ReceivingWorker) CloseRead added in v1.18.1

func (this *ReceivingWorker) CloseRead()

func (*ReceivingWorker) Flush added in v1.18.1

func (this *ReceivingWorker) Flush(current uint32)

func (*ReceivingWorker) MarkPingNecessary added in v1.19.1

func (this *ReceivingWorker) MarkPingNecessary(b bool)

func (*ReceivingWorker) PingNecessary added in v1.18.1

func (this *ReceivingWorker) PingNecessary() bool

func (*ReceivingWorker) ProcessSegment added in v1.18.1

func (this *ReceivingWorker) ProcessSegment(seg *DataSegment)

func (*ReceivingWorker) ProcessSendingNext added in v1.18.1

func (this *ReceivingWorker) ProcessSendingNext(number uint32)

func (*ReceivingWorker) Read added in v1.18.1

func (this *ReceivingWorker) Read(b []byte) int

func (*ReceivingWorker) Write added in v1.18.1

func (this *ReceivingWorker) Write(seg Segment)

type RoundTripInfo added in v1.23.1

type RoundTripInfo struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*RoundTripInfo) SmoothedTime added in v1.23.1

func (this *RoundTripInfo) SmoothedTime() uint32

func (*RoundTripInfo) Timeout added in v1.23.1

func (this *RoundTripInfo) Timeout() uint32

func (*RoundTripInfo) Update added in v1.23.1

func (this *RoundTripInfo) Update(rtt uint32, current uint32)

func (*RoundTripInfo) UpdatePeerRTO added in v1.23.1

func (this *RoundTripInfo) UpdatePeerRTO(rto uint32, current uint32)

type Segment

type Segment interface {
	common.Releasable
	ByteSize() int
	Bytes([]byte) []byte
}

func ReadSegment added in v1.18.1

func ReadSegment(buf []byte) (Segment, []byte)

type SegmentOption added in v1.18.1

type SegmentOption byte
const (
	SegmentOptionClose SegmentOption = 1
)

type SegmentWriter added in v1.18.1

type SegmentWriter interface {
	Write(seg Segment)
}

type SendingQueue added in v1.17.3

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

func NewSendingQueue added in v1.17.3

func NewSendingQueue(size uint32) *SendingQueue

func (*SendingQueue) Clear added in v1.17.3

func (this *SendingQueue) Clear()

func (*SendingQueue) IsEmpty added in v1.17.3

func (this *SendingQueue) IsEmpty() bool

func (*SendingQueue) IsFull added in v1.17.3

func (this *SendingQueue) IsFull() bool

func (*SendingQueue) Last added in v1.23.1

func (this *SendingQueue) Last() *alloc.Buffer

func (*SendingQueue) Len added in v1.17.3

func (this *SendingQueue) Len() uint32

func (*SendingQueue) Pop added in v1.17.3

func (this *SendingQueue) Pop() *alloc.Buffer

func (*SendingQueue) Push added in v1.17.3

func (this *SendingQueue) Push(seg *alloc.Buffer)

type SendingWindow added in v1.18.1

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

func NewSendingWindow added in v1.18.1

func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow

func (*SendingWindow) Clear added in v1.18.1

func (this *SendingWindow) Clear(una uint32)

func (*SendingWindow) First added in v1.18.1

func (this *SendingWindow) First() *DataSegment

func (*SendingWindow) Flush added in v1.18.1

func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32, maxInFlightSize uint32)

func (*SendingWindow) HandleFastAck added in v1.18.1

func (this *SendingWindow) HandleFastAck(number uint32)

func (*SendingWindow) IsEmpty added in v1.19.2

func (this *SendingWindow) IsEmpty() bool

func (*SendingWindow) IsFull added in v1.18.2

func (this *SendingWindow) IsFull() bool

func (*SendingWindow) Len added in v1.18.1

func (this *SendingWindow) Len() int

func (*SendingWindow) Push added in v1.18.1

func (this *SendingWindow) Push(seg *DataSegment)

func (*SendingWindow) Remove added in v1.18.1

func (this *SendingWindow) Remove(idx uint32)

func (*SendingWindow) Size added in v1.18.2

func (this *SendingWindow) Size() uint32

type SendingWorker added in v1.18.1

type SendingWorker struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewSendingWorker added in v1.18.1

func NewSendingWorker(kcp *Connection) *SendingWorker

func (*SendingWorker) CloseWrite added in v1.18.1

func (this *SendingWorker) CloseWrite()

func (*SendingWorker) FillWindow added in v1.21.1

func (this *SendingWorker) FillWindow(current uint32)

func (*SendingWorker) FindFirstUnacknowledged added in v1.18.1

func (this *SendingWorker) FindFirstUnacknowledged()

@Private

func (*SendingWorker) Flush added in v1.18.1

func (this *SendingWorker) Flush(current uint32)

func (*SendingWorker) IsEmpty added in v1.19.2

func (this *SendingWorker) IsEmpty() bool

func (*SendingWorker) MarkPingNecessary added in v1.19.1

func (this *SendingWorker) MarkPingNecessary(b bool)

func (*SendingWorker) OnPacketLoss added in v1.18.1

func (this *SendingWorker) OnPacketLoss(lossRate uint32)

func (*SendingWorker) PingNecessary added in v1.18.1

func (this *SendingWorker) PingNecessary() bool

func (*SendingWorker) ProcessAck added in v1.18.1

func (this *SendingWorker) ProcessAck(number uint32)

@Private

func (*SendingWorker) ProcessReceivingNext added in v1.18.1

func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32)

func (*SendingWorker) ProcessReceivingNextWithoutLock added in v1.19.1

func (this *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32)

func (*SendingWorker) ProcessSegment added in v1.18.2

func (this *SendingWorker) ProcessSegment(current uint32, seg *AckSegment)

func (*SendingWorker) Push added in v1.18.1

func (this *SendingWorker) Push(b []byte) int

func (*SendingWorker) Write added in v1.18.1

func (this *SendingWorker) Write(seg Segment)

@Private

type SimpleAuthenticator

type SimpleAuthenticator struct{}

func (*SimpleAuthenticator) Open

func (this *SimpleAuthenticator) Open(buffer *alloc.Buffer) bool

func (*SimpleAuthenticator) Overhead added in v1.23.1

func (this *SimpleAuthenticator) Overhead() int

func (*SimpleAuthenticator) Seal

func (this *SimpleAuthenticator) Seal(buffer *alloc.Buffer)

type State added in v1.18.1

type State int32
const (
	StateActive          State = 0
	StateReadyToClose    State = 1
	StatePeerClosed      State = 2
	StateTerminating     State = 3
	StatePeerTerminating State = 4
	StateTerminated      State = 5
)

func (State) Is added in v1.21.2

func (this State) Is(states ...State) bool

type Writer

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

func (*Writer) Close

func (this *Writer) Close() error

func (*Writer) Write

func (this *Writer) Write(payload []byte) (int, error)

Jump to

Keyboard shortcuts

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