diff --git a/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt b/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt index ebe31f71..1ce94bdf 100644 --- a/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt +++ b/Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt @@ -20,6 +20,8 @@ import com.arialyy.aria.core.DuaContext import com.arialyy.aria.core.inf.IBlockManager import com.arialyy.aria.core.task.AbsTaskUtil import com.arialyy.aria.core.task.BlockManager +import com.arialyy.aria.core.task.TaskResp +import com.arialyy.aria.exception.AriaException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -47,7 +49,7 @@ internal class HttpDTaskUtil : AbsTaskUtil() { } override fun stop() { - TODO("Not yet implemented") + blockManager?.stop() } override fun start() { @@ -64,6 +66,14 @@ internal class HttpDTaskUtil : AbsTaskUtil() { addCoreInterceptor(HttpDBlockInterceptor()) addCoreInterceptor(HttpBlockThreadInterceptor()) val resp = interceptor() + if (resp == null || resp.code != TaskResp.CODE_SUCCESS) { + getTask().getTaskOption(HttpDTaskOption::class.java).taskListener.onFail( + false, + AriaException("start task fail, task interrupt, code: ${resp?.code ?: TaskResp.CODE_INTERRUPT}") + ) + blockManager?.stop() + return@launch + } Looper.loop() } } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IBlockManager.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IBlockManager.java index 8a03a771..9d1b1a2c 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IBlockManager.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IBlockManager.java @@ -42,6 +42,8 @@ public interface IBlockManager { void start(List threadTaskList); + void stop(); + void setBlockNum(int blockNum); void putUnfinishedBlock(BlockRecord record); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DBlockManager.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DBlockManager.kt index 146d2f01..47f462ec 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DBlockManager.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DBlockManager.kt @@ -30,6 +30,7 @@ import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import timber.log.Timber import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.ThreadFactory import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.TimeUnit.MILLISECONDS import java.util.concurrent.atomic.AtomicInteger @@ -42,11 +43,12 @@ class BlockManager(task: ITask) : IBlockManager { private val completedNum = AtomicInteger(0) // 完成的线程数 private val threadNum = task.getTaskOption(ITaskOption::class.java).threadNum private val scope = MainScope() - private val dispatcher = ThreadPoolExecutor( + private val threadPool = ThreadPoolExecutor( threadNum, threadNum, 0L, MILLISECONDS, - LinkedBlockingQueue() - ).asCoroutineDispatcher() + LinkedBlockingQueue(), + ) + private val dispatcher = threadPool.asCoroutineDispatcher() private var progress: Long = 0 //当前总进度 private lateinit var looper: Looper @@ -115,7 +117,7 @@ class BlockManager(task: ITask) : IBlockManager { unfinishedBlock.add(record) } - override fun getUnfinishedBlockList(): MutableList { + override fun getUnfinishedBlockList(): List { return unfinishedBlock } @@ -135,6 +137,10 @@ class BlockManager(task: ITask) : IBlockManager { } } + override fun stop() { + quitLooper() + } + override fun setBlockNum(blockNum: Int) { this.blockNum = blockNum }