Reannounce renewed UPnP mapping

This commit is contained in:
Audrius Butkevicius 2014-08-12 23:29:29 +01:00
parent 8976e53998
commit dc9df0a79a
2 changed files with 21 additions and 6 deletions

View File

@ -577,7 +577,9 @@ func setupUPnP(rnd rand.Source) {
if err == nil { if err == nil {
externalPort = r externalPort = r
l.Infoln("Created UPnP port mapping - external port", externalPort) l.Infoln("Created UPnP port mapping - external port", externalPort)
if cfg.Options.UPnPRenewal > 0 {
go renewUPnP(igd, rnd, port) go renewUPnP(igd, rnd, port)
}
break break
} }
} }
@ -597,10 +599,6 @@ func setupUPnP(rnd rand.Source) {
} }
func renewUPnP(igd *upnp.IGD, rnd rand.Source, port int) { func renewUPnP(igd *upnp.IGD, rnd rand.Source, port int) {
if cfg.Options.UPnPRenewal < 1 {
return
}
for { for {
time.Sleep(time.Duration(cfg.Options.UPnPRenewal) * time.Minute) time.Sleep(time.Duration(cfg.Options.UPnPRenewal) * time.Minute)
@ -615,6 +613,7 @@ func renewUPnP(igd *upnp.IGD, rnd rand.Source, port int) {
if err == nil { if err == nil {
l.Infoln("Updated UPnP port mapping - external port", externalPort) l.Infoln("Updated UPnP port mapping - external port", externalPort)
externalPort = r externalPort = r
discoverer.StartGlobal(cfg.Options.GlobalAnnServer, uint16(r))
continue continue
} }
l.Warnln("Failed to update UPnP port mapping - externalPort", externalPort) l.Warnln("Failed to update UPnP port mapping - externalPort", externalPort)

View File

@ -33,6 +33,7 @@ type Discoverer struct {
forcedBcastTick chan time.Time forcedBcastTick chan time.Time
extAnnounceOK bool extAnnounceOK bool
extAnnounceOKmut sync.Mutex extAnnounceOKmut sync.Mutex
globalBcastStop chan bool
} }
var ( var (
@ -70,6 +71,11 @@ func (d *Discoverer) StartLocal() {
} }
func (d *Discoverer) StartGlobal(server string, extPort uint16) { func (d *Discoverer) StartGlobal(server string, extPort uint16) {
if d.globalBcastStop != nil {
d.globalBcastStop <- true
} else {
d.globalBcastStop = make(chan bool)
}
d.extServer = server d.extServer = server
d.extPort = extPort d.extPort = extPort
go d.sendExternalAnnouncements() go d.sendExternalAnnouncements()
@ -230,7 +236,17 @@ func (d *Discoverer) sendExternalAnnouncements() {
d.extAnnounceOKmut.Unlock() d.extAnnounceOKmut.Unlock()
if ok { if ok {
time.Sleep(d.globalBcastIntv) // Don't do a long sleep, listen for a stop signal, just incase
// the UPnP mapping has changed, and a new routine should be started.
for i := time.Duration(0); i < d.globalBcastIntv; i += time.Duration(1) {
select {
case <-d.globalBcastStop:
return
default:
time.Sleep(1 * time.Second)
}
}
} else { } else {
time.Sleep(errorRetryIntv) time.Sleep(errorRetryIntv)
} }