Increased server stability

Fixed NPE in IoEventHandler
Fixed NPEs in PacketWriteQueue
Reworked PacketWriteQueue slightly so that instead of breaking at first null packet we continue past it and pop the whole queue along the way
This commit is contained in:
Ceikry
2022-06-27 14:09:44 +00:00
committed by Ryan
parent ee5bc78b62
commit 95414bc02b
2 changed files with 20 additions and 17 deletions
@@ -72,6 +72,7 @@ public class IoEventHandler {
} catch (IOException e) {
if (e.getMessage().contains("reset by peer")) {
session.disconnect();
if (session.getPlayer() != null)
session.getPlayer().clear(true);
} else e.printStackTrace();
}
@@ -6,6 +6,7 @@ import core.net.packet.out.*
import rs09.game.system.SystemLogger
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.NoSuchElementException
import kotlin.collections.ArrayList
class PacketWriteQueue : TickListener {
@@ -15,8 +16,8 @@ class PacketWriteQueue : TickListener {
companion object {
private val queueLock = ReentrantLock()
private val packetsToQueue = ArrayList<QueuedPacket<*>>(1000)
private val packetsToWrite = LinkedList<QueuedPacket<*>>()
private val packetsToQueue = ArrayList<QueuedPacket<*>?>(1000)
private val packetsToWrite = LinkedList<QueuedPacket<*>?>()
@JvmStatic
fun <T> handle(packet: OutgoingPacket<T>, context: T) {
@@ -40,24 +41,23 @@ class PacketWriteQueue : TickListener {
}
@JvmStatic
fun pop(): QueuedPacket<*>? {
return try {
packetsToWrite.pop()
fun flush() {
queueLock.lock()
var hasEnded = false
while (!hasEnded) {
try {
val packet = packetsToWrite.pop()
write(packet?.out ?: continue, packet.context ?: continue)
} catch (e: NoSuchElementException) {
null
hasEnded = true
}
}
@JvmStatic
fun flush() {
queueLock.lock()
var packet: QueuedPacket<*>?
while (pop().also { packet = it } != null)
write(packet?.out ?: break, packet?.context ?: break)
if (packetsToWrite.isNotEmpty()) {
SystemLogger.logWarn("Packet queue was NOT empty! Remaining packets: ${packetsToWrite.size}")
try {
for (pkt in packetsToWrite) SystemLogger.logWarn("${pkt.out.javaClass.simpleName} <- ${pkt.context}")
for (pkt: QueuedPacket<*>? in packetsToWrite) SystemLogger.logWarn("${pkt?.out?.javaClass?.simpleName ?: "NULL"} <- ${pkt?.context ?: "NULL"}")
} catch (e: Exception)
{
e.printStackTrace()
@@ -65,12 +65,14 @@ class PacketWriteQueue : TickListener {
packetsToWrite.clear()
}
}
queueLock.unlock()
val queueIter = packetsToQueue.iterator()
while (queueIter.hasNext()) {
packetsToWrite.add(queueIter.next())
queueIter.remove()
}
queueLock.unlock()
}
@Suppress("UNCHECKED_CAST")