Skip to content

Commit 6d6e63f

Browse files
committed
Properly check for error conditions with EncapsulatedPacket
this needs to be redesigned, but we can do better than just blacklisting these errors. on the receive layer they should always be correct, but on the send layer it's possible for application code to forget to set stuff, so these should be checked.
1 parent 9496370 commit 6d6e63f

File tree

5 files changed

+16
-24
lines changed

5 files changed

+16
-24
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
includes:
2-
- tests/phpstan/configs/encapsulated-packet-sucks.neon
32
- tests/phpstan/configs/phpstan-bugs.neon
43
- vendor/phpstan/phpstan-strict-rules/rules.neon
54

src/generic/ReceiveReliabilityLayer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private function handleEncapsulatedPacket(EncapsulatedPacket $packet) : void{
176176
}
177177

178178
if(PacketReliability::isSequenced($packet->reliability)){
179-
assert($packet->orderChannel !== null, 'This should have been set during decode');
179+
assert($packet->orderChannel !== null && $packet->sequenceIndex !== null, 'These should have been set during decode');
180180
if($packet->sequenceIndex < $this->receiveSequencedHighestIndex[$packet->orderChannel] or $packet->orderIndex < $this->receiveOrderedIndex[$packet->orderChannel]){
181181
//too old sequenced packet, discard it
182182
return;

src/generic/SendReliabilityLayer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ public function addEncapsulatedToQueue(EncapsulatedPacket $packet, bool $immedia
170170

171171
if(PacketReliability::isOrdered($packet->reliability)){
172172
if($packet->orderChannel === null){
173-
throw new \InvalidArgumentException("Order channel must be set for an ordered packet");
173+
throw new \InvalidArgumentException("Order channel must be set reliability $packet->reliability");
174174
}
175175
$packet->orderIndex = $this->sendOrderedIndex[$packet->orderChannel]++;
176176
}elseif(PacketReliability::isSequenced($packet->reliability)){
177177
if($packet->orderChannel === null){
178-
throw new \InvalidArgumentException("Order channel must be set for a sequenced packet");
178+
throw new \InvalidArgumentException("Order channel must be set for reliability $packet->reliability");
179179
}
180180
$packet->orderIndex = $this->sendOrderedIndex[$packet->orderChannel]; //sequenced packets don't increment the ordered channel index
181181
$packet->sequenceIndex = $this->sendSequencedIndex[$packet->orderChannel]++;

src/protocol/EncapsulatedPacket.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,19 @@ public function toBinary() : string{
8383
return
8484
chr(($this->reliability << self::RELIABILITY_SHIFT) | ($this->splitInfo !== null ? self::SPLIT_FLAG : 0)) .
8585
Binary::writeShort(strlen($this->buffer) << 3) .
86-
(PacketReliability::isReliable($this->reliability) ? Binary::writeLTriad($this->messageIndex) : "") .
87-
(PacketReliability::isSequenced($this->reliability) ? Binary::writeLTriad($this->sequenceIndex) : "") .
88-
(PacketReliability::isSequencedOrOrdered($this->reliability) ? Binary::writeLTriad($this->orderIndex) . chr($this->orderChannel) : "") .
86+
(PacketReliability::isReliable($this->reliability) ?
87+
Binary::writeLTriad($this->messageIndex ?? throw new \LogicException("Message index must be set for reliability $this->reliability")) :
88+
""
89+
) .
90+
(PacketReliability::isSequenced($this->reliability) ?
91+
Binary::writeLTriad($this->sequenceIndex ?? throw new \LogicException("Sequence index must be set for reliability $this->reliability")) :
92+
""
93+
) .
94+
(PacketReliability::isSequencedOrOrdered($this->reliability) ?
95+
Binary::writeLTriad($this->orderIndex ?? throw new \LogicException("Order index must be set for reliability $this->reliability")) .
96+
chr($this->orderChannel ?? throw new \LogicException("Order channel must be set for reliability $this->reliability")) :
97+
""
98+
) .
8999
($this->splitInfo !== null ? Binary::writeInt($this->splitInfo->getTotalPartCount()) . Binary::writeShort($this->splitInfo->getId()) . Binary::writeInt($this->splitInfo->getPartIndex()) : "")
90100
. $this->buffer;
91101
}

tests/phpstan/configs/encapsulated-packet-sucks.neon

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)