Explicitly specify packet destinations instead of broadcasting to all
connected sinks
This commit is contained in:
parent
fa7c499f29
commit
a8b53e1117
|
@ -7,6 +7,7 @@ import net.minecraft.util.Tickable
|
||||||
import net.shadowfacts.phycon.api.PacketSink
|
import net.shadowfacts.phycon.api.PacketSink
|
||||||
import net.shadowfacts.phycon.api.packet.Packet
|
import net.shadowfacts.phycon.api.packet.Packet
|
||||||
import net.shadowfacts.phycon.api.util.MACAddress
|
import net.shadowfacts.phycon.api.util.MACAddress
|
||||||
|
import java.lang.ref.WeakReference
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,7 +18,7 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type), T
|
||||||
var macAddress = MACAddress.random()
|
var macAddress = MACAddress.random()
|
||||||
protected set
|
protected set
|
||||||
|
|
||||||
private val sendQueue = LinkedList<Packet>()
|
private val sendQueue = LinkedList<Pair<Packet, WeakReference<PacketSink>>>()
|
||||||
|
|
||||||
override fun handle(packet: Packet) {
|
override fun handle(packet: Packet) {
|
||||||
if (acceptsPacket(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
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun enqueue(packet: Packet) {
|
fun enqueue(packet: Packet, destination: PacketSink) {
|
||||||
sendQueue.add(packet)
|
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() {
|
override fun tick() {
|
||||||
if (sendQueue.isNotEmpty()) {
|
if (sendQueue.isNotEmpty()) {
|
||||||
val packet = sendQueue.pop()
|
val (packet, destination) = sendQueue.pop()
|
||||||
val destinations = NetworkUtil.findDestinations(world!!, pos)
|
destination.get()?.handle(packet)
|
||||||
destinations.forEach {
|
|
||||||
it.handle(packet)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ class NetworkInterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.NETWORK_IN
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleReadAll(packet: RequestReadAllPacket) {
|
fun handleReadAll(packet: RequestReadAllPacket) {
|
||||||
enqueue(ReadAllPacket(readAll(), macAddress, packet.source))
|
enqueueToSingle(ReadAllPacket(readAll(), macAddress, packet.source))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readAll(): Map<ItemStack, Int> {
|
fun readAll(): Map<ItemStack, Int> {
|
||||||
|
|
|
@ -31,7 +31,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) {
|
||||||
fun onActivate() {
|
fun onActivate() {
|
||||||
if (!world!!.isClient) {
|
if (!world!!.isClient) {
|
||||||
cachedItems.clear()
|
cachedItems.clear()
|
||||||
enqueue(RequestReadAllPacket(macAddress))
|
enqueueToSingle(RequestReadAllPacket(macAddress))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue