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
- func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, error)
- func ListenKCP(address v2net.Address, port v2net.Port) (internet.Listener, error)
- type AckList
- type AckSegment
- type AuthenticationWriter
- type Authenticator
- type BufferedSegmentWriter
- type CmdOnlySegment
- type Config
- type Connection
- func (this *Connection) Close() error
- func (this *Connection) Elapsed() uint32
- func (this *Connection) FetchInputFrom(conn net.Conn)
- func (this *Connection) LocalAddr() net.Addr
- func (this *Connection) Read(b []byte) (int, error)
- func (this *Connection) RemoteAddr() net.Addr
- func (this *Connection) Reusable() bool
- func (this *Connection) SetDeadline(t time.Time) error
- func (this *Connection) SetReadDeadline(t time.Time) error
- func (this *Connection) SetReusable(b bool)
- func (this *Connection) SetWriteDeadline(t time.Time) error
- func (this *Connection) Terminate()
- func (this *Connection) Write(b []byte) (int, error)
- type DataSegment
- type KCP
- type Listener
- type ReceivingQueue
- type ReceivingWindow
- func (this *ReceivingWindow) Advance()
- func (this *ReceivingWindow) Position(idx uint32) uint32
- func (this *ReceivingWindow) Remove(idx uint32) *DataSegment
- func (this *ReceivingWindow) RemoveFirst() *DataSegment
- func (this *ReceivingWindow) Set(idx uint32, value *DataSegment) bool
- func (this *ReceivingWindow) Size() uint32
- type ReceivingWorker
- func (this *ReceivingWorker) CloseRead()
- func (this *ReceivingWorker) Flush()
- func (this *ReceivingWorker) PingNecessary() bool
- func (this *ReceivingWorker) ProcessSegment(seg *DataSegment)
- func (this *ReceivingWorker) ProcessSendingNext(number uint32)
- func (this *ReceivingWorker) Read(b []byte) (int, error)
- func (this *ReceivingWorker) SetReadDeadline(t time.Time)
- func (this *ReceivingWorker) Write(seg Segment)
- type Segment
- type SegmentCommand
- type SegmentOption
- type SegmentWriter
- type SendingQueue
- type SendingWindow
- func (this *SendingWindow) Clear(una uint32)
- func (this *SendingWindow) First() *DataSegment
- func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32, maxInFlightSize uint32)
- func (this *SendingWindow) HandleFastAck(number uint32)
- func (this *SendingWindow) IsFull() bool
- func (this *SendingWindow) Len() int
- func (this *SendingWindow) Push(seg *DataSegment)
- func (this *SendingWindow) Remove(idx uint32)
- func (this *SendingWindow) Size() uint32
- type SendingWorker
- func (this *SendingWorker) CloseWrite()
- func (this *SendingWorker) FindFirstUnacknowledged()
- func (this *SendingWorker) Flush()
- func (this *SendingWorker) OnPacketLoss(lossRate uint32)
- func (this *SendingWorker) PingNecessary() bool
- func (this *SendingWorker) ProcessAck(number uint32)
- func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32)
- func (this *SendingWorker) ProcessSegment(seg *AckSegment)
- func (this *SendingWorker) Push(b []byte) int
- func (this *SendingWorker) Write(seg Segment)
- type SimpleAuthenticator
- type State
- type Writer
Constants ¶
const (
DataSegmentOverhead = 18
)
Variables ¶
This section is empty.
Functions ¶
func DialKCP ¶
func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, error)
Types ¶
type AckList ¶ added in v1.18.1
func NewAckList ¶ added in v1.18.1
func NewAckList(writer SegmentWriter) *AckList
type AckSegment ¶ added in v1.18.1
type AckSegment struct { Conv uint16 Opt SegmentOption ReceivingWindow uint32 ReceivingNext uint32 Count byte NumberList []uint32 TimestampList []uint32 }
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) Release ¶ added in v1.18.1
func (this *AckSegment) Release()
type AuthenticationWriter ¶ added in v1.18.1
type AuthenticationWriter struct { Authenticator 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()
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 ¶ added in v1.18.1
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 Cmd SegmentCommand Opt SegmentOption SendingNext uint32 ReceivinNext uint32 }
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 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) GetReceivingQueueSize ¶ added in v1.18.1
func (*Config) GetReceivingWindowSize ¶
func (*Config) GetSendingInFlightSize ¶ added in v1.18.2
func (*Config) GetSendingQueueSize ¶ added in v1.18.1
func (*Config) GetSendingWindowSize ¶
type Connection ¶
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) 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()
type DataSegment ¶ added in v1.18.1
type DataSegment struct { Conv uint16 Opt SegmentOption Timestamp uint32 Number uint32 SendingNext uint32 Data *alloc.Buffer // contains filtered or unexported fields }
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 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 ¶ added in v1.18.1
func (kcp *KCP) HandleOption(opt SegmentOption)
func (*KCP) OnPeerClosed ¶ added in v1.18.1
func (kcp *KCP) OnPeerClosed()
type Listener ¶
Listener defines a server listening for connections
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 ¶
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 ¶
Close stops listening on the UDP address. Already Accepted connections are not closed.
type ReceivingQueue ¶ added in v1.18.1
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) Put ¶ added in v1.18.1
func (this *ReceivingQueue) Put(payload *alloc.Buffer) bool
func (*ReceivingQueue) Read ¶ added in v1.18.1
func (this *ReceivingQueue) Read(buf []byte) (int, error)
func (*ReceivingQueue) SetReadDeadline ¶ added in v1.18.1
func (this *ReceivingQueue) SetReadDeadline(t time.Time) error
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 {
// contains filtered or unexported fields
}
func NewReceivingWorker ¶ added in v1.18.1
func NewReceivingWorker(kcp *KCP) *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()
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, error)
func (*ReceivingWorker) SetReadDeadline ¶ added in v1.18.1
func (this *ReceivingWorker) SetReadDeadline(t time.Time)
func (*ReceivingWorker) Write ¶ added in v1.18.1
func (this *ReceivingWorker) Write(seg Segment)
type Segment ¶
type Segment interface { common.Releasable ByteSize() int Bytes([]byte) []byte }
func ReadSegment ¶ added in v1.18.1
type SegmentCommand ¶ added in v1.18.1
type SegmentCommand byte
const ( SegmentCommandACK SegmentCommand = 0 SegmentCommandData SegmentCommand = 1 SegmentCommandTerminated SegmentCommand = 2 SegmentCommandPing SegmentCommand = 3 )
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) Len ¶ added in v1.17.3
func (this *SendingQueue) Len() uint32
func (*SendingQueue) Pop ¶ added in v1.17.3
func (this *SendingQueue) Pop() *DataSegment
func (*SendingQueue) Push ¶ added in v1.17.3
func (this *SendingQueue) Push(seg *DataSegment)
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) 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
func NewSendingWorker ¶ added in v1.18.1
func NewSendingWorker(kcp *KCP) *SendingWorker
func (*SendingWorker) CloseWrite ¶ added in v1.18.1
func (this *SendingWorker) CloseWrite()
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()
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)
func (*SendingWorker) ProcessReceivingNext ¶ added in v1.18.1
func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32)
func (*SendingWorker) ProcessSegment ¶ added in v1.18.2
func (this *SendingWorker) ProcessSegment(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)
type SimpleAuthenticator ¶
type SimpleAuthenticator struct{}
func (*SimpleAuthenticator) HeaderSize ¶
func (this *SimpleAuthenticator) HeaderSize() int
func (*SimpleAuthenticator) Seal ¶
func (this *SimpleAuthenticator) Seal(buffer *alloc.Buffer)