diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt index 567626d..26bf15f 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/DeviceBlockEntity.kt @@ -7,6 +7,7 @@ import net.minecraft.util.Tickable import net.shadowfacts.phycon.api.PacketSink import net.shadowfacts.phycon.api.packet.Packet import net.shadowfacts.phycon.api.util.MACAddress +import java.lang.ref.WeakReference import java.util.* /** @@ -17,7 +18,7 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T var macAddress = MACAddress.random() protected set - private val sendQueue = LinkedList() + private val sendQueue = LinkedList>>() override fun handle(packet: Packet) { if (acceptsPacket(packet)) { @@ -35,21 +36,33 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T } } - fun acceptsMulticastPacket(packet: Packet): Boolean { + open fun acceptsMulticastPacket(packet: Packet): Boolean { return false } - fun enqueue(packet: Packet) { - sendQueue.add(packet) + fun enqueue(packet: Packet, destination: PacketSink) { + sendQueue.add(packet to WeakReference(destination)) + } + + // todo: better name for this + fun enqueueToSingle(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()) + } + + fun enqueueToAll(packet: Packet) { + sendQueue.addAll(NetworkUtil.findDestinations(world!!, pos).map { packet to WeakReference(it) }) } override fun tick() { if (sendQueue.isNotEmpty()) { - val packet = sendQueue.pop() - val destinations = NetworkUtil.findDestinations(world!!, pos) - destinations.forEach { - it.handle(packet) - } + val (packet, destination) = sendQueue.pop() + destination.get()?.handle(packet) } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/networkinterface/NetworkInterfaceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/networkinterface/NetworkInterfaceBlockEntity.kt index ff1a976..293dccd 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/networkinterface/NetworkInterfaceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/networkinterface/NetworkInterfaceBlockEntity.kt @@ -43,7 +43,7 @@ class NetworkInterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.NETWORK_IN } fun handleReadAll(packet: RequestReadAllPacket) { - enqueue(ReadAllPacket(readAll(), macAddress, packet.source)) + enqueueToSingle(ReadAllPacket(readAll(), macAddress, packet.source)) } fun readAll(): Map { 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 a0a79bd..f492f8c 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 @@ -31,7 +31,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) { fun onActivate() { if (!world!!.isClient) { cachedItems.clear() - enqueue(RequestReadAllPacket(macAddress)) + enqueueToSingle(RequestReadAllPacket(macAddress)) } }