From de735cc5b8f8af484dd1e600561e6564c2804663 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 18 Apr 2024 14:31:08 +0700 Subject: [PATCH] Send network packets right away --- .../kstarbound/client/ClientConnection.kt | 6 +-- .../kstarbound/client/StarboundClient.kt | 5 +-- .../kstarbound/network/Connection.kt | 14 +------ .../network/packets/ProtocolResponsePacket.kt | 2 +- .../kstarbound/server/ChatHandler.kt | 4 +- .../kstarbound/server/ServerConnection.kt | 41 ++++++++----------- .../kstarbound/server/StarboundServer.kt | 2 +- .../server/world/ServerWorldTracker.kt | 2 +- 8 files changed, 27 insertions(+), 49 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientConnection.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientConnection.kt index 931e2463..9506f07f 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientConnection.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientConnection.kt @@ -59,7 +59,7 @@ class ClientConnection(val client: StarboundClient, type: ConnectionType) : Conn private var clientStateNetVersion = 0L - override fun flush() { + fun tick() { if (!pendingDisconnect && isConnected) { val entries = rpc.write() @@ -74,8 +74,6 @@ class ClientConnection(val client: StarboundClient, type: ConnectionType) : Conn clientStateNetVersion = new } - - super.flush() } private var pendingDisconnect = false @@ -94,7 +92,7 @@ class ClientConnection(val client: StarboundClient, type: ConnectionType) : Conn if (channel.isOpen) { pendingDisconnect = true - sendAndFlush(ClientDisconnectRequestPacket) + send(ClientDisconnectRequestPacket) } else { disconnectNow() } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt index 34e900d1..7d609ed4 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt @@ -774,7 +774,7 @@ class StarboundClient private constructor(val clientID: Int) : BlockableEventLoo if (world != null && Starbound.initialized) world.tick(Starbound.TIMESTEP) - activeConnection?.flush() + activeConnection?.tick() return } @@ -833,8 +833,7 @@ class StarboundClient private constructor(val clientID: Int) : BlockableEventLoo GLFW.glfwPollEvents() performOpenGLCleanup() - - activeConnection?.flush() + activeConnection?.tick() } private fun tick() { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/network/Connection.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/network/Connection.kt index f0b5d217..be7bc2a8 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/network/Connection.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/network/Connection.kt @@ -159,23 +159,13 @@ abstract class Connection(val side: ConnectionSide, val type: ConnectionType) : isReady = true } - fun send(packet: IPacket) { + fun send(packet: IPacket, flush: Boolean = true) { if (channel.isOpen && isConnected) { channel.write(packet) + if (flush) channel.flush() } } - fun sendAndFlush(packet: IPacket) { - if (channel.isOpen && isConnected) { - channel.write(packet) - flush() - } - } - - open fun flush() { - channel.flush() - } - abstract fun disconnect(reason: String = "") override fun close() { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/network/packets/ProtocolResponsePacket.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/network/packets/ProtocolResponsePacket.kt index 3428e865..60766252 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/network/packets/ProtocolResponsePacket.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/network/packets/ProtocolResponsePacket.kt @@ -20,7 +20,7 @@ data class ProtocolResponsePacket(val allowed: Boolean) : IClientPacket { if (allowed) { if (connection.isLegacy) { connection.setupLegacy() - connection.sendAndFlush( + connection.send( ClientConnectPacket( ByteArray(ClientConnectPacket.DIGEST_SIZE), allowAssetMismatch = true, diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ChatHandler.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ChatHandler.kt index af300c8f..c99a67bb 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ChatHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ChatHandler.kt @@ -32,7 +32,7 @@ class ChatHandler(val server: StarboundServer) { if (world == null) { LOGGER.warn("{} tried to say something, but they are in limbo: {}", source.nickname, packet.text) - source.sendAndFlush(ChatReceivePacket( + source.send(ChatReceivePacket( ChatMessage( MessageContext.COMMAND_RESULT, text = "You appear to be in limbo, nobody can hear you! Use Global chat." @@ -53,7 +53,7 @@ class ChatHandler(val server: StarboundServer) { } ChatSendMode.PARTY -> { - source.sendAndFlush(ChatReceivePacket( + source.send(ChatReceivePacket( ChatMessage( MessageContext.COMMAND_RESULT, text = "Party chat not implemented." diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt index 1f44b487..481c953c 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt @@ -108,26 +108,6 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn private var remoteVersion = 0L - override fun flush() { - if (isConnected && isReady) { - val entries = rpc.write() - - if (entries != null || modifiedShipChunks.isNotEmpty() || server2clientGroup.upstream.hasChangedSince(remoteVersion)) { - val (data, version) = server2clientGroup.write(remoteVersion, isLegacy) - remoteVersion = version - - channel.write(ClientContextUpdatePacket( - entries ?: listOf(), - KOptional(modifiedShipChunks.associateWith { shipChunks[it]!! }), - KOptional(data))) - - modifiedShipChunks.clear() - } - } - - super.flush() - } - override fun onChannelClosed() { playerEntity = null @@ -423,11 +403,22 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn warpQueue.trySend(destination to deploy) } - fun tick(delta: Double) { - if (!isConnected || !channel.isOpen || !isReady) - return + fun tick() { + if (isConnected && isReady) { + val entries = rpc.write() - flush() + if (entries != null || modifiedShipChunks.isNotEmpty() || server2clientGroup.upstream.hasChangedSince(remoteVersion)) { + val (data, version) = server2clientGroup.write(remoteVersion, isLegacy) + remoteVersion = version + + send(ClientContextUpdatePacket( + entries ?: listOf(), + KOptional(modifiedShipChunks.associateWith { shipChunks[it]!! }), + KOptional(data))) + + modifiedShipChunks.clear() + } + } } private var announcedDisconnect = true @@ -449,7 +440,7 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn if (channel.isOpen) { // send pending updates - flush() + channel.flush() } isReady = false diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt index b14efce4..e4a38a6d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt @@ -267,7 +267,7 @@ sealed class StarboundServer(val root: File) : BlockableEventLoop("Server thread channels.connections.forEach { try { - it.tick(delta) + it.tick() } catch (err: Throwable) { LOGGER.error("Exception while ticking client connection", err) it.disconnect("Exception while ticking client connection: $err") diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerWorldTracker.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerWorldTracker.kt index 53fc2395..b3fd7f31 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerWorldTracker.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerWorldTracker.kt @@ -106,7 +106,7 @@ class ServerWorldTracker(val world: ServerWorld, val client: ServerConnection, p )) Starbound.legacyJson { - client.sendAndFlush(CentralStructureUpdatePacket(Starbound.gson.toJsonTree(world.centralStructure))) + client.send(CentralStructureUpdatePacket(Starbound.gson.toJsonTree(world.centralStructure))) } } }