From 3c2cdfea1c14ca1099fe21387f0611a162d01f9c Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 10 Mar 2019 18:05:27 +0000 Subject: [PATCH] Keep AWDL awake, or wake it up again after a minute if suspended for some reason (e.g. sleep) --- src/yggdrasil/multicast.go | 2 +- src/yggdrasil/multicast_darwin.go | 46 ++++++++++++++++++++++-------- src/yggdrasil/multicast_other.go | 2 +- src/yggdrasil/multicast_unix.go | 2 +- src/yggdrasil/multicast_windows.go | 2 +- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/yggdrasil/multicast.go b/src/yggdrasil/multicast.go index bf4b2a74..a29bbc7e 100644 --- a/src/yggdrasil/multicast.go +++ b/src/yggdrasil/multicast.go @@ -57,7 +57,7 @@ func (m *multicast) start() error { // Windows can't set this flag, so we need to handle it in other ways } - m.multicastWake() + go m.multicastStarted() go m.listen() go m.announce() } diff --git a/src/yggdrasil/multicast_darwin.go b/src/yggdrasil/multicast_darwin.go index 66c18241..53646113 100644 --- a/src/yggdrasil/multicast_darwin.go +++ b/src/yggdrasil/multicast_darwin.go @@ -6,24 +6,46 @@ package yggdrasil #cgo CFLAGS: -x objective-c #cgo LDFLAGS: -framework Foundation #import -void WakeUpAWDL() { - NSNetServiceBrowser *serviceBrowser; - - serviceBrowser = [[NSNetServiceBrowser alloc] init]; - serviceBrowser.includesPeerToPeer = YES; +NSNetServiceBrowser *serviceBrowser; +void StartAWDLBrowsing() { + if (serviceBrowser == nil) { + serviceBrowser = [[NSNetServiceBrowser alloc] init]; + serviceBrowser.includesPeerToPeer = YES; + } [serviceBrowser searchForServicesOfType:@"_yggdrasil._tcp" inDomain:@""]; } +void StopAWDLBrowsing() { + if (serviceBrowser == nil) { + return; + } + [serviceBrowser stop]; +} */ import "C" -import "syscall" -import "golang.org/x/sys/unix" +import ( + "syscall" + "time" -func (m *multicast) multicastWake() { - for _, intf := range m.interfaces() { - if intf.Name == "awdl0" { - m.core.log.Infoln("Multicast discovery is waking up AWDL") - C.WakeUpAWDL() + "golang.org/x/sys/unix" +) + +var awdlGoroutineStarted bool + +func (m *multicast) multicastStarted() { + if awdlGoroutineStarted { + return + } + m.core.log.Infoln("Multicast discovery will wake up AWDL if required") + awdlGoroutineStarted = true + for { + C.StopAWDLBrowsing() + for _, intf := range m.interfaces() { + if intf.Name == "awdl0" { + C.StartAWDLBrowsing() + break + } } + time.Sleep(time.Minute) } } diff --git a/src/yggdrasil/multicast_other.go b/src/yggdrasil/multicast_other.go index 043dd3ae..e20bbda3 100644 --- a/src/yggdrasil/multicast_other.go +++ b/src/yggdrasil/multicast_other.go @@ -4,7 +4,7 @@ package yggdrasil import "syscall" -func (m *multicast) multicastWake() { +func (m *multicast) multicastStarted() { } diff --git a/src/yggdrasil/multicast_unix.go b/src/yggdrasil/multicast_unix.go index 4237481c..3da1ab4e 100644 --- a/src/yggdrasil/multicast_unix.go +++ b/src/yggdrasil/multicast_unix.go @@ -5,7 +5,7 @@ package yggdrasil import "syscall" import "golang.org/x/sys/unix" -func (m *multicast) multicastWake() { +func (m *multicast) multicastStarted() { } diff --git a/src/yggdrasil/multicast_windows.go b/src/yggdrasil/multicast_windows.go index 14c93129..3e07f6cc 100644 --- a/src/yggdrasil/multicast_windows.go +++ b/src/yggdrasil/multicast_windows.go @@ -5,7 +5,7 @@ package yggdrasil import "syscall" import "golang.org/x/sys/windows" -func (m *multicast) multicastWake() { +func (m *multicast) multicastStarted() { }