From 0f368f8139019d44ea69a3df5dc829afa35d42d4 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Mon, 13 Jun 2016 15:03:58 +0800 Subject: [PATCH] KCP: comments --- transport/config.go | 7 ++++++ transport/hub/kcp.go | 14 +++++++++++ transport/hub/kcpv/config.go | 45 +++++++++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/transport/config.go b/transport/config.go index 18205c1c8..7ea9beccf 100644 --- a/transport/config.go +++ b/transport/config.go @@ -17,6 +17,13 @@ func (this *Config) Apply() error { enableKcp = this.enableKcp if enableKcp { KcpConfig = this.kcpConfig + /* + KCP do not support connectionReuse, + it is mandatory to set connectionReuse to false + Since KCP have no handshake and + does not SlowStart, there isn't benefit to + use that anyway. + */ connectionReuse = false } return nil diff --git a/transport/hub/kcp.go b/transport/hub/kcp.go index 63c67001f..d50c00c48 100644 --- a/transport/hub/kcp.go +++ b/transport/hub/kcp.go @@ -48,6 +48,11 @@ func (kvl *KCPVlistener) Accept() (net.Conn, error) { } else { kvl.previousSocketid_mapid++ kvl.previousSocketid[kvl.previousSocketid_mapid] = conn.GetConv() + /* + Here we assume that count(connection) < 512 + This won't always true. + More work might be necessary to deal with this in a better way. + */ if kvl.previousSocketid_mapid >= 512 { delete(kvl.previousSocketid, kvl.previousSocketid_mapid-512) } @@ -93,6 +98,11 @@ func (kcpvc *KCPVconn) Write(b []byte) (int, error) { kcpvc.hc.SetDeadline(kcpvc.conntokeep) return kcpvc.hc.Write(b) } + +/*ApplyConf will apply kcpvc.conf to current Socket + +It is recommmanded to call this func once and only once +*/ func (kcpvc *KCPVconn) ApplyConf() error { nodelay, interval, resend, nc := 0, 40, 0, 0 if kcpvc.conf.Mode != "manual" { @@ -121,6 +131,10 @@ func (kcpvc *KCPVconn) ApplyConf() error { return nil } +/*Close Close the current conn +We have to delay the close of Socket for a few second +or the VMess EOF can be too late to send. +*/ func (kcpvc *KCPVconn) Close() error { go func() { time.Sleep(2000 * time.Millisecond) diff --git a/transport/hub/kcpv/config.go b/transport/hub/kcpv/config.go index 348b31de5..9b162d308 100644 --- a/transport/hub/kcpv/config.go +++ b/transport/hub/kcpv/config.go @@ -1,5 +1,34 @@ package kcpv +/*AdvancedConfig define behavior of KCP in detail + +MaximumTransmissionUnit: +Largest protocol data unit that the layer can pass onwards +can be discovered by running tracepath + +SendingWindowSize , ReceivingWindowSize: +the size of Tx/Rx window, by packet + +ForwardErrorCorrectionGroupSize: +The the size of packet to generate a Forward Error Correction +packet, this is used to counteract packet loss. + +AcknowledgeNoDelay: +Do not wait a certain of time before sending the ACK packet, +increase bandwich cost and might increase performance + +Dscp: +Differentiated services code point, +be used to reconized to discriminate packet. +It is recommanded to keep it 0 to avoid being detected. + +ReadTimeout,WriteTimeout: +Close the Socket if it have been silent for too long, +Small value can recycle zombie socket faster but +can cause v2ray to kill the proxy connection it is relaying, +Higher value can prevent server from closing zombie socket and +waste resources. +*/ type AdvancedConfig struct { Mtu int `json:"MaximumTransmissionUnit"` Sndwnd int `json:"SendingWindowSize"` @@ -11,6 +40,20 @@ type AdvancedConfig struct { WriteTimeout int `json:"WriteTimeout"` } +/*Config define basic behavior of KCP +Mode: +can be one of these values: +fast3,fast2,fast,normal +<<<<<<- less delay +->>>>>> less bandwich wasted + +EncryptionKey: +a string that will be the EncryptionKey of +All KCP connection we Listen-Accpet or +Dial, We are not very sure about how this +encryption hehave and DO use a unique randomly +generated key. +*/ type Config struct { Mode string `json:"Mode"` Key string `json:"EncryptionKey"` @@ -18,5 +61,5 @@ type Config struct { } var DefaultAdvancedConfigs = &AdvancedConfig{ - Mtu: 1350, Sndwnd: 1024, Rcvwnd: 1024, Fec: 4, Dscp: 0, ReadTimeout: 18600, WriteTimeout: 18500, Acknodelay: false, + Mtu: 1350, Sndwnd: 1024, Rcvwnd: 1024, Fec: 4, Dscp: 0, ReadTimeout: 600, WriteTimeout: 500, Acknodelay: false, }