@@ -4,9 +4,9 @@ import android.net.Uri
44import androidx.core.net.toUri
55import com.lagradost.api.Log
66import com.lagradost.cloudstream3.CommonActivity
7+ import com.lagradost.cloudstream3.ErrorLoadingException
78import com.lagradost.cloudstream3.TvType
89import com.lagradost.cloudstream3.mvvm.logError
9- import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
1010import com.lagradost.cloudstream3.utils.ExtractorLink
1111import com.lagradost.fetchbutton.aria2c.Aria2Args
1212import com.lagradost.fetchbutton.aria2c.Aria2Settings
@@ -117,7 +117,7 @@ object Torrent {
117117 connections = metadata.items.sumOf { it.connections }
118118 )
119119 )
120- if (metadata.status != DownloadStatusTell .Complete ) {
120+ if (metadata.status != DownloadStatusTell .Complete ) {
121121 extraWait = defaultWait
122122 }
123123 when (metadata.status) {
@@ -336,7 +336,31 @@ object Torrent {
336336
337337 when (metadata?.status) {
338338 DownloadStatusTell .Removed , DownloadStatusTell .Error , null -> {
339- Aria2Starter .download(uriReq)
339+ var isValid = false
340+ // use incremental delay in the case of weird behavior of startup time or something
341+ for (i in 0 .. 10 ) {
342+ val response = Aria2Starter .instance?.client?.sendUri(uriReq)
343+ DownloadListener .sessionIdToLastRequest[requestId] = uriReq
344+ // instance not started
345+ if (response == null ) {
346+ Aria2Starter .refresh()
347+ delay(100L * i)
348+ continue
349+ }
350+ // send error, due to closed or timeout
351+ val gid = response.getOrNull()
352+ if (gid == null ) {
353+ Aria2Starter .refresh()
354+ delay(100L * i)
355+ continue
356+ }
357+ DownloadListener .insert(gid, requestId)
358+ isValid = true
359+ break
360+ }
361+ if (! isValid) {
362+ throw ErrorLoadingException (" Unable to connect to internal server" )
363+ }
340364 }
341365
342366 else -> Unit
0 commit comments