Commit 5485ffe8 authored by Him188's avatar Him188

Support Bot.isOnline, close #342

parent ab464388
...@@ -45,6 +45,8 @@ internal abstract class BotImpl<N : BotNetworkHandler> constructor( ...@@ -45,6 +45,8 @@ internal abstract class BotImpl<N : BotNetworkHandler> constructor(
@Suppress("PropertyName") @Suppress("PropertyName")
internal lateinit var _network: N internal lateinit var _network: N
override val isOnline: Boolean get() = _network.areYouOk()
/** /**
* Close server connection, resend login packet, BUT DOESN'T [BotNetworkHandler.init] * Close server connection, resend login packet, BUT DOESN'T [BotNetworkHandler.init]
*/ */
......
...@@ -8,7 +8,13 @@ ...@@ -8,7 +8,13 @@
*/ */
@file:OptIn(LowLevelAPI::class) @file:OptIn(LowLevelAPI::class)
@file:Suppress("EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR", "NOTHING_TO_INLINE") @file:Suppress(
"EXPERIMENTAL_API_USAGE",
"DEPRECATION_ERROR",
"NOTHING_TO_INLINE",
"INVISIBLE_MEMBER",
"INVISIBLE_REFERENCE"
)
package net.mamoe.mirai.qqandroid.contact package net.mamoe.mirai.qqandroid.contact
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:Suppress("EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR") @file:Suppress("EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package net.mamoe.mirai.qqandroid.contact package net.mamoe.mirai.qqandroid.contact
......
...@@ -86,10 +86,10 @@ internal abstract class BotNetworkHandler : CoroutineScope { ...@@ -86,10 +86,10 @@ internal abstract class BotNetworkHandler : CoroutineScope {
/** /**
* 当 [Bot] 正常运作时, 这个函数将一直挂起协程到 [Bot] 被 [Bot.close] * 当 [Bot] 正常运作时, 这个函数将一直挂起协程到 [Bot] 被 [Bot.close]
* 当 [Bot] 离线时, 这个函数立即返回.
*/ */
abstract suspend fun join() abstract suspend fun join()
// cool name
abstract fun areYouOk(): Boolean abstract fun areYouOk(): Boolean
......
...@@ -64,8 +64,10 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo ...@@ -64,8 +64,10 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
private val packetReceiveLock: Mutex = Mutex() private val packetReceiveLock: Mutex = Mutex()
override fun areYouOk(): Boolean { override fun areYouOk(): Boolean {
return this.isActive && ::channel.isInitialized && channel.isOpen return kotlin.runCatching {
&& heartbeatJob?.isActive == true && _packetReceiverJob?.isActive == true this.isActive && ::channel.isInitialized && channel.isOpen
&& heartbeatJob?.isActive == true && _packetReceiverJob?.isActive == true
}.getOrElse { false }
} }
private suspend fun startPacketReceiverJobOrKill(cancelCause: CancellationException? = null): Job { private suspend fun startPacketReceiverJobOrKill(cancelCause: CancellationException? = null): Job {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive package net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive
import kotlinx.io.core.ByteReadPacket import kotlinx.io.core.ByteReadPacket
......
...@@ -25,6 +25,7 @@ import net.mamoe.mirai.network.LoginFailedException ...@@ -25,6 +25,7 @@ import net.mamoe.mirai.network.LoginFailedException
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext import kotlin.coroutines.EmptyCoroutineContext
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic import kotlin.jvm.JvmSynthetic
...@@ -41,22 +42,25 @@ suspend inline fun <B : Bot> B.alsoLogin(): B = also { login() } ...@@ -41,22 +42,25 @@ suspend inline fun <B : Bot> B.alsoLogin(): B = also { login() }
* 有关 [Bot] 生命管理, 请查看 [BotConfiguration.inheritCoroutineContext] * 有关 [Bot] 生命管理, 请查看 [BotConfiguration.inheritCoroutineContext]
* *
* @see Contact 联系人 * @see Contact 联系人
* @see kotlinx.coroutines.isActive 判断 [Bot] 是否正常运行中. (在线, 且没有被 [close]) * @see kotlinx.coroutines.isActive 判断 [Bot] 是否正常运行中. (协程正常运行) (但不能判断是否在线, 需使用 [isOnline])
* *
* @see BotFactory 构造 [Bot] 的工厂, [Bot] 唯一的构造方式. * @see BotFactory 构造 [Bot] 的工厂, [Bot] 唯一的构造方式.
*/ */
@Suppress("INAPPLICABLE_JVM_NAME", "EXPOSED_SUPER_CLASS") @Suppress("INAPPLICABLE_JVM_NAME", "EXPOSED_SUPER_CLASS")
abstract class Bot( abstract class Bot internal constructor(
val configuration: BotConfiguration val configuration: BotConfiguration
) : CoroutineScope, LowLevelBotAPIAccessor, BotJavaFriendlyAPI, ContactOrBot { ) : CoroutineScope, LowLevelBotAPIAccessor, BotJavaFriendlyAPI, ContactOrBot {
final override val coroutineContext: CoroutineContext = final override val coroutineContext: CoroutineContext =
configuration.parentCoroutineContext + SupervisorJob(configuration.parentCoroutineContext[Job]) + configuration.parentCoroutineContext
(configuration.parentCoroutineContext[CoroutineExceptionHandler] .plus(SupervisorJob(configuration.parentCoroutineContext[Job]))
?: CoroutineExceptionHandler { _, e -> .plus(configuration.parentCoroutineContext[CoroutineExceptionHandler]
logger.error("An exception was thrown under a coroutine of Bot", e) ?: CoroutineExceptionHandler { _, e ->
}) logger.error("An exception was thrown under a coroutine of Bot", e)
}
)
companion object { companion object {
@JvmField
@Suppress("ObjectPropertyName") @Suppress("ObjectPropertyName")
internal val _instances: LockFreeLinkedList<WeakRef<Bot>> = LockFreeLinkedList() internal val _instances: LockFreeLinkedList<WeakRef<Bot>> = LockFreeLinkedList()
...@@ -133,6 +137,12 @@ abstract class Bot( ...@@ -133,6 +137,12 @@ abstract class Bot(
*/ */
abstract val logger: MiraiLogger abstract val logger: MiraiLogger
/**
* 判断 Bot 是否在线 (可正常收发消息)
*/
@SinceMirai("1.0.1")
abstract val isOnline: Boolean
// region contacts // region contacts
/** /**
...@@ -206,6 +216,7 @@ abstract class Bot( ...@@ -206,6 +216,7 @@ abstract class Bot(
* *
* @see Image.queryUrl [Image] 的扩展函数 * @see Image.queryUrl [Image] 的扩展函数
*/ */
@PlannedRemoval("1.2.0")
@Deprecated( @Deprecated(
"use extension.", "use extension.",
replaceWith = ReplaceWith("image.queryUrl()", imports = ["net.mamoe.mirai.message.data.queryUrl"]) replaceWith = ReplaceWith("image.queryUrl()", imports = ["net.mamoe.mirai.message.data.queryUrl"])
...@@ -236,6 +247,7 @@ abstract class Bot( ...@@ -236,6 +247,7 @@ abstract class Bot(
* *
* @param event 好友验证的事件对象 * @param event 好友验证的事件对象
*/ */
@PlannedRemoval("1.2.0")
@Deprecated("use member function.", replaceWith = ReplaceWith("event.accept()")) @Deprecated("use member function.", replaceWith = ReplaceWith("event.accept()"))
@JvmSynthetic @JvmSynthetic
abstract suspend fun acceptNewFriendRequest(event: NewFriendRequestEvent) abstract suspend fun acceptNewFriendRequest(event: NewFriendRequestEvent)
...@@ -246,6 +258,7 @@ abstract class Bot( ...@@ -246,6 +258,7 @@ abstract class Bot(
* @param event 好友验证的事件对象 * @param event 好友验证的事件对象
* @param blackList 拒绝后是否拉入黑名单 * @param blackList 拒绝后是否拉入黑名单
*/ */
@PlannedRemoval("1.2.0")
@Deprecated("use member function.", replaceWith = ReplaceWith("event.reject(blackList)")) @Deprecated("use member function.", replaceWith = ReplaceWith("event.reject(blackList)"))
@JvmSynthetic @JvmSynthetic
abstract suspend fun rejectNewFriendRequest(event: NewFriendRequestEvent, blackList: Boolean = false) abstract suspend fun rejectNewFriendRequest(event: NewFriendRequestEvent, blackList: Boolean = false)
...@@ -255,6 +268,7 @@ abstract class Bot( ...@@ -255,6 +268,7 @@ abstract class Bot(
* *
* @param event 加群验证的事件对象 * @param event 加群验证的事件对象
*/ */
@PlannedRemoval("1.2.0")
@Deprecated("use member function.", replaceWith = ReplaceWith("event.accept()")) @Deprecated("use member function.", replaceWith = ReplaceWith("event.accept()"))
@JvmSynthetic @JvmSynthetic
abstract suspend fun acceptMemberJoinRequest(event: MemberJoinRequestEvent) abstract suspend fun acceptMemberJoinRequest(event: MemberJoinRequestEvent)
...@@ -265,6 +279,7 @@ abstract class Bot( ...@@ -265,6 +279,7 @@ abstract class Bot(
* @param event 加群验证的事件对象 * @param event 加群验证的事件对象
* @param blackList 拒绝后是否拉入黑名单 * @param blackList 拒绝后是否拉入黑名单
*/ */
@PlannedRemoval("1.2.0")
@Deprecated("use member function.", replaceWith = ReplaceWith("event.reject(blackList)")) @Deprecated("use member function.", replaceWith = ReplaceWith("event.reject(blackList)"))
@JvmSynthetic @JvmSynthetic
abstract suspend fun rejectMemberJoinRequest(event: MemberJoinRequestEvent, blackList: Boolean = false) abstract suspend fun rejectMemberJoinRequest(event: MemberJoinRequestEvent, blackList: Boolean = false)
...@@ -275,6 +290,7 @@ abstract class Bot( ...@@ -275,6 +290,7 @@ abstract class Bot(
* @param event 加群验证的事件对象 * @param event 加群验证的事件对象
* @param blackList 忽略后是否拉入黑名单 * @param blackList 忽略后是否拉入黑名单
*/ */
@PlannedRemoval("1.2.0")
@Deprecated("use member function.", replaceWith = ReplaceWith("event.ignore(blackList)")) @Deprecated("use member function.", replaceWith = ReplaceWith("event.ignore(blackList)"))
@JvmSynthetic @JvmSynthetic
abstract suspend fun ignoreMemberJoinRequest(event: MemberJoinRequestEvent, blackList: Boolean = false) abstract suspend fun ignoreMemberJoinRequest(event: MemberJoinRequestEvent, blackList: Boolean = false)
...@@ -284,6 +300,7 @@ abstract class Bot( ...@@ -284,6 +300,7 @@ abstract class Bot(
* *
* @param event 邀请入群的事件对象 * @param event 邀请入群的事件对象
*/ */
@PlannedRemoval("1.2.0")
@Deprecated("use member function.", replaceWith = ReplaceWith("event.accept")) @Deprecated("use member function.", replaceWith = ReplaceWith("event.accept"))
@JvmSynthetic @JvmSynthetic
abstract suspend fun acceptInvitedJoinGroupRequest(event: BotInvitedJoinGroupRequestEvent) abstract suspend fun acceptInvitedJoinGroupRequest(event: BotInvitedJoinGroupRequestEvent)
...@@ -293,6 +310,7 @@ abstract class Bot( ...@@ -293,6 +310,7 @@ abstract class Bot(
* *
* @param event 邀请入群的事件对象 * @param event 邀请入群的事件对象
*/ */
@PlannedRemoval("1.2.0")
@Deprecated("use member function.", replaceWith = ReplaceWith("event.ignore")) @Deprecated("use member function.", replaceWith = ReplaceWith("event.ignore"))
@JvmSynthetic @JvmSynthetic
abstract suspend fun ignoreInvitedJoinGroupRequest(event: BotInvitedJoinGroupRequestEvent) abstract suspend fun ignoreInvitedJoinGroupRequest(event: BotInvitedJoinGroupRequestEvent)
...@@ -322,7 +340,8 @@ inline val Bot.supervisorJob: CompletableJob ...@@ -322,7 +340,8 @@ inline val Bot.supervisorJob: CompletableJob
get() = this.coroutineContext[Job] as CompletableJob get() = this.coroutineContext[Job] as CompletableJob
/** /**
* 挂起协程直到 [Bot] 下线. * 挂起协程直到 [Bot] 协程被关闭 ([Bot.close]).
* 即使 [Bot] 离线, 也会等待直到协程关闭.
*/ */
@JvmSynthetic @JvmSynthetic
suspend inline fun Bot.join() = this.coroutineContext[Job]!!.join() suspend inline fun Bot.join() = this.coroutineContext[Job]!!.join()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment