Send network packets right away

This commit is contained in:
DBotThePony 2024-04-18 14:31:08 +07:00
parent bad3fd9b16
commit de735cc5b8
Signed by: DBot
GPG Key ID: DCC23B5715498507
8 changed files with 27 additions and 49 deletions

View File

@ -59,7 +59,7 @@ class ClientConnection(val client: StarboundClient, type: ConnectionType) : Conn
private var clientStateNetVersion = 0L private var clientStateNetVersion = 0L
override fun flush() { fun tick() {
if (!pendingDisconnect && isConnected) { if (!pendingDisconnect && isConnected) {
val entries = rpc.write() val entries = rpc.write()
@ -74,8 +74,6 @@ class ClientConnection(val client: StarboundClient, type: ConnectionType) : Conn
clientStateNetVersion = new clientStateNetVersion = new
} }
super.flush()
} }
private var pendingDisconnect = false private var pendingDisconnect = false
@ -94,7 +92,7 @@ class ClientConnection(val client: StarboundClient, type: ConnectionType) : Conn
if (channel.isOpen) { if (channel.isOpen) {
pendingDisconnect = true pendingDisconnect = true
sendAndFlush(ClientDisconnectRequestPacket) send(ClientDisconnectRequestPacket)
} else { } else {
disconnectNow() disconnectNow()
} }

View File

@ -774,7 +774,7 @@ class StarboundClient private constructor(val clientID: Int) : BlockableEventLoo
if (world != null && Starbound.initialized) if (world != null && Starbound.initialized)
world.tick(Starbound.TIMESTEP) world.tick(Starbound.TIMESTEP)
activeConnection?.flush() activeConnection?.tick()
return return
} }
@ -833,8 +833,7 @@ class StarboundClient private constructor(val clientID: Int) : BlockableEventLoo
GLFW.glfwPollEvents() GLFW.glfwPollEvents()
performOpenGLCleanup() performOpenGLCleanup()
activeConnection?.tick()
activeConnection?.flush()
} }
private fun tick() { private fun tick() {

View File

@ -159,23 +159,13 @@ abstract class Connection(val side: ConnectionSide, val type: ConnectionType) :
isReady = true isReady = true
} }
fun send(packet: IPacket) { fun send(packet: IPacket, flush: Boolean = true) {
if (channel.isOpen && isConnected) { if (channel.isOpen && isConnected) {
channel.write(packet) 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 = "") abstract fun disconnect(reason: String = "")
override fun close() { override fun close() {

View File

@ -20,7 +20,7 @@ data class ProtocolResponsePacket(val allowed: Boolean) : IClientPacket {
if (allowed) { if (allowed) {
if (connection.isLegacy) { if (connection.isLegacy) {
connection.setupLegacy() connection.setupLegacy()
connection.sendAndFlush( connection.send(
ClientConnectPacket( ClientConnectPacket(
ByteArray(ClientConnectPacket.DIGEST_SIZE), ByteArray(ClientConnectPacket.DIGEST_SIZE),
allowAssetMismatch = true, allowAssetMismatch = true,

View File

@ -32,7 +32,7 @@ class ChatHandler(val server: StarboundServer) {
if (world == null) { if (world == null) {
LOGGER.warn("{} tried to say something, but they are in limbo: {}", source.nickname, packet.text) LOGGER.warn("{} tried to say something, but they are in limbo: {}", source.nickname, packet.text)
source.sendAndFlush(ChatReceivePacket( source.send(ChatReceivePacket(
ChatMessage( ChatMessage(
MessageContext.COMMAND_RESULT, MessageContext.COMMAND_RESULT,
text = "You appear to be in limbo, nobody can hear you! Use Global chat." 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 -> { ChatSendMode.PARTY -> {
source.sendAndFlush(ChatReceivePacket( source.send(ChatReceivePacket(
ChatMessage( ChatMessage(
MessageContext.COMMAND_RESULT, MessageContext.COMMAND_RESULT,
text = "Party chat not implemented." text = "Party chat not implemented."

View File

@ -108,26 +108,6 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn
private var remoteVersion = 0L 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() { override fun onChannelClosed() {
playerEntity = null playerEntity = null
@ -423,11 +403,22 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn
warpQueue.trySend(destination to deploy) warpQueue.trySend(destination to deploy)
} }
fun tick(delta: Double) { fun tick() {
if (!isConnected || !channel.isOpen || !isReady) if (isConnected && isReady) {
return 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 private var announcedDisconnect = true
@ -449,7 +440,7 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn
if (channel.isOpen) { if (channel.isOpen) {
// send pending updates // send pending updates
flush() channel.flush()
} }
isReady = false isReady = false

View File

@ -267,7 +267,7 @@ sealed class StarboundServer(val root: File) : BlockableEventLoop("Server thread
channels.connections.forEach { channels.connections.forEach {
try { try {
it.tick(delta) it.tick()
} catch (err: Throwable) { } catch (err: Throwable) {
LOGGER.error("Exception while ticking client connection", err) LOGGER.error("Exception while ticking client connection", err)
it.disconnect("Exception while ticking client connection: $err") it.disconnect("Exception while ticking client connection: $err")

View File

@ -106,7 +106,7 @@ class ServerWorldTracker(val world: ServerWorld, val client: ServerConnection, p
)) ))
Starbound.legacyJson { Starbound.legacyJson {
client.sendAndFlush(CentralStructureUpdatePacket(Starbound.gson.toJsonTree(world.centralStructure))) client.send(CentralStructureUpdatePacket(Starbound.gson.toJsonTree(world.centralStructure)))
} }
} }
} }