From 43d2c47d8ac1faf652f7f8dbc012c39339b2db13 Mon Sep 17 00:00:00 2001 From: xtaci Date: Wed, 1 Jul 2020 18:04:55 +0800 Subject: [PATCH] flush when input has make a window slide --- kcp.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kcp.go b/kcp.go index 57eabc5..e11e424 100644 --- a/kcp.go +++ b/kcp.go @@ -441,7 +441,7 @@ func (kcp *KCP) parse_fastack(sn, ts uint32) { } } -func (kcp *KCP) parse_una(una uint32) { +func (kcp *KCP) parse_una(una uint32) int { count := 0 for k := range kcp.snd_buf { seg := &kcp.snd_buf[k] @@ -455,6 +455,7 @@ func (kcp *KCP) parse_una(una uint32) { if count > 0 { kcp.snd_buf = kcp.remove_front(kcp.snd_buf, count) } + return count } // ack append @@ -534,6 +535,7 @@ func (kcp *KCP) Input(data []byte, regular, ackNoDelay bool) int { var latest uint32 // the latest ack packet var flag int var inSegs uint64 + var windowSlides bool for { var ts, sn, length, una, conv uint32 @@ -569,7 +571,9 @@ func (kcp *KCP) Input(data []byte, regular, ackNoDelay bool) int { if regular { kcp.rmt_wnd = uint32(wnd) } - kcp.parse_una(una) + if kcp.parse_una(una) > 0 { + windowSlides = true + } kcp.shrink_buf() if cmd == IKCP_CMD_ACK { @@ -650,6 +654,10 @@ func (kcp *KCP) Input(data []byte, regular, ackNoDelay bool) int { } } + if windowSlides { // if window has slided, flush + kcp.flush(false) + } + if ackNoDelay && len(kcp.acklist) > 0 { // ack immediately kcp.flush(true) }