From c70f3eabfeec4e5d83d004c8f4b10634c0dbc318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E6=B1=A0?= Date: Mon, 19 Feb 2024 11:30:22 +0800 Subject: [PATCH] `Shamrock`: typo BaseTransmitServlet.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 白池 --- .../shamrock/remote/service/HttpService.kt | 21 +++++++++---------- .../remote/service/WebSocketClientService.kt | 18 ++++++++-------- .../remote/service/WebSocketService.kt | 18 ++++++++-------- .../remote/service/api/BaseTransmitServlet.kt | 12 ++++------- .../remote/service/api/HttpTransmitServlet.kt | 5 ++--- .../service/api/WebSocketClientServlet.kt | 12 +++++------ .../service/api/WebSocketTransmitServlet.kt | 8 +++---- .../xposed/hooks/InitRemoteService.kt | 2 +- 8 files changed, 45 insertions(+), 51 deletions(-) diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/HttpService.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/HttpService.kt index c4882f98..ae6407c3 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/HttpService.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/HttpService.kt @@ -26,34 +26,33 @@ import moe.fuqiuluo.shamrock.remote.action.handlers.QuickOperation.quicklyReply import moe.fuqiuluo.shamrock.remote.service.api.GlobalEventTransmitter internal object HttpService: HttpTransmitServlet() { - private val jobList = arrayListOf() + private val subscribes = arrayListOf() - override fun submitFlowJob(job: Job) { - // HTTP 回调不会触发断连,无需释放之前的JOB - jobList.add(job) + override fun subscribe(job: Job) { + subscribes.add(job) } - override fun cancelFlowJobs() { - jobList.removeIf { + override fun unsubscribe() { + subscribes.removeIf { it.cancel() return@removeIf true } } - override fun initTransmitter() { - if (jobList.isNotEmpty()) return - submitFlowJob(GlobalScope.launch { + override fun init() { + if (subscribes.isNotEmpty()) return + subscribe(GlobalScope.launch { GlobalEventTransmitter.onMessageEvent { (record, event) -> val respond = pushTo(event) ?: return@onMessageEvent handleQuicklyReply(record, event.messageId, respond.bodyAsText()) } }) - submitFlowJob(GlobalScope.launch { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onNoticeEvent { event -> pushTo(event) } }) - submitFlowJob(GlobalScope.launch { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onRequestEvent { pushTo(it) } diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt index 52d970f6..c191502f 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt @@ -16,28 +16,28 @@ internal class WebSocketClientService( heartbeatInterval: Long, wsHeaders: Map ) : WebSocketClientServlet(address, heartbeatInterval, wsHeaders) { - private val eventJobList = mutableSetOf() + private val subscribes = mutableSetOf() init { startHeartbeatTimer() } - override fun submitFlowJob(job: Job) { - eventJobList.add(job) + override fun subscribe(job: Job) { + subscribes.add(job) } - override fun initTransmitter() { - submitFlowJob(GlobalScope.launch { + override fun init() { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onMessageEvent { (_, event) -> pushTo(event) } }) - submitFlowJob(GlobalScope.launch { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onNoticeEvent { event -> pushTo(event) } }) - submitFlowJob(GlobalScope.launch { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onRequestEvent { event -> pushTo(event) } @@ -45,8 +45,8 @@ internal class WebSocketClientService( LogCenter.log("WebSocketClientService: 初始化服务", Level.WARN) } - override fun cancelFlowJobs() { - eventJobList.removeIf { job -> + override fun unsubscribe() { + subscribes.removeIf { job -> job.cancel() return@removeIf true } diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketService.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketService.kt index 9dfdda2a..1e0dcfba 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketService.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketService.kt @@ -26,24 +26,24 @@ internal class WebSocketService( port: Int, heartbeatInterval: Long, ): WebSocketTransmitServlet(host, port, heartbeatInterval) { - private val eventJobList = mutableSetOf() + private val subscribes = mutableSetOf() - override fun submitFlowJob(job: Job) { - eventJobList.add(job) + override fun subscribe(job: Job) { + subscribes.add(job) } - override fun initTransmitter() { - submitFlowJob(GlobalScope.launch { + override fun init() { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onMessageEvent { (_, event) -> pushTo(event) } }) - submitFlowJob(GlobalScope.launch { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onNoticeEvent { event -> pushTo(event) } }) - submitFlowJob(GlobalScope.launch { + subscribe(GlobalScope.launch { GlobalEventTransmitter.onRequestEvent { event -> pushTo(event) } @@ -51,8 +51,8 @@ internal class WebSocketService( LogCenter.log("WebSocketService: 初始化服务", Level.WARN) } - override fun cancelFlowJobs() { - eventJobList.removeIf { job -> + override fun unsubscribe() { + subscribes.removeIf { job -> job.cancel() return@removeIf true } diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/BaseTransmitServlet.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/BaseTransmitServlet.kt index 12cdac21..a25e4eb1 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/BaseTransmitServlet.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/BaseTransmitServlet.kt @@ -1,24 +1,20 @@ package moe.fuqiuluo.shamrock.remote.service.api import com.tencent.mobileqq.app.QQAppInterface -import com.tencent.qqnt.kernel.nativeinterface.MsgElement -import com.tencent.qqnt.kernel.nativeinterface.MsgRecord import kotlinx.coroutines.Job -import moe.fuqiuluo.shamrock.remote.service.data.push.NoticeSubType -import moe.fuqiuluo.shamrock.remote.service.data.push.NoticeType import moe.fuqiuluo.shamrock.xposed.helper.AppRuntimeFetcher import oicq.wlogin_sdk.tools.MD5 internal interface BaseTransmitServlet { val address: String - fun allowTransmit(): Boolean + fun transmitAccess(): Boolean - fun submitFlowJob(job: Job) + fun subscribe(job: Job) - fun cancelFlowJobs() + fun unsubscribe() - fun initTransmitter() + fun init() val app: QQAppInterface get() = AppRuntimeFetcher.appRuntime as QQAppInterface diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/HttpTransmitServlet.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/HttpTransmitServlet.kt index 76522634..a666742f 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/HttpTransmitServlet.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/HttpTransmitServlet.kt @@ -8,7 +8,6 @@ import io.ktor.client.request.setBody import io.ktor.client.statement.HttpResponse import io.ktor.http.ContentType import io.ktor.http.contentType -import kotlinx.coroutines.Job import moe.fuqiuluo.shamrock.remote.service.config.ShamrockConfig import moe.fuqiuluo.shamrock.tools.GlobalClient import moe.fuqiuluo.shamrock.helper.Level @@ -22,12 +21,12 @@ import java.net.SocketException internal abstract class HttpTransmitServlet : BaseTransmitServlet { override val address: String by lazy { ShamrockConfig.getWebHookAddress() } - override fun allowTransmit(): Boolean { + override fun transmitAccess(): Boolean { return ShamrockConfig.allowWebHook() } protected suspend inline fun pushTo(body: T): HttpResponse? { - if (!allowTransmit()) return null + if (!transmitAccess()) return null try { if (address.startsWith("http://") || address.startsWith("https://")) { val response = GlobalClient.post(address) { diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt index dfce765b..09ca0746 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt @@ -45,7 +45,7 @@ internal abstract class WebSocketClientServlet( private var firstOpen = true private val sendLock = Mutex() - override fun allowTransmit(): Boolean { + override fun transmitAccess(): Boolean { return ShamrockConfig.openWebSocketClient() } @@ -90,9 +90,9 @@ internal abstract class WebSocketClientServlet( if (firstOpen) { firstOpen = false } else { - cancelFlowJobs() + unsubscribe() } - initTransmitter() + init() } override fun onClose(code: Int, reason: String?, remote: Boolean) { @@ -105,18 +105,18 @@ internal abstract class WebSocketClientServlet( } } LogCenter.log("WebSocketClient onClose: $code, $reason, $remote") - cancelFlowJobs() + unsubscribe() connectedClients.remove(url) } override fun onError(ex: Exception?) { LogCenter.log("WebSocketClient onError: ${ex?.message}") - cancelFlowJobs() + unsubscribe() connectedClients.remove(url) } protected suspend inline fun pushTo(body: T) { - if (!allowTransmit() || isClosed || isClosing) return + if (!transmitAccess() || isClosed || isClosing) return try { sendLock.withLock { send(GlobalJson.encodeToString(body)) diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt index 2e35b594..80d8ee70 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt @@ -48,7 +48,7 @@ internal abstract class WebSocketTransmitServlet( override val address: String get() = "-" - override fun allowTransmit(): Boolean { + override fun transmitAccess(): Boolean { return ShamrockConfig.openWebSocket() } @@ -129,16 +129,16 @@ internal abstract class WebSocketTransmitServlet( override fun onError(conn: WebSocket, ex: Exception?) { LogCenter.log("WSServer Error: " + ex?.stackTraceToString(), Level.ERROR) - cancelFlowJobs() + unsubscribe() } override fun onStart() { LogCenter.log("WSServer start running on ws://${getAddress()}!") - initTransmitter() + init() } protected suspend inline fun pushTo(body: T) { - if(!allowTransmit()) return + if(!transmitAccess()) return try { sendLock.withLock { broadcastTextEvent(GlobalJson.encodeToString(body)) diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/hooks/InitRemoteService.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/hooks/InitRemoteService.kt index 65b20c48..54d09d9d 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/hooks/InitRemoteService.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/hooks/InitRemoteService.kt @@ -38,7 +38,7 @@ internal class InitRemoteService : IAction { if (ShamrockConfig.allowWebHook()) { - HttpService.initTransmitter() + HttpService.init() } val runtime = AppRuntimeFetcher.appRuntime