Inline val

This commit is contained in:
Him188 2019-11-08 19:53:31 +08:00
parent 2f416a5dc1
commit c1e0ba721f

View File

@ -38,7 +38,7 @@ internal expect val NetworkDispatcher: CoroutineDispatcher
* *
* @see BotNetworkHandler * @see BotNetworkHandler
*/ */
internal class TIMBotNetworkHandler internal constructor(override val bot: Bot) : internal class TIMBotNetworkHandler internal constructor(override inline val bot: Bot) :
BotNetworkHandler<TIMBotNetworkHandler.BotSocketAdapter>, PacketHandlerList() { BotNetworkHandler<TIMBotNetworkHandler.BotSocketAdapter>, PacketHandlerList() {
override val coroutineContext: CoroutineContext = override val coroutineContext: CoroutineContext =
@ -55,6 +55,8 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
private var heartbeatJob: Job? = null private var heartbeatJob: Job? = null
private lateinit var userContext: CoroutineContext
override suspend fun addHandler(temporaryPacketHandler: TemporaryPacketHandler<*, *>) { override suspend fun addHandler(temporaryPacketHandler: TemporaryPacketHandler<*, *>) {
handlersLock.withLock { handlersLock.withLock {
@ -63,8 +65,9 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
temporaryPacketHandler.send(this[ActionPacketHandler].session) temporaryPacketHandler.send(this[ActionPacketHandler].session)
} }
override suspend fun login(configuration: BotConfiguration): LoginResult = override suspend fun login(configuration: BotConfiguration): LoginResult {
withContext(this.coroutineContext) { userContext = coroutineContext
return withContext(this.coroutineContext) {
TIMProtocol.SERVER_IP.forEach { ip -> TIMProtocol.SERVER_IP.forEach { ip ->
bot.logger.info("Connecting server $ip") bot.logger.info("Connecting server $ip")
socket = BotSocketAdapter(ip, configuration) socket = BotSocketAdapter(ip, configuration)
@ -79,6 +82,7 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
} }
return@withContext LoginResult.TIMEOUT return@withContext LoginResult.TIMEOUT
} }
}
internal var loginResult: CompletableDeferred<LoginResult> = CompletableDeferred() internal var loginResult: CompletableDeferred<LoginResult> = CompletableDeferred()
@ -135,7 +139,9 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
try { try {
channel.read(buffer)// JVM: withContext(IO) channel.read(buffer)// JVM: withContext(IO)
} catch (e: ClosedChannelException) { } catch (e: ClosedChannelException) {
close() withContext(userContext) {
close()
}
return return
} catch (e: ReadPacketInternalException) { } catch (e: ReadPacketInternalException) {
bot.logger.error("Socket channel read failed: ${e.message}") bot.logger.error("Socket channel read failed: ${e.message}")
@ -154,7 +160,7 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
} }
//buffer.resetForRead() //buffer.resetForRead()
launch { launch(CoroutineName("handleServerPacket")) {
// `.use`: Ensure that the packet is consumed **totally** // `.use`: Ensure that the packet is consumed **totally**
// so that all the buffers are released // so that all the buffers are released
ByteArrayPool.useInstance { ByteArrayPool.useInstance {
@ -260,7 +266,7 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
} }
} }
override suspend fun sendPacket(packet: OutgoingPacket): Unit = withContext(coroutineContext) { override suspend fun sendPacket(packet: OutgoingPacket): Unit = withContext(coroutineContext + CoroutineName("sendPacket")) {
check(channel.isOpen) { "channel is not open" } check(channel.isOpen) { "channel is not open" }
if (BeforePacketSendEvent(bot, packet).broadcast().cancelled) { if (BeforePacketSendEvent(bot, packet).broadcast().cancelled) {
@ -275,7 +281,10 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
check(channel.send(buffer) == shouldBeSent) { "Buffer is not entirely sent. Required sent length=$shouldBeSent, but after channel.send, buffer remains ${buffer.readBytes().toUHexString()}" }//JVM: withContext(IO) check(channel.send(buffer) == shouldBeSent) { "Buffer is not entirely sent. Required sent length=$shouldBeSent, but after channel.send, buffer remains ${buffer.readBytes().toUHexString()}" }//JVM: withContext(IO)
} catch (e: SendPacketInternalException) { } catch (e: SendPacketInternalException) {
bot.logger.error("Caught SendPacketInternalException: ${e.cause?.message}") bot.logger.error("Caught SendPacketInternalException: ${e.cause?.message}")
bot.reinitializeNetworkHandler(configuration, e)
withContext(userContext) {
bot.reinitializeNetworkHandler(configuration, e)
}
return@withContext return@withContext
} finally { } finally {
buffer.release(IoBuffer.Pool) buffer.release(IoBuffer.Pool)
@ -342,19 +351,20 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
SubmitPasswordResponseDecrypter -> SubmitPasswordResponseDecrypter(privateKey) SubmitPasswordResponseDecrypter -> SubmitPasswordResponseDecrypter(privateKey)
PrivateKey -> privateKey PrivateKey -> privateKey
SessionKey -> sessionKey SessionKey -> sessionKey
else -> {
error("No decrypter found") else -> error("No decrypter is found")
}
} as? D ?: error("Internal error: could not cast decrypter which is found for factory to class Decrypter") } as? D ?: error("Internal error: could not cast decrypter which is found for factory to class Decrypter")
suspend fun onPacketReceived(packet: Any) {//complex function, but it doesn't matter suspend fun onPacketReceived(packet: Any) {//complex function, but it doesn't matter
when (packet) { when (packet) {
is TouchPacket.TouchResponse -> { is TouchPacket.TouchResponse -> {
if (packet.serverIP != null) {//redirection if (packet.serverIP != null) {//redirection
socket.close() withContext(userContext) {
socket = BotSocketAdapter(packet.serverIP!!, socket.configuration) socket.close()
bot.logger.info("Redirecting to ${packet.serverIP}") socket = BotSocketAdapter(packet.serverIP!!, socket.configuration)
loginResult.complete(socket.resendTouch()) bot.logger.info("Redirecting to ${packet.serverIP}")
loginResult.complete(socket.resendTouch())
}
} else {//password submission } else {//password submission
this.loginIP = packet.loginIP this.loginIP = packet.loginIP
this.loginTime = packet.loginTime this.loginTime = packet.loginTime
@ -490,7 +500,7 @@ internal class TIMBotNetworkHandler internal constructor(override val bot: Bot)
} }
is RequestSessionPacket.SessionKeyResponse -> { is RequestSessionPacket.SessionKeyResponse -> {
sessionKey = packet.sessionKey!! sessionKey = packet.sessionKey
bot.logger.info("sessionKey = ${sessionKey.value.toUHexString()}") bot.logger.info("sessionKey = ${sessionKey.value.toUHexString()}")
heartbeatJob = launch { heartbeatJob = launch {