kcp

package
v1.18.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2016 License: MIT Imports: 15 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 (
	DataSegmentOverhead = 18
)

Variables

This section is empty.

Functions

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)

Types

type AckList

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

func NewAckList

func NewAckList(writer SegmentWriter) *AckList

func (*AckList) Add

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

func (*AckList) Clear

func (this *AckList) Clear(una uint32)

func (*AckList) Flush

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

type AckSegment

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

func (*AckSegment) ByteSize

func (this *AckSegment) ByteSize() int

func (*AckSegment) Bytes

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

func (*AckSegment) Release

func (this *AckSegment) Release()

type AuthenticationWriter

type AuthenticationWriter struct {
	Authenticator Authenticator
	Writer        io.Writer
}

func (*AuthenticationWriter) Mtu

func (this *AuthenticationWriter) Mtu() uint32

func (*AuthenticationWriter) Release

func (this *AuthenticationWriter) Release()

func (*AuthenticationWriter) Write

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

type Authenticator

type Authenticator interface {
	HeaderSize() int
	// Encrypt encrypts the whole block in src into dst.
	// Dst and src may point at the same memory.
	Seal(buffer *alloc.Buffer)

	// Decrypt decrypts the whole block in src into dst.
	// Dst and src may point at the same memory.
	Open(buffer *alloc.Buffer) bool
}

func NewSimpleAuthenticator

func NewSimpleAuthenticator() Authenticator

type BufferedSegmentWriter

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

func NewSegmentWriter

func NewSegmentWriter(writer *AuthenticationWriter) *BufferedSegmentWriter

func (*BufferedSegmentWriter) Flush

func (this *BufferedSegmentWriter) Flush()

func (*BufferedSegmentWriter) FlushWithoutLock

func (this *BufferedSegmentWriter) FlushWithoutLock()

func (*BufferedSegmentWriter) Write

func (this *BufferedSegmentWriter) Write(seg ISegment)

type CmdOnlySegment

type CmdOnlySegment struct {
	Conv         uint16
	Cmd          SegmentCommand
	Opt          SegmentOption
	SendingNext  uint32
	ReceivinNext uint32
}

func (*CmdOnlySegment) ByteSize

func (this *CmdOnlySegment) ByteSize() int

func (*CmdOnlySegment) Bytes

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

func (*CmdOnlySegment) Release

func (this *CmdOnlySegment) Release()

type Config

type Config struct {
	Mtu              uint32 // Maximum transmission unit
	Tti              uint32
	UplinkCapacity   uint32
	DownlinkCapacity uint32
	Congestion       bool
	WriteBuffer      uint32
	ReadBuffer       uint32
}

func DefaultConfig

func DefaultConfig() Config

func (*Config) Apply

func (this *Config) Apply()

func (*Config) GetReceivingQueueSize

func (this *Config) GetReceivingQueueSize() uint32

func (*Config) GetReceivingWindowSize

func (this *Config) GetReceivingWindowSize() uint32

func (*Config) GetSendingQueueSize

func (this *Config) GetSendingQueueSize() uint32

func (*Config) GetSendingWindowSize

func (this *Config) GetSendingWindowSize() uint32

type Connection

