Skip to content

Commit

Permalink
Use shared memory to transact EncodedTorrentInfo, close #1131.
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Nov 3, 2024
1 parent 623d9a5 commit e1343c1
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import kotlinx.coroutines.withContext
import kotlinx.io.files.Path
import me.him188.ani.app.domain.torrent.IRemoteTorrentDownloader
import me.him188.ani.app.domain.torrent.ITorrentDownloaderStatsCallback
import me.him188.ani.app.domain.torrent.parcel.PEncodedTorrentInfo
import me.him188.ani.app.domain.torrent.parcel.PTorrentDownloaderStats
import me.him188.ani.app.domain.torrent.parcel.toParceled
import me.him188.ani.app.torrent.api.TorrentDownloader
import me.him188.ani.app.torrent.api.TorrentLibInfo
import me.him188.ani.app.torrent.api.TorrentSession
Expand Down Expand Up @@ -68,13 +68,13 @@ class RemoteTorrentDownloader(
): TorrentSession {
return withContext(Dispatchers.IO_) {
RemoteTorrentSession {
call { startDownload(PEncodedTorrentInfo(data.data), overrideSaveDir?.absolutePath) }
call { startDownload(data.toParceled(), overrideSaveDir?.absolutePath) }
}
}
}

override fun getSaveDirForTorrent(data: EncodedTorrentInfo): SystemPath {
val remotePath = call { getSaveDirForTorrent(PEncodedTorrentInfo(data.data)) }
val remotePath = call { getSaveDirForTorrent(data.toParceled()) }
return Path(remotePath).inSystem
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,30 @@

package me.him188.ani.app.domain.torrent.parcel

import android.os.Build
import android.os.Parcelable
import android.os.SharedMemory
import androidx.annotation.RequiresApi
import kotlinx.parcelize.Parcelize
import me.him188.ani.app.torrent.api.files.EncodedTorrentInfo

@RequiresApi(Build.VERSION_CODES.O_MR1)
@Parcelize
class PEncodedTorrentInfo(
val data: ByteArray
val dataMem: SharedMemory,
val length: Int
) : Parcelable {
fun toEncodedTorrentInfo(): EncodedTorrentInfo {
val buffer = dataMem.mapReadOnly()
val data = ByteArray(length).apply { buffer.get(this) }
dataMem.close()
return EncodedTorrentInfo.createRaw(data)
}
}

@RequiresApi(Build.VERSION_CODES.O_MR1)
fun EncodedTorrentInfo.toParceled(): PEncodedTorrentInfo {
val dataMem = SharedMemory.create("encoded_torrent_info${data.hashCode()}", data.size)
dataMem.mapReadWrite().apply { put(data, 0, data.size) }
return PEncodedTorrentInfo(dataMem, data.size)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

package me.him188.ani.app.domain.torrent.service.proxy

import android.os.Build
import androidx.annotation.RequiresApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.io.files.Path
import me.him188.ani.app.domain.torrent.IDisposableHandle
import me.him188.ani.app.domain.torrent.IRemoteTorrentDownloader
Expand All @@ -20,21 +23,19 @@ import me.him188.ani.app.domain.torrent.ITorrentDownloaderStatsCallback
import me.him188.ani.app.domain.torrent.parcel.PEncodedTorrentInfo
import me.him188.ani.app.domain.torrent.parcel.PTorrentDownloaderStats
import me.him188.ani.app.domain.torrent.parcel.PTorrentLibInfo
import me.him188.ani.app.domain.torrent.parcel.toParceled
import me.him188.ani.app.torrent.api.TorrentDownloader
import me.him188.ani.app.torrent.api.files.EncodedTorrentInfo
import me.him188.ani.utils.coroutines.IO_
import me.him188.ani.utils.coroutines.childScope
import me.him188.ani.utils.io.absolutePath
import me.him188.ani.utils.io.inSystem
import me.him188.ani.utils.logging.logger
import kotlin.coroutines.CoroutineContext

class TorrentDownloaderProxy(
private val delegate: TorrentDownloader,
context: CoroutineContext,
) : IRemoteTorrentDownloader.Stub() {
private val scope = context.childScope()
private val logger = logger<TorrentDownloaderProxy>()

override fun getTotalStatus(flow: ITorrentDownloaderStatsCallback?): IDisposableHandle {
val job = scope.launch(Dispatchers.IO_) {
Expand Down Expand Up @@ -65,25 +66,28 @@ class TorrentDownloaderProxy(
)
}

@RequiresApi(Build.VERSION_CODES.O_MR1)
override fun fetchTorrent(uri: String?, timeoutSeconds: Int): PEncodedTorrentInfo? {
if (uri == null) return null

val fetched = runBlocking { delegate.fetchTorrent(uri, timeoutSeconds) }
return PEncodedTorrentInfo(fetched.data)
return fetched.toParceled()
}

@RequiresApi(Build.VERSION_CODES.O_MR1)
override fun startDownload(data: PEncodedTorrentInfo?, overrideSaveDir: String?): IRemoteTorrentSession? {
if (data == null) return null

val session = runBlocking {
delegate.startDownload(
EncodedTorrentInfo.createRaw(data.data),
overrideSaveDir = overrideSaveDir?.run { Path(this).inSystem }
withContext(Dispatchers.IO_) { data.toEncodedTorrentInfo() },
overrideSaveDir = overrideSaveDir?.run { Path(this).inSystem },
)
}
return TorrentSessionProxy(session, scope.coroutineContext)
}

@RequiresApi(Build.VERSION_CODES.O_MR1)
override fun getSaveDirForTorrent(data: PEncodedTorrentInfo?): String? {
if (data == null) return null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.torrent.anitorrent

import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
Expand Down Expand Up @@ -388,5 +396,163 @@ http://tracker4.itzmx.com:2710/announce
udp://tracker.opentrackr.org:1337/announce
http://tracker.opentrackr.org:1337/announce
http://1337.abcvg.info:80/announce
http://bt.okmp3.ru:2710/announce
http://ipv4announce.sktorrent.eu:6969/announce
http://ipv6.rer.lol:6969/announce
http://nyaa.tracker.wf:7777/announce
http://public.tracker.vraphim.com:6969/announce
http://saltwood.top:6969/announce
http://t.nyaatracker.com:80/announce
http://taciturn-shadow.spb.ru:6969/announce
http://tk.greedland.net:80/announce
http://torrentsmd.com:8080/announce
http://tracker-zhuqiy.dgj055.icu:80/announce
http://tracker.bt4g.com:2095/announce
http://tracker.electro-torrent.pl:80/announce
http://tracker.tfile.co:80/announce
http://www.all4nothin.net:80/announce.php
http://www.wareztorrent.com:80/announce
https://1337.abcvg.info:443/announce
https://p2p.azu.red:443/announce
https://tr.abir.ga:443/announce
https://tr.burnabyhighstar.com:443/announce
https://tracker.gcrenwp.top:443/announce
https://tracker.kuroy.me:443/announce
https://tracker.leechshield.link:443/announce
https://tracker.lilithraws.org:443/announce
https://tracker.tamersunion.org:443/announce
https://tracker.yemekyedim.com:443/announce
https://tracker1.520.jp:443/announce
https://trackers.mlsub.net:443/announce
https://www.peckservers.com:9443/announce
udp://bandito.byterunner.io:6969/announce
udp://d40969.acod.regrucolo.ru:6969/announce
udp://ec2-18-191-163-220.us-east-2.compute.amazonaws.com:6969/announce
udp://evan.im:6969/announce
udp://exodus.desync.com:6969/announce
udp://ismaarino.com:1234/announce
udp://leet-tracker.moe:1337/announce
udp://martin-gebhardt.eu:25/announce
udp://moonburrow.club:6969/announce
udp://ns-1.x-fins.com:6969/announce
udp://ns1.monolithindustries.com:6969/announce
udp://odd-hd.fr:6969/announce
udp://open.demonii.com:1337/announce
udp://open.free-tracker.ga:6969/announce
udp://open.stealth.si:80/announce
udp://open.tracker.cl:1337/announce
udp://open.tracker.ink:6969/announce
udp://opentor.org:2710/announce
udp://opentracker.io:6969/announce
udp://p2p.publictracker.xyz:6969/announce
udp://p4p.arenabg.com:1337/announce
udp://retracker.hotplug.ru:2710/announce
udp://retracker01-msk-virt.corbina.net:80/announce
udp://seedpeer.net:6969/announce
udp://serpb.vpsburti.com:6969/announce
udp://thetracker.org:80/announce
udp://tr4ck3r.duckdns.org:6969/announce
udp://trackarr.org:6969/announce
udp://tracker.0x7c0.com:6969/announce
udp://tracker.bittor.pw:1337/announce
udp://tracker.breizh.pm:6969/announce
udp://tracker.deadorbit.nl:6969/announce
udp://tracker.dler.com:6969/announce
udp://tracker.doko.moe:6969/announce
udp://tracker.dump.cl:6969/announce
udp://tracker.filemail.com:6969/announce
udp://tracker.fnix.net:6969/announce
udp://tracker.gmi.gd:6969/announce
udp://tracker.ololosh.space:6969/announce
udp://tracker.opentrackr.org:1337/announce
udp://tracker.skyts.net:6969/announce
udp://tracker.srv00.com:6969/announce
udp://tracker.theoks.net:6969/announce
udp://tracker.torrent.eu.org:451/announce
udp://tracker.tryhackx.org:6969/announce
udp://tracker1.bt.moack.co.kr:80/announce
udp://ttk2.nbaonlineservice.com:6969/announce
udp://z.mercax.com:53/announce
wss://tracker.openwebtorrent.com:443/announce
""".trimIndent().lineSequence().filter { it.isNotBlank() }.joinToString()
}

0 comments on commit e1343c1

Please sign in to comment.