summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSimon Rozman <simon@rozman.si>2019-02-08 08:48:35 +0100
committerSimon Rozman <simon@rozman.si>2019-02-08 08:48:35 +0100
commit5981d5cacff73b3ba9fc9619ea60f8fc3ad65295 (patch)
treef2bc57bd8b5753fb22f3fda9e987dace0c4ade32
parentb13739ada2cfa1c66672e7a4edfe339e394b1a4f (diff)
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 <simon@rozman.si>
-rw-r--r--tun/tun_windows.go46
1 files 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.