-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
9 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 8 additions & 51 deletions
59
src/main/kotlin/moe/fuqiuluo/unidbg/session/SessionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,31 @@ | ||
package moe.fuqiuluo.unidbg.session | ||
|
||
import CONFIG | ||
import com.github.unidbg.worker.WorkerPool | ||
import com.github.unidbg.worker.WorkerPoolFactory | ||
import moe.fuqiuluo.api.BlackListError | ||
import moe.fuqiuluo.comm.EnvData | ||
import java.util.concurrent.ConcurrentHashMap | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.concurrent.timer | ||
|
||
object SessionManager { | ||
private var workerPoolMap = hashMapOf<String, WorkerPool>() | ||
private val envDataByUin = ConcurrentHashMap<Long, EnvData>() | ||
private val uin0 ="0"; | ||
init { | ||
if (CONFIG.shareToken) { | ||
timer("reload", false, 1000L * 60 * 40, 1000L * 60 * 40) { | ||
workerPoolMap[uin0]?.close() | ||
} | ||
} | ||
} | ||
|
||
fun get(uin: Long): Session? { | ||
if (uin !in this) { | ||
return null | ||
} | ||
private val sessionMap = ConcurrentHashMap<Long, Session>() | ||
|
||
if (CONFIG.shareToken) { | ||
val envData = envDataByUin[uin]!! | ||
return workerPoolMap[uin.toString()]?.borrow<Session?>(3 * 1000L, TimeUnit.MILLISECONDS).also { | ||
val env = it?.vm?.envData | ||
if (env != null && env.uin != uin) { | ||
env.uin = uin | ||
env.androidId = envData.androidId | ||
env.code = envData.code | ||
env.guid = envData.guid | ||
env.packageName = envData.packageName | ||
env.qua = envData.qua | ||
env.version = envData.version | ||
env.qimei36 = envData.qimei36 | ||
} | ||
} | ||
} else { | ||
return workerPoolMap[uin.toString()]?.borrow(3 * 1000L, TimeUnit.MILLISECONDS) | ||
} | ||
operator fun get(uin: Long): Session? { | ||
return sessionMap[uin] | ||
} | ||
|
||
operator fun contains(uin: Long) = envDataByUin.containsKey(uin) | ||
operator fun contains(uin: Long) = sessionMap.containsKey(uin) | ||
|
||
fun register(envData: EnvData) { | ||
if (CONFIG.blackList?.contains(envData.uin) == true) { | ||
throw BlackListError | ||
} | ||
if (envData.uin in this && !CONFIG.shareToken) { | ||
if (envData.uin in this) { | ||
close(envData.uin) | ||
} | ||
|
||
envDataByUin[envData.uin] = envData | ||
if(workerPoolMap.containsKey(uin0) && CONFIG.shareToken) { | ||
return | ||
} | ||
workerPoolMap[if (CONFIG.shareToken) uin0 else envData.uin.toString()] = WorkerPoolFactory.create({ pool -> | ||
Session(envData, pool) | ||
}, CONFIG.count) | ||
sessionMap[envData.uin] = Session(envData) | ||
} | ||
|
||
fun close(uin: Long) { | ||
envDataByUin.remove(uin) | ||
if (!CONFIG.shareToken) { | ||
workerPoolMap[uin.toString()]?.close() | ||
} | ||
sessionMap[uin]?.vm?.destroy() | ||
sessionMap.remove(uin) | ||
} | ||
} |