type Connection struct {
	sync.RWMutex
	// 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 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 net.Conn)

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

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

SetWriteDeadline implements the Conn SetWriteDeadline method.

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

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

func (*DataSegment) ByteSize

func (this *DataSegment) ByteSize() int

func (*DataSegment) Bytes

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

func (*DataSegment) Release

func (this *DataSegment) Release()

type ISegment

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

func ReadSegment

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

type KCP

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

KCP defines a single KCP connection

func NewKCP

func NewKCP(conv uint16, output *AuthenticationWriter) *KCP

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

func (*KCP) HandleOption

func (kcp *KCP) HandleOption(opt SegmentOption)

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

func (kcp *KCP) OnClose()

func (*KCP) OnPeerClosed

func (kcp *KCP) OnPeerClosed()

func (*KCP) SetState

func (kcp *KCP) SetState(state State)

func (*KCP) Update

func (kcp *KCP) Update(current uint32)

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.

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

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

func NewReceivingQueue

func NewReceivingQueue(size uint32) *ReceivingQueue

func (*ReceivingQueue) Close

func (this *ReceivingQueue) Close()

func (*ReceivingQueue) Put

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

func (*ReceivingQueue) Read

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

func (*ReceivingQueue) SetReadDeadline

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

type ReceivingWindow

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

func NewReceivingWindow

func NewReceivingWindow(size uint32) *ReceivingWindow

func (*ReceivingWindow) Advance

func (this *ReceivingWindow) Advance()

func (*ReceivingWindow) Position

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

func (*ReceivingWindow) Remove

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

func (*ReceivingWindow) RemoveFirst

func (this *ReceivingWindow) RemoveFirst() *DataSegment

func (*ReceivingWindow) Set

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

func (*ReceivingWindow) Size

func (this *ReceivingWindow) Size() uint32

type ReceivingWorker

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

func NewReceivingWorker

func NewReceivingWorker(kcp *KCP) *ReceivingWorker

func (*ReceivingWorker) CloseRead

func (this *ReceivingWorker) CloseRead()

func (*ReceivingWorker) Flush

func (this *ReceivingWorker) Flush()

func (*ReceivingWorker) PingNecessary

func (this *ReceivingWorker) PingNecessary() bool

func (*ReceivingWorker) ProcessSegment

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

func (*ReceivingWorker) ProcessSendingNext

func (this *ReceivingWorker) ProcessSendingNext(number uint32)

func (*ReceivingWorker) Read

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

func (*ReceivingWorker) SetReadDeadline

func (this *ReceivingWorker) SetReadDeadline(t time.Time)

func (*ReceivingWorker) Write

func (this *ReceivingWorker) Write(seg ISegment)

type SegmentCommand

type SegmentCommand byte
const (
	SegmentCommandACK        SegmentCommand = 0
	SegmentCommandData       SegmentCommand = 1
	SegmentCommandTerminated SegmentCommand = 2
	SegmentCommandPing       SegmentCommand = 3
)

type SegmentOption

type SegmentOption byte
const (
	SegmentOptionClose SegmentOption = 1
)

type SegmentWriter

type SegmentWriter interface {
	Write(seg ISegment)
}

type SendingQueue

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

func NewSendingQueue

func NewSendingQueue(size uint32) *SendingQueue

func (*SendingQueue) Clear

func (this *SendingQueue) Clear()

func (*SendingQueue) IsEmpty

func (this *SendingQueue) IsEmpty() bool

func (*SendingQueue) IsFull

func (this *SendingQueue) IsFull() bool

func (*SendingQueue) Len

func (this *SendingQueue) Len() uint32

func (*SendingQueue) Pop

func (this *SendingQueue) Pop() *DataSegment

func (*SendingQueue) Push

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

type SendingWindow

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

func NewSendingWindow

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

func (*SendingWindow) Clear

func (this *SendingWindow) Clear(una uint32)

func (*SendingWindow) First

func (this *SendingWindow) First() *DataSegment

func (*SendingWindow) Flush

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

func (*SendingWindow) HandleFastAck

func (this *SendingWindow) HandleFastAck(number uint32)

func (*SendingWindow) Len

func (this *SendingWindow) Len() int

func (*SendingWindow) Push

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

func (*SendingWindow) Remove

func (this *SendingWindow) Remove(idx uint32)

type SendingWorker

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

func NewSendingWorker

func NewSendingWorker(kcp *KCP) *SendingWorker

func (*SendingWorker) CloseWrite

func (this *SendingWorker) CloseWrite()

func (*SendingWorker) FindFirstUnacknowledged

func (this *SendingWorker) FindFirstUnacknowledged()

@Private

func (*SendingWorker) Flush

func (this *SendingWorker) Flush()

func (*SendingWorker) OnPacketLoss

func (this *SendingWorker) OnPacketLoss(lost bool)

func (*SendingWorker) PingNecessary

func (this *SendingWorker) PingNecessary() bool

func (*SendingWorker) ProcessAck

func (this *SendingWorker) ProcessAck(number uint32)

func (*SendingWorker) ProcessAckSegment

func (this *SendingWorker) ProcessAckSegment(seg *AckSegment)

func (*SendingWorker) ProcessReceivingNext

func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32)

func (*SendingWorker) Push

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

func (*SendingWorker) Write

func (this *SendingWorker) Write(seg ISegment)

type SimpleAuthenticator

type SimpleAuthenticator struct{}

func (*SimpleAuthenticator) HeaderSize

func (this *SimpleAuthenticator) HeaderSize() int

func (*SimpleAuthenticator) Open

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

func (*SimpleAuthenticator) Seal

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

type State

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

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