Allow multicast to be shut down more sanely

This commit is contained in:
Neil Alexander 2019-09-18 15:31:43 +01:00
parent 00a972b74e
commit 366fe7e772
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 113 additions and 118 deletions

View File

@ -27,6 +27,8 @@ type Multicast struct {
listeners map[string]*yggdrasil.TcpListener listeners map[string]*yggdrasil.TcpListener
listenPort uint16 listenPort uint16
isOpen bool isOpen bool
announcer *time.Timer
platformhandler *time.Timer
} }
// Init prepares the multicast interface for use. // Init prepares the multicast interface for use.
@ -63,9 +65,9 @@ func (m *Multicast) Start() error {
} }
m.isOpen = true m.isOpen = true
go m.multicastStarted()
go m.listen() go m.listen()
go m.announce() m.multicastStarted()
m.announce()
return nil return nil
} }
@ -73,6 +75,8 @@ func (m *Multicast) Start() error {
// Stop is not implemented for multicast yet. // Stop is not implemented for multicast yet.
func (m *Multicast) Stop() error { func (m *Multicast) Stop() error {
m.isOpen = false m.isOpen = false
m.announcer.Stop()
m.platformhandler.Stop()
m.sock.Close() m.sock.Close()
return nil return nil
} }
@ -136,7 +140,6 @@ func (m *Multicast) announce() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
for {
interfaces := m.Interfaces() interfaces := m.Interfaces()
// There might be interfaces that we configured listeners for but are no // There might be interfaces that we configured listeners for but are no
// longer up - if that's the case then we should stop the listeners // longer up - if that's the case then we should stop the listeners
@ -236,8 +239,7 @@ func (m *Multicast) announce() {
break break
} }
} }
time.Sleep(time.Second * 15) m.announcer = time.AfterFunc(time.Second*15, m.announce)
}
} }
func (m *Multicast) listen() { func (m *Multicast) listen() {

View File

@ -32,21 +32,14 @@ import (
var awdlGoroutineStarted bool var awdlGoroutineStarted bool
func (m *Multicast) multicastStarted() { func (m *Multicast) multicastStarted() {
if awdlGoroutineStarted {
return
}
awdlGoroutineStarted = true
for {
C.StopAWDLBrowsing() C.StopAWDLBrowsing()
for intf := range m.Interfaces() { for intf := range m.Interfaces() {
if intf == "awdl0" { if intf == "awdl0" {
m.log.Infoln("Multicast discovery is using AWDL discovery")
C.StartAWDLBrowsing() C.StartAWDLBrowsing()
break break
} }
} }
time.Sleep(time.Minute) m.platformhandler = time.AfterFunc(time.Minute, m.multicastStarted)
}
} }
func (m *Multicast) multicastReuse(network string, address string, c syscall.RawConn) error { func (m *Multicast) multicastReuse(network string, address string, c syscall.RawConn) error {