From a2bf128d43e7a819d18c3675b945269bc3134fac Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 10 Sep 2019 22:01:16 +0800 Subject: [PATCH] more accurate write block --- sess.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sess.go b/sess.go index 92c4f15..9ce84c0 100644 --- a/sess.go +++ b/sess.go @@ -276,7 +276,10 @@ func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) { } s.mu.Lock() - if s.kcp.WaitSnd() < int(s.kcp.snd_wnd) { + + // make sure write do not overflow the max sliding window on both side + waitsnd := s.kcp.WaitSnd() + if waitsnd < int(s.kcp.snd_wnd) && waitsnd < int(s.kcp.rmt_wnd) { for _, b := range v { n += len(b) for { @@ -290,7 +293,8 @@ func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) { } } - if s.kcp.WaitSnd() >= int(s.kcp.snd_wnd) || !s.writeDelay { + waitsnd = s.kcp.WaitSnd() + if waitsnd >= int(s.kcp.snd_wnd) || waitsnd >= int(s.kcp.rmt_wnd) || !s.writeDelay { s.kcp.flush(false) s.uncork() }