Skip to content

Commit

Permalink
fix: 서버가 종료되지 않아 timeout 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
cccgh5 committed Apr 21, 2024
1 parent 44e121b commit 726d562
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,16 @@ abstract class HQBukkitPlugin : JavaPlugin, HQPlugin, KoinComponent, CoroutineSc
job.coroutineContext[CoroutineDispatcher.Key] != Dispatchers.BukkitMain && job.coroutineContext[CoroutineDispatcher.Key] != Dispatchers.BukkitAsync
}.filter {
it.job.children.count() == 0
}.toList().joinAll()
}.toList().forEach { job ->
logger.info("${AnsiColor.CYAN}Disabling...[${job.coroutineContext[CoroutineName]?.name} routine]${AnsiColor.RESET}")
if (withTimeoutOrNull(1000) { job.join() } == null) {
logger.info("${AnsiColor.CYAN}Timeout-Cancel [${job.coroutineContext[CoroutineName]?.name} routine]${AnsiColor.RESET}")
job.cancelAndJoin()
logger.info("${AnsiColor.CYAN}Cancel finished. [${job.coroutineContext[CoroutineName]?.name} routine]${AnsiColor.RESET}")
} else {
logger.info("${AnsiColor.CYAN}Finished. [${job.coroutineContext[CoroutineName]?.name} routine]${AnsiColor.RESET}")
}
}

logger.info("${AnsiColor.CYAN}Disabling...${AnsiColor.RESET}")
onPreDisable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NettyClientBootstrap(
Direction.OUTBOUND.registerPacket(BroadcastPacket::class)
Direction.OUTBOUND.registerPacket(MessagePacket::class)
}

bootup = false
future.whenCompleteAsync { channel, throwable ->
if (throwable != null) {
Expand All @@ -43,6 +44,7 @@ class NettyClientBootstrap(
}

val handlerBoss = channel.pipeline().get(BossHandler::class.java)

handlerBoss.setDisconnectionHandler {
if (!plugin.isEnabled) return@setDisconnectionHandler
it.setEnabled(false)
Expand All @@ -58,17 +60,18 @@ class NettyClientBootstrap(
}

handlerBoss.setPacketPreprocessHandler { packet, wrapper ->
if (!plugin.isEnabled) return@setPacketPreprocessHandler
plugin.server.pluginManager.callEvent(AsyncNettyPacketReceivedEvent(wrapper, packet))
plugin.server.scheduler.runTask(plugin, Runnable {
plugin.server.pluginManager.callEvent(NettyPacketReceivedEvent(wrapper, packet))
})
}

logger.info("netty-client initialization success!")

handlerBoss.connectionState = ConnectionState.HANDSHAKING
channel.writeAndFlush(HandShakePacket(plugin.server.port))
channel.pipeline().addFirst("timeout-handler", TimeOutHandler(5L, TimeUnit.SECONDS))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ class NettyModule(
(nettyService as HQNettyServiceImpl).enabled = config.getBoolean("netty.enabled")
//nettyEnabled = config.getBoolean("netty.enabled")

channelHandler.apply {
Direction.INBOUND.addListener(HandShakePacket::class, this)
}

Direction.INBOUND.addListener(HandShakePacket::class, channelHandler)
Direction.INBOUND.addListener(ShutdownPacket::class) { packet, channel ->
if (packet.shutdownTarget) {
channel.setEnabled(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.hqservice.framework.database.repository.player.packet.handler

import kotlinx.coroutines.*
import kr.hqservice.framework.bukkit.core.coroutine.element.TeardownOptionCoroutineContextElement
import kr.hqservice.framework.bukkit.core.coroutine.extension.BukkitMain
import kr.hqservice.framework.bukkit.core.listener.Listener
import kr.hqservice.framework.bukkit.core.listener.Subscribe
Expand Down Expand Up @@ -51,7 +52,7 @@ class PlayerConnectionPacketHandler(
}

private fun saveAndClear(player: Player): Job {
return coroutineScope.launch(Dispatchers.IO) {
return coroutineScope.launch(Dispatchers.IO + CoroutineName("PlayerRepositorySaveRoutine")) {
playerRepositoryRegistry.getAll().forEach { repository ->
onSave(player, repository)
repository.remove(player.uniqueId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ class PlayerConnectionListener(

@EventHandler
fun playerConnectedEvent(event: ServerConnectedEvent) {
if (event.player.server != null) return
val nettyPlayer = NettyPlayerImpl(
event.player.name,
event.player.displayName,
event.player.uniqueId,
getChannelByAddress(event.server.address),
)

val packet = PlayerConnectionPacket(
nettyPlayer,
PlayerConnectionState.CONNECTED,
Expand Down

0 comments on commit 726d562

Please sign in to comment.