From 94fb16fdfa2fc1509acfcaf66ddc67bfcd4b6f4a Mon Sep 17 00:00:00 2001 From: v2ray Date: Sat, 2 Jul 2016 11:19:32 +0200 Subject: [PATCH] refine kcp constructor --- transport/internet/kcp/config.go | 8 ++++++++ transport/internet/kcp/connection.go | 26 ++++++++++++-------------- transport/internet/kcp/kcp.go | 12 ++++++------ transport/internet/kcp/receiving.go | 2 +- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/transport/internet/kcp/config.go b/transport/internet/kcp/config.go index 9dfd1b65..5e8345dd 100644 --- a/transport/internet/kcp/config.go +++ b/transport/internet/kcp/config.go @@ -22,6 +22,10 @@ func (this *Config) GetSendingWindowSize() uint32 { return size } +func (this *Config) GetSendingQueueSize() uint32 { + return this.WriteBuffer / this.Mtu +} + func (this *Config) GetReceivingWindowSize() uint32 { size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2 if size == 0 { @@ -30,6 +34,10 @@ func (this *Config) GetReceivingWindowSize() uint32 { return size } +func (this *Config) GetReceivingQueueSize() uint32 { + return this.ReadBuffer / this.Mtu +} + func DefaultConfig() Config { return Config{ Mtu: 1350, diff --git a/transport/internet/kcp/connection.go b/transport/internet/kcp/connection.go index 0513b6f7..f8c881db 100644 --- a/transport/internet/kcp/connection.go +++ b/transport/internet/kcp/connection.go @@ -40,18 +40,17 @@ func nowMillisec() int64 { // Connection is a KCP connection over UDP. type Connection struct { sync.RWMutex - state ConnState - kcp *KCP // the core ARQ - kcpAccess sync.Mutex - block Authenticator - needUpdate bool - local, remote net.Addr - wd time.Time // write deadline - chReadEvent chan struct{} - writer io.WriteCloser - since int64 - terminateOnce signal.Once - writeBufferSize uint32 + state ConnState + kcp *KCP // the core ARQ + kcpAccess sync.Mutex + block Authenticator + needUpdate bool + local, remote net.Addr + wd time.Time // write deadline + chReadEvent chan struct{} + writer io.WriteCloser + since int64 + terminateOnce signal.Once } // NewConnection create a new KCP connection between local and remote. @@ -63,13 +62,12 @@ func NewConnection(conv uint16, writerCloser io.WriteCloser, local *net.UDPAddr, conn.block = block conn.writer = writerCloser conn.since = nowMillisec() - conn.writeBufferSize = effectiveConfig.WriteBuffer / effectiveConfig.Mtu authWriter := &AuthenticationWriter{ Authenticator: block, Writer: writerCloser, } - conn.kcp = NewKCP(conv, effectiveConfig.GetSendingWindowSize(), effectiveConfig.GetReceivingWindowSize(), conn.writeBufferSize, authWriter) + conn.kcp = NewKCP(conv, authWriter) conn.kcp.NoDelay(effectiveConfig.Tti, 2, effectiveConfig.Congestion) conn.kcp.current = conn.Elapsed() diff --git a/transport/internet/kcp/kcp.go b/transport/internet/kcp/kcp.go index a8f8631a..27e99e13 100644 --- a/transport/internet/kcp/kcp.go +++ b/transport/internet/kcp/kcp.go @@ -65,22 +65,22 @@ type KCP struct { // NewKCP create a new kcp control object, 'conv' must equal in two endpoint // from the same connection. -func NewKCP(conv uint16, sendingWindowSize uint32, receivingWindowSize uint32, sendingQueueSize uint32, output *AuthenticationWriter) *KCP { +func NewKCP(conv uint16, output *AuthenticationWriter) *KCP { log.Debug("KCP|Core: creating KCP ", conv) kcp := new(KCP) kcp.conv = conv - kcp.snd_wnd = sendingWindowSize - kcp.rcv_wnd = receivingWindowSize + kcp.snd_wnd = effectiveConfig.GetSendingWindowSize() + kcp.rcv_wnd = effectiveConfig.GetReceivingWindowSize() kcp.rmt_wnd = IKCP_WND_RCV kcp.mss = output.Mtu() - DataSegmentOverhead kcp.rx_rto = IKCP_RTO_DEF kcp.interval = IKCP_INTERVAL kcp.output = NewSegmentWriter(output) - kcp.rcv_buf = NewReceivingWindow(receivingWindowSize) - kcp.snd_queue = NewSendingQueue(sendingQueueSize) + kcp.rcv_buf = NewReceivingWindow(effectiveConfig.GetReceivingWindowSize()) + kcp.snd_queue = NewSendingQueue(effectiveConfig.GetSendingQueueSize()) kcp.rcv_queue = NewReceivingQueue() kcp.acklist = NewACKList(kcp) - kcp.snd_buf = NewSendingWindow(kcp, sendingWindowSize) + kcp.snd_buf = NewSendingWindow(kcp, effectiveConfig.GetSendingWindowSize()) kcp.cwnd = kcp.snd_wnd return kcp } diff --git a/transport/internet/kcp/receiving.go b/transport/internet/kcp/receiving.go index 5c8c72e5..a64ac1b9 100644 --- a/transport/internet/kcp/receiving.go +++ b/transport/internet/kcp/receiving.go @@ -67,7 +67,7 @@ type ReceivingQueue struct { func NewReceivingQueue() *ReceivingQueue { return &ReceivingQueue{ - queue: make(chan *alloc.Buffer, effectiveConfig.ReadBuffer/effectiveConfig.Mtu), + queue: make(chan *alloc.Buffer, effectiveConfig.GetReceivingQueueSize()), } }