Tweak NetworkStackDispatcher behavior on incomplete insertions

This commit is contained in:
Shadowfacts 2023-03-12 22:51:06 -04:00
parent c18af9794b
commit 33614e0dc6
2 changed files with 16 additions and 8 deletions

View File

@ -225,7 +225,8 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
// as with extracting, we "know" the new amounts and so can update instantly without actually sending out packets
updateAndSync()
return remaining
// don't start a second insertion, since remaining will be dispatched at the next beginInsertions
return ItemStack.EMPTY
}
override fun onInventoryChanged(inv: Inventory) {

View File

@ -36,7 +36,9 @@ interface NetworkStackDispatcher<Insertion: NetworkStackDispatcher.PendingInsert
insertion.results.add(packet.capacity to packet.stackReceiver)
if (insertion.isFinishable(this)) {
val remaining = finishInsertion(insertion)
// todo: do something with remaining
if (!remaining.isEmpty) {
pendingInsertions.add(createPendingInsertion(remaining))
}
}
}
}
@ -56,12 +58,13 @@ interface NetworkStackDispatcher<Insertion: NetworkStackDispatcher.PendingInsert
// copy the insertion stack so subclasses that override this method can still see the originally dispatched stack after the super call
val remaining = insertion.stack.copy()
while (!remaining.isEmpty && sortedResults.isNotEmpty()) {
val (capacity, receivingInterface) = sortedResults.removeFirst()
val (capacity, receiver) = sortedResults.removeFirst()
if (capacity <= 0) continue
val copy = remaining.copyWithCount(min(capacity, remaining.count))
sendPacket(ItemStackPacket(copy, ipAddress, receivingInterface.ipAddress))
val sentCount = min(capacity, remaining.count)
val copy = remaining.copyWithCount(sentCount)
sendPacket(ItemStackPacket(copy, ipAddress, receiver.ipAddress))
// todo: the destination should confirm how much was actually inserted, in case of race condition
remaining.count -= capacity
remaining.count -= sentCount
}
return remaining
@ -89,7 +92,11 @@ fun <Self, Insertion: NetworkStackDispatcher.PendingInsertion<Insertion>> Self.f
val finishable = pendingInsertions.filter { it.isFinishable(this) }
// finishInsertion removes the object from pendingInsertions
finishable.forEach(::finishInsertion)
// todo: do something with remaining?
for (insertion in finishable) {
val remaining = finishInsertion(insertion)
if (!remaining.isEmpty) {
pendingInsertions.add(createPendingInsertion(remaining))
}
}
// todo: if a timed-out insertion can't be finished, we should probably retry after some time (exponential backoff?)
}