From 98134825a6e7173c84ec72b67d9efb759f7162c4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 30 Oct 2019 13:59:28 -0400 Subject: [PATCH] Send packets immediately instead of on the next tick --- .../phycon/network/DeviceBlockEntity.kt | 31 +++++++------------ .../netinterface/InterfaceBlockEntity.kt | 6 ++-- .../block/netswitch/SwitchBlockEntity.kt | 7 ++--- .../block/terminal/TerminalBlockEntity.kt | 11 +++---- 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt index 80faa8b..92da52a 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt @@ -14,13 +14,11 @@ import java.util.* /** * @author shadowfacts */ -abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), Tickable, PacketSink { +abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), PacketSink { var macAddress = MACAddress.random() protected set - private val sendQueue = LinkedList>>() - override fun getMACAddress(): MACAddress { return macAddress } @@ -33,7 +31,7 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T protected abstract fun handlePacket(packet: Packet) - fun acceptsPacket(packet: Packet): Boolean { + protected fun acceptsPacket(packet: Packet): Boolean { return when (packet.destination.type) { MACAddress.Type.BROADCAST -> true MACAddress.Type.UNICAST -> macAddress == packet.destination @@ -45,33 +43,28 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T return false } - fun enqueue(packet: Packet, destination: PacketSink) { - sendQueue.add(packet to WeakReference(destination)) + fun send(packet: Packet, destination: PacketSink) { + destination.handle(packet) } // todo: better name for this - fun enqueueToSingle(packet: Packet) { + fun sendToSingle(packet: Packet) { val destinations = NetworkUtil.findDestinations(world!!, pos) if (destinations.size != 1) { // todo: handle this better println("Can't send packet, multiple destinations available: $destinations") return } - enqueue(packet, destinations.first()) + send(packet, destinations.first()) } - fun enqueueToAll(packet: Packet) { - enqueueToAll(packet, NetworkUtil.findDestinations(world!!, pos)) + fun sendToAll(packet: Packet) { + sendToAll(packet, NetworkUtil.findDestinations(world!!, pos)) } - fun enqueueToAll(packet: Packet, destinations: Iterable) { - sendQueue.addAll(destinations.map { packet to WeakReference(it) }) - } - - override fun tick() { - if (sendQueue.isNotEmpty()) { - val (packet, destination) = sendQueue.pop() - destination.get()?.handle(packet) + fun sendToAll(packet: Packet, destinations: Iterable) { + destinations.forEach { + it.handle(packet) } } @@ -86,7 +79,7 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T } fun onBreak() { - enqueueToAll(DeviceRemovedPacket(this)) + sendToAll(DeviceRemovedPacket(this)) } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt index 3e5ff16..1830c8d 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt @@ -43,21 +43,21 @@ class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE) { private fun handleRequestInventory(packet: RequestInventoryPacket) { getInventory()?.also { inv -> - enqueueToSingle(ReadInventoryPacket(inv, macAddress, packet.source)) + sendToSingle(ReadInventoryPacket(inv, macAddress, packet.source)) } } private fun handleLocateStack(packet: LocateStackPacket) { getInventory()?.also { inv -> val amount = inv.getAmount(packet.stack) - enqueueToSingle(StackLocationPacket(packet.stack, amount, this, macAddress, packet.source)) + sendToSingle(StackLocationPacket(packet.stack, amount, this, macAddress, packet.source)) } } private fun handleExtractStack(packet: ExtractStackPacket) { getInventory()?.also { inv -> val extracted = inv.extract(packet.stack, packet.amount) - enqueueToSingle(ItemStackPacket(extracted, macAddress, packet.source)) + sendToSingle(ItemStackPacket(extracted, macAddress, packet.source)) } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt index d555d7e..4a06711 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/netswitch/SwitchBlockEntity.kt @@ -8,7 +8,6 @@ import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.network.DeviceBlockEntity import net.shadowfacts.phycon.network.NetworkUtil import java.lang.RuntimeException -import javax.print.attribute.standard.Destination /** * @author shadowfacts @@ -24,13 +23,13 @@ class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH) { override fun handle(packet: Packet) { if (packet.destination == MACAddress.BROADCAST) { val allDestinations = NetworkUtil.findDestinations(world!!, pos).filter { it.macAddress != packet.source } - enqueueToAll(packet, allDestinations) + sendToAll(packet, allDestinations) } else { val direction = macTable[packet.destination] if (direction != null) { val dest = findDestination(direction) if (dest != null && packet.destination == dest.macAddress) { - enqueue(packet, dest) + send(packet, dest) return } } @@ -53,7 +52,7 @@ class SwitchBlockEntity: DeviceBlockEntity(PhyBlockEntities.SWITCH) { val dest = findDestination(dir) if (dest != null && packet.destination == dest.macAddress) { macTable[packet.destination] = dir - enqueue(packet, dest) + send(packet, dest) break } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt index 8e6e917..964e6e2 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt @@ -13,6 +13,7 @@ import net.minecraft.inventory.InventoryListener import net.minecraft.item.ItemStack import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.ListTag +import net.minecraft.util.Tickable import net.shadowfacts.phycon.api.packet.Packet import net.shadowfacts.phycon.api.util.MACAddress import net.shadowfacts.phycon.init.PhyBlockEntities @@ -27,7 +28,7 @@ import kotlin.math.min /** * @author shadowfacts */ -class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener, BlockEntityClientSerializable { +class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener, BlockEntityClientSerializable, Tickable { companion object { val LOCATE_REQUEST_TIMEOUT = 40 // ticks @@ -119,8 +120,6 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento } override fun tick() { - super.tick() - if (observers > 0 && (++counter % 20) == 0) { if (world!!.isClient) { println(cachedNetItems) @@ -137,7 +136,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento sync() inventoryCache.clear() - enqueueToSingle(RequestInventoryPacket(macAddress)) + sendToSingle(RequestInventoryPacket(macAddress)) ContainerProviderRegistry.INSTANCE.openContainer(TerminalContainer.ID, player) { buf -> buf.writeBlockPos(pos) } @@ -147,7 +146,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento fun requestItem(stack: ItemStack, amount: Int = stack.count) { pendingRequests.add(StackLocateRequest(stack, amount, counter)) - enqueueToSingle(LocateStackPacket(stack, macAddress)) + sendToSingle(LocateStackPacket(stack, macAddress)) } private fun stackLocateRequestCompleted(request: StackLocateRequest) { @@ -158,7 +157,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento val (sourceAmount, sourceInterface) = sortedResults.removeAt(0) val amountToRequest = min(sourceAmount, request.amount - amountRequested) amountRequested += amountToRequest - enqueueToSingle(ExtractStackPacket(request.stack, amountToRequest, macAddress, sourceInterface.macAddress)) + sendToSingle(ExtractStackPacket(request.stack, amountToRequest, macAddress, sourceInterface.macAddress)) } }