Skip to content

A full-featured download manager.

refactor: upnp close client

Dr_rOotb33b505ccbb0
diff --git a/src/main/Application.js b/src/main/Application.js
index 0b57274f..a6a8063a 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -207,7 +207,7 @@ export default class Application extends EventEmitter {
await this.stopUPnPMapping()
}
- this.upnp.destroy()
+ this.upnp.closeClient()
}
autoSyncTracker () {
diff --git a/src/main/core/UPnPManager.js b/src/main/core/UPnPManager.js
index 2d5e5b6f..6d42bbaa 100644
--- a/src/main/core/UPnPManager.js
+++ b/src/main/core/UPnPManager.js
@@ -3,6 +3,7 @@ import NatAPI from 'nat-api'
import logger from './Logger'
let client = null
+let timer = null
const mappingStatus = {}
export default class UPnPManager {
@@ -21,6 +22,7 @@ export default class UPnPManager {
}
map (port) {
+ this.clearCloseTimer()
this.init()
return new Promise((resolve, reject) => {
@@ -41,10 +43,13 @@ export default class UPnPManager {
logger.info(`[Motrix] UPnPManager port ${port} mapping succeeded`)
resolve()
})
+ }).finally(() => {
+ this.delayCloseClient()
})
}
unmap (port) {
+ this.clearCloseTimer()
this.init()
return new Promise((resolve, reject) => {
@@ -70,10 +75,24 @@ export default class UPnPManager {
mappingStatus[port] = false
resolve()
})
+ }).finally(() => {
+ this.delayCloseClient()
})
}
- destroy () {
+ delayCloseClient () {
+ console.log('delayCloseClient===>', timer)
+ timer = setTimeout(() => {
+ this.closeClient()
+ }, 30000)
+ }
+
+ clearCloseTimer () {
+ clearTimeout(timer)
+ }
+
+ closeClient () {
+ console.log('closeClient===>')
if (!client) {
return
}