From 5981d5cacff73b3ba9fc9619ea60f8fc3ad65295 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 8 Feb 2019 08:48:35 +0100 Subject: wintun: Check for user close in read loop regardless the load Do the WaitForSingleObject() always to provide high-load responsiveness. Reorder events so TUN_SIGNAL_CLOSE has priority over TUN_SIGNAL_DATA_AVAIL, to provide high-load responsiveness at all. Signed-off-by: Simon Rozman --- tun/tun_windows.go | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/tun/tun_windows.go b/tun/tun_windows.go index f284baf..687057a 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -22,10 +22,10 @@ const ( ) const ( - TUN_SIGNAL_DATA_AVAIL = 0 - TUN_SIGNAL_CLOSE = 1 + TUN_SIGNAL_CLOSE = iota + TUN_SIGNAL_DATA_AVAIL - TUN_SIGNAL_MAX = 2 + TUN_SIGNAL_MAX ) type tunPacket struct { @@ -229,27 +229,25 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { } } - if tun.rdBuff.numPackets < TUN_MAX_PACKET_EXCHANGE || !tun.rdBuff.left { - // Buffer was not full. Wait for the interface data or user close. - r, err := windows.WaitForMultipleObjects(tun.signals[:], false, windows.INFINITE) - if err != nil { - return 0, errors.New("Waiting for data failed: " + err.Error()) - } - switch r { - case windows.WAIT_OBJECT_0 + TUN_SIGNAL_DATA_AVAIL: - // Data is available. - case windows.WAIT_ABANDONED + TUN_SIGNAL_DATA_AVAIL: - // TUN stopped. Reopen it. - tun.closeTUN() - continue - case windows.WAIT_OBJECT_0 + TUN_SIGNAL_CLOSE, windows.WAIT_ABANDONED + TUN_SIGNAL_CLOSE: - return 0, errors.New("TUN closed") - case windows.WAIT_TIMEOUT: - // Congratulations, we reached infinity. Let's do it again! :) - continue - default: - return 0, errors.New("unexpected result from WaitForMultipleObjects") - } + // Wait for user close or interface data. + r, err := windows.WaitForMultipleObjects(tun.signals[:], false, windows.INFINITE) + if err != nil { + return 0, errors.New("Waiting for data failed: " + err.Error()) + } + switch r { + case windows.WAIT_OBJECT_0 + TUN_SIGNAL_CLOSE, windows.WAIT_ABANDONED + TUN_SIGNAL_CLOSE: + return 0, errors.New("TUN closed") + case windows.WAIT_OBJECT_0 + TUN_SIGNAL_DATA_AVAIL: + // Data is available. + case windows.WAIT_ABANDONED + TUN_SIGNAL_DATA_AVAIL: + // TUN stopped. Reopen it. + tun.closeTUN() + continue + case windows.WAIT_TIMEOUT: + // Congratulations, we reached infinity. Let's do it again! :) + continue + default: + return 0, errors.New("unexpected result from WaitForMultipleObjects") } // Fill queue. -- cgit v1.2.3