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
- Variables
- func Dial(ctx context.Context, dest net.Destination, kcpConfig *KcpConfig, ...) (net.Conn, error)
- func NewAEADAESGCMBasedOnSeed(seed string) cipher.AEAD
- func NewKcpDialer(config *KcpConfig, engine security.Engine, socketConfig i.Dialer) *kcpDialer
- func NewSimpleAuthenticator() cipher.AEAD
- type AckList
- type AckSegment
- func (s *AckSegment) ByteSize() int32
- func (*AckSegment) Command() Command
- func (s *AckSegment) Conversation() uint16
- func (s *AckSegment) IsEmpty() bool
- func (s *AckSegment) IsFull() bool
- func (s *AckSegment) PutNumber(number uint32)
- func (s *AckSegment) PutTimestamp(timestamp uint32)
- func (s *AckSegment) Release()
- func (s *AckSegment) Serialize(b []byte)
- type CmdOnlySegment
- type Command
- type ConnMetadata
- type Connection
- func (c *Connection) Close() error
- func (c *Connection) CloseWrite() error
- func (c *Connection) Elapsed() uint32
- func (c *Connection) HandleOption(opt SegmentOption)
- func (c *Connection) Input(segments []Segment)
- func (c *Connection) LocalAddr() net.Addr
- func (c *Connection) OnPeerClosed()
- func (c *Connection) Ping(current uint32, cmd Command)
- func (c *Connection) Read(b []byte) (int, error)
- func (c *Connection) ReadMultiBuffer() (buf.MultiBuffer, error)
- func (c *Connection) RemoteAddr() net.Addr
- func (c *Connection) SetDeadline(t time.Time) error
- func (c *Connection) SetReadDeadline(t time.Time) error
- func (c *Connection) SetState(state State)
- func (c *Connection) SetWriteDeadline(t time.Time) error
- func (c *Connection) State() State
- func (c *Connection) Terminate()
- func (c *Connection) Write(b []byte) (int, error)
- func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error
- type ConnectionID
- type DataSegment
- type KCPPacketReader
- type KCPPacketWriter
- type KcpConfig
- func (*KcpConfig) Descriptor() ([]byte, []int)deprecated
- func (x *KcpConfig) GetCongestion() bool
- func (x *KcpConfig) GetDownlinkCapacity() uint32
- func (c *KcpConfig) GetDownlinkCapacityValue() uint32
- func (x *KcpConfig) GetHeaderConfig() *anypb.Any
- func (c *KcpConfig) GetMTUValue() uint32
- func (x *KcpConfig) GetMtu() uint32
- func (c *KcpConfig) GetPackerHeader() (headers.PacketHeader, error)
- func (x *KcpConfig) GetReadBuffer() uint32
- func (c *KcpConfig) GetReadBufferSize() uint32
- func (c *KcpConfig) GetReceivingBufferSize() uint32
- func (c *KcpConfig) GetReceivingInFlightSize() uint32
- func (c *KcpConfig) GetSecurity() (cipher.AEAD, error)
- func (x *KcpConfig) GetSeed() string
- func (c *KcpConfig) GetSendingBufferSize() uint32
- func (c *KcpConfig) GetSendingInFlightSize() uint32
- func (c *KcpConfig) GetTTIValue() uint32
- func (x *KcpConfig) GetTti() uint32
- func (x *KcpConfig) GetUplinkCapacity() uint32
- func (c *KcpConfig) GetUplinkCapacityValue() uint32
- func (x *KcpConfig) GetWriteBuffer() uint32
- func (c *KcpConfig) GetWriteBufferSize() uint32
- func (*KcpConfig) ProtoMessage()
- func (x *KcpConfig) ProtoReflect() protoreflect.Message
- func (x *KcpConfig) Reset()
- func (x *KcpConfig) String() string
- type Listener
- type PacketReader
- type PacketWriter
- type ReceivingWindow
- type ReceivingWorker
- func (*ReceivingWorker) CloseRead()
- func (w *ReceivingWorker) Flush(current uint32)
- func (w *ReceivingWorker) IsDataAvailable() bool
- func (w *ReceivingWorker) NextNumber() uint32
- func (w *ReceivingWorker) ProcessSegment(seg *DataSegment)
- func (w *ReceivingWorker) ProcessSendingNext(number uint32)
- func (w *ReceivingWorker) Read(b []byte) int
- func (w *ReceivingWorker) ReadMultiBuffer() buf.MultiBuffer
- func (w *ReceivingWorker) Release()
- func (w *ReceivingWorker) UpdateNecessary() bool
- func (w *ReceivingWorker) Write(seg Segment) error
- type RetryableWriter
- type RoundTripInfo
- type Segment
- type SegmentOption
- type SegmentWriter
- type SendingWindow
- func (sw *SendingWindow) Clear(una uint32)
- func (sw *SendingWindow) FirstNumber() uint32
- func (sw *SendingWindow) Flush(current uint32, rto uint32, maxInFlightSize uint32)
- func (sw *SendingWindow) HandleFastAck(number uint32, rto uint32)
- func (sw *SendingWindow) IsEmpty() bool
- func (sw *SendingWindow) Len() uint32
- func (sw *SendingWindow) Push(number uint32, b *buf.Buffer)
- func (sw *SendingWindow) Release()
- func (sw *SendingWindow) Remove(number uint32) bool
- func (sw *SendingWindow) Visit(visitor func(seg *DataSegment) bool)
- type SendingWorker
- func (w *SendingWorker) CloseWrite()
- func (w *SendingWorker) FindFirstUnacknowledged()
- func (w *SendingWorker) FirstUnacknowledged() uint32
- func (w *SendingWorker) Flush(current uint32)
- func (w *SendingWorker) IsEmpty() bool
- func (w *SendingWorker) OnPacketLoss(lossRate uint32)
- func (w *SendingWorker) ProcessReceivingNext(nextNumber uint32)
- func (w *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32)
- func (w *SendingWorker) ProcessSegment(current uint32, seg *AckSegment, rto uint32)
- func (w *SendingWorker) Push(b *buf.Buffer) bool
- func (w *SendingWorker) Release()
- func (w *SendingWorker) UpdateNecessary() bool
- func (w *SendingWorker) Write(seg Segment) error
- type SimpleAuthenticator
- type SimpleSegmentWriter
- type State
- type Updater
- type Writer
Constants ¶
const (
DataSegmentOverhead = 18
)
Variables ¶
var ( ErrIOTimeout = errors.New("Read/Write timeout") ErrClosedListener = errors.New("Listener closed.") ErrClosedConnection = errors.New("Connection closed.") )
var File_transport_protocols_kcp_config_proto protoreflect.FileDescriptor
Functions ¶
func Dial ¶
func Dial(ctx context.Context, dest net.Destination, kcpConfig *KcpConfig, securityConfig security.Engine, socketConfig i.Dialer) (net.Conn, error)
Dial dials a new KCP connections to the specific destination.
func NewKcpDialer ¶
func NewSimpleAuthenticator ¶
NewSimpleAuthenticator creates a new SimpleAuthenticator
Types ¶
type AckList ¶
type AckList struct {
// contains filtered or unexported fields
}
func NewAckList ¶
func NewAckList(writer SegmentWriter) *AckList
type AckSegment ¶
type AckSegment struct {
Conv uint16
Option SegmentOption
ReceivingWindow uint32
ReceivingNext uint32
Timestamp uint32
NumberList []uint32
}
func NewAckSegment ¶
func NewAckSegment() *AckSegment
func (*AckSegment) ByteSize ¶
func (s *AckSegment) ByteSize() int32
func (*AckSegment) Command ¶
func (*AckSegment) Command() Command
func (*AckSegment) Conversation ¶
func (s *AckSegment) Conversation() uint16
func (*AckSegment) IsEmpty ¶
func (s *AckSegment) IsEmpty() bool
func (*AckSegment) IsFull ¶
func (s *AckSegment) IsFull() bool
func (*AckSegment) PutNumber ¶
func (s *AckSegment) PutNumber(number uint32)
func (*AckSegment) PutTimestamp ¶
func (s *AckSegment) PutTimestamp(timestamp uint32)
func (*AckSegment) Release ¶
func (s *AckSegment) Release()
func (*AckSegment) Serialize ¶
func (s *AckSegment) Serialize(b []byte)
type CmdOnlySegment ¶
type CmdOnlySegment struct {
Conv uint16
Cmd Command
Option SegmentOption
SendingNext uint32
ReceivingNext uint32
PeerRTO uint32
}
func NewCmdOnlySegment ¶
func NewCmdOnlySegment() *CmdOnlySegment
func (*CmdOnlySegment) ByteSize ¶
func (*CmdOnlySegment) ByteSize() int32
func (*CmdOnlySegment) Command ¶
func (s *CmdOnlySegment) Command() Command
func (*CmdOnlySegment) Conversation ¶
func (s *CmdOnlySegment) Conversation() uint16
func (*CmdOnlySegment) Release ¶
func (*CmdOnlySegment) Release()
func (*CmdOnlySegment) Serialize ¶
func (s *CmdOnlySegment) Serialize(b []byte)
type ConnMetadata ¶
type Connection ¶
type Connection struct {
Config *KcpConfig
// contains filtered or unexported fields
}
Connection is a KCP connection over UDP.
func NewConnection ¶
func NewConnection(meta ConnMetadata, writer PacketWriter, closer io.Closer, config *KcpConfig) *Connection
NewConnection create a new KCP connection between local and remote.
func (*Connection) CloseWrite ¶
func (c *Connection) CloseWrite() error
func (*Connection) Elapsed ¶
func (c *Connection) Elapsed() uint32
func (*Connection) HandleOption ¶
func (c *Connection) HandleOption(opt SegmentOption)
func (*Connection) Input ¶
func (c *Connection) Input(segments []Segment)
Input when you received a low level packet (eg. UDP packet), call it
func (*Connection) LocalAddr ¶
func (c *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 ¶
func (c *Connection) OnPeerClosed()
func (*Connection) Ping ¶
func (c *Connection) Ping(current uint32, cmd Command)
func (*Connection) Read ¶
func (c *Connection) Read(b []byte) (int, error)
Read implements the Conn Read method.
func (*Connection) ReadMultiBuffer ¶
func (c *Connection) ReadMultiBuffer() (buf.MultiBuffer, error)
ReadMultiBuffer implements buf.Reader.
func (*Connection) RemoteAddr ¶
func (c *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) SetDeadline ¶
func (c *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 (c *Connection) SetReadDeadline(t time.Time) error
SetReadDeadline implements the Conn SetReadDeadline method.
func (*Connection) SetState ¶
func (c *Connection) SetState(state State)
func (*Connection) SetWriteDeadline ¶
func (c *Connection) SetWriteDeadline(t time.Time) error
SetWriteDeadline implements the Conn SetWriteDeadline method.
func (*Connection) State ¶
func (c *Connection) State() State
func (*Connection) Terminate ¶
func (c *Connection) Terminate()
func (*Connection) Write ¶
func (c *Connection) Write(b []byte) (int, error)
Write implements io.Writer.
func (*Connection) WriteMultiBuffer ¶
func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error
WriteMultiBuffer implements buf.Writer.
type DataSegment ¶
type DataSegment struct {
Conv uint16
Option SegmentOption
Timestamp uint32
Number uint32
SendingNext uint32
// contains filtered or unexported fields
}
func NewDataSegment ¶
func NewDataSegment() *DataSegment
func (*DataSegment) ByteSize ¶
func (s *DataSegment) ByteSize() int32
func (*DataSegment) Command ¶
func (*DataSegment) Command() Command
func (*DataSegment) Conversation ¶
func (s *DataSegment) Conversation() uint16
func (*DataSegment) Data ¶
func (s *DataSegment) Data() *buf.Buffer
func (*DataSegment) Detach ¶
func (s *DataSegment) Detach() *buf.Buffer
func (*DataSegment) Release ¶
func (s *DataSegment) Release()
func (*DataSegment) Serialize ¶
func (s *DataSegment) Serialize(b []byte)
type KCPPacketReader ¶
type KCPPacketReader struct {
Security cipher.AEAD
Header headers.PacketHeader
}
func (*KCPPacketReader) Read ¶
func (r *KCPPacketReader) Read(b []byte) []Segment
type KCPPacketWriter ¶
func (*KCPPacketWriter) Overhead ¶
func (w *KCPPacketWriter) Overhead() int
type KcpConfig ¶
type KcpConfig struct {
Mtu uint32 `protobuf:"varint,1,opt,name=mtu,proto3" json:"mtu,omitempty"`
Tti uint32 `protobuf:"varint,2,opt,name=tti,proto3" json:"tti,omitempty"`
UplinkCapacity uint32 `protobuf:"varint,3,opt,name=uplink_capacity,json=uplinkCapacity,proto3" json:"uplink_capacity,omitempty"`
DownlinkCapacity uint32 `protobuf:"varint,4,opt,name=downlink_capacity,json=downlinkCapacity,proto3" json:"downlink_capacity,omitempty"`
Congestion bool `protobuf:"varint,5,opt,name=congestion,proto3" json:"congestion,omitempty"`
WriteBuffer uint32 `protobuf:"varint,6,opt,name=write_buffer,json=writeBuffer,proto3" json:"write_buffer,omitempty"`
ReadBuffer uint32 `protobuf:"varint,7,opt,name=read_buffer,json=readBuffer,proto3" json:"read_buffer,omitempty"`
HeaderConfig *anypb.Any `protobuf:"bytes,8,opt,name=header_config,json=headerConfig,proto3" json:"header_config,omitempty"`
Seed string `protobuf:"bytes,10,opt,name=seed,proto3" json:"seed,omitempty"`
// contains filtered or unexported fields
}
func (*KcpConfig) Descriptor
deprecated
func (*KcpConfig) GetCongestion ¶
func (*KcpConfig) GetDownlinkCapacity ¶
func (*KcpConfig) GetDownlinkCapacityValue ¶
GetDownlinkCapacityValue returns the value of DownlinkCapacity settings.
func (*KcpConfig) GetHeaderConfig ¶
func (*KcpConfig) GetMTUValue ¶
GetMTUValue returns the value of MTU settings.
func (*KcpConfig) GetPackerHeader ¶
func (c *KcpConfig) GetPackerHeader() (headers.PacketHeader, error)
func (*KcpConfig) GetReadBuffer ¶
func (*KcpConfig) GetReadBufferSize ¶
GetReadBufferSize returns the size of ReadBuffer in bytes.
func (*KcpConfig) GetReceivingBufferSize ¶
func (*KcpConfig) GetReceivingInFlightSize ¶
func (*KcpConfig) GetSecurity ¶
GetSecurity returns the security settings.
func (*KcpConfig) GetSendingBufferSize ¶
func (*KcpConfig) GetSendingInFlightSize ¶
func (*KcpConfig) GetTTIValue ¶
GetTTIValue returns the value of TTI settings.
func (*KcpConfig) GetUplinkCapacity ¶
func (*KcpConfig) GetUplinkCapacityValue ¶
GetUplinkCapacityValue returns the value of UplinkCapacity settings.
func (*KcpConfig) GetWriteBuffer ¶
func (*KcpConfig) GetWriteBufferSize ¶
GetWriteBufferSize returns the size of WriterBuffer in bytes.
func (*KcpConfig) ProtoMessage ¶
func (*KcpConfig) ProtoMessage()
func (*KcpConfig) ProtoReflect ¶
func (x *KcpConfig) ProtoReflect() protoreflect.Message
type Listener ¶
Listener defines a server listening for connections
func (*Listener) ActiveConnections ¶
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.
func (*Listener) Remove ¶
func (l *Listener) Remove(id ConnectionID)
type PacketReader ¶
type PacketWriter ¶
type ReceivingWindow ¶
type ReceivingWindow struct {
// contains filtered or unexported fields
}
func NewReceivingWindow ¶
func NewReceivingWindow() *ReceivingWindow
func (*ReceivingWindow) Has ¶
func (w *ReceivingWindow) Has(id uint32) bool
func (*ReceivingWindow) Remove ¶
func (w *ReceivingWindow) Remove(id uint32) *DataSegment
func (*ReceivingWindow) Set ¶
func (w *ReceivingWindow) Set(id uint32, value *DataSegment) bool
type ReceivingWorker ¶
func NewReceivingWorker ¶
func NewReceivingWorker(kcp *Connection) *ReceivingWorker
func (*ReceivingWorker) CloseRead ¶
func (*ReceivingWorker) CloseRead()
func (*ReceivingWorker) Flush ¶
func (w *ReceivingWorker) Flush(current uint32)
func (*ReceivingWorker) IsDataAvailable ¶
func (w *ReceivingWorker) IsDataAvailable() bool
func (*ReceivingWorker) NextNumber ¶
func (w *ReceivingWorker) NextNumber() uint32
func (*ReceivingWorker) ProcessSegment ¶
func (w *ReceivingWorker) ProcessSegment(seg *DataSegment)
func (*ReceivingWorker) ProcessSendingNext ¶
func (w *ReceivingWorker) ProcessSendingNext(number uint32)
func (*ReceivingWorker) Read ¶
func (w *ReceivingWorker) Read(b []byte) int
func (*ReceivingWorker) ReadMultiBuffer ¶
func (w *ReceivingWorker) ReadMultiBuffer() buf.MultiBuffer
func (*ReceivingWorker) Release ¶
func (w *ReceivingWorker) Release()
func (*ReceivingWorker) UpdateNecessary ¶
func (w *ReceivingWorker) UpdateNecessary() bool
func (*ReceivingWorker) Write ¶
func (w *ReceivingWorker) Write(seg Segment) error
type RetryableWriter ¶
type RetryableWriter struct {
// contains filtered or unexported fields
}
func (*RetryableWriter) Write ¶
func (w *RetryableWriter) Write(seg Segment) error
type RoundTripInfo ¶
func (*RoundTripInfo) SmoothedTime ¶
func (info *RoundTripInfo) SmoothedTime() uint32
func (*RoundTripInfo) Timeout ¶
func (info *RoundTripInfo) Timeout() uint32
func (*RoundTripInfo) Update ¶
func (info *RoundTripInfo) Update(rtt uint32, current uint32)
func (*RoundTripInfo) UpdatePeerRTO ¶
func (info *RoundTripInfo) UpdatePeerRTO(rto uint32, current uint32)
type Segment ¶
type Segment interface {
Release()
Conversation() uint16
Command() Command
ByteSize() int32
Serialize([]byte)
// contains filtered or unexported methods
}
func ReadSegment ¶
type SegmentWriter ¶
func NewRetryableWriter ¶
func NewRetryableWriter(writer SegmentWriter) SegmentWriter
func NewSegmentWriter ¶
func NewSegmentWriter(writer io.Writer) SegmentWriter
type SendingWindow ¶
type SendingWindow struct {
// contains filtered or unexported fields
}
func NewSendingWindow ¶
func NewSendingWindow(writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow
func (*SendingWindow) Clear ¶
func (sw *SendingWindow) Clear(una uint32)
func (*SendingWindow) FirstNumber ¶
func (sw *SendingWindow) FirstNumber() uint32
func (*SendingWindow) Flush ¶
func (sw *SendingWindow) Flush(current uint32, rto uint32, maxInFlightSize uint32)
func (*SendingWindow) HandleFastAck ¶
func (sw *SendingWindow) HandleFastAck(number uint32, rto uint32)
func (*SendingWindow) IsEmpty ¶
func (sw *SendingWindow) IsEmpty() bool
func (*SendingWindow) Len ¶
func (sw *SendingWindow) Len() uint32
func (*SendingWindow) Release ¶
func (sw *SendingWindow) Release()
func (*SendingWindow) Remove ¶
func (sw *SendingWindow) Remove(number uint32) bool
func (*SendingWindow) Visit ¶
func (sw *SendingWindow) Visit(visitor func(seg *DataSegment) bool)
type SendingWorker ¶
func NewSendingWorker ¶
func NewSendingWorker(kcp *Connection) *SendingWorker
func (*SendingWorker) CloseWrite ¶
func (w *SendingWorker) CloseWrite()
func (*SendingWorker) FindFirstUnacknowledged ¶
func (w *SendingWorker) FindFirstUnacknowledged()
func (*SendingWorker) FirstUnacknowledged ¶
func (w *SendingWorker) FirstUnacknowledged() uint32
func (*SendingWorker) Flush ¶
func (w *SendingWorker) Flush(current uint32)
func (*SendingWorker) IsEmpty ¶
func (w *SendingWorker) IsEmpty() bool
func (*SendingWorker) OnPacketLoss ¶
func (w *SendingWorker) OnPacketLoss(lossRate uint32)
func (*SendingWorker) ProcessReceivingNext ¶
func (w *SendingWorker) ProcessReceivingNext(nextNumber uint32)
func (*SendingWorker) ProcessReceivingNextWithoutLock ¶
func (w *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32)
func (*SendingWorker) ProcessSegment ¶
func (w *SendingWorker) ProcessSegment(current uint32, seg *AckSegment, rto uint32)
func (*SendingWorker) Release ¶
func (w *SendingWorker) Release()
func (*SendingWorker) UpdateNecessary ¶
func (w *SendingWorker) UpdateNecessary() bool
func (*SendingWorker) Write ¶
func (w *SendingWorker) Write(seg Segment) error
type SimpleAuthenticator ¶
type SimpleAuthenticator struct{}
SimpleAuthenticator is a legacy AEAD used for KCP encryption.
func (*SimpleAuthenticator) NonceSize ¶
func (*SimpleAuthenticator) NonceSize() int
NonceSize implements cipher.AEAD.NonceSize().
func (*SimpleAuthenticator) Open ¶
func (a *SimpleAuthenticator) Open(dst, nonce, cipherText, extra []byte) ([]byte, error)
Open implements cipher.AEAD.Open().
func (*SimpleAuthenticator) Overhead ¶
func (*SimpleAuthenticator) Overhead() int
Overhead implements cipher.AEAD.NonceSize().
func (*SimpleAuthenticator) Seal ¶
func (a *SimpleAuthenticator) Seal(dst, nonce, plain, extra []byte) []byte
Seal implements cipher.AEAD.Seal().
type SimpleSegmentWriter ¶
func (*SimpleSegmentWriter) Write ¶
func (w *SimpleSegmentWriter) Write(seg Segment) error
type State ¶
type State int32
State of the connection
const ( StateActive State = 0 // Connection is active StateReadyToClose State = 1 // Connection is closed locally StatePeerClosed State = 2 // Connection is closed on remote StateTerminating State = 3 // Connection is ready to be destroyed locally StatePeerTerminating State = 4 // Connection is ready to be destroyed on remote StateTerminated State = 5 // Connection is destroyed. )
type Updater ¶
type Updater struct {
// contains filtered or unexported fields
}