Skip to content

Commit ae0a0f6

Browse files
committed
Merge branch 'stable' into ext-encoding
2 parents 1b2e4f3 + 669eb4d commit ae0a0f6

File tree

7 files changed

+26
-37
lines changed

7 files changed

+26
-37
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: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ private function handleEncapsulatedPacket(EncapsulatedPacket $packet) : void{
176176
}
177177

178178
if(PacketReliability::isSequenced($packet->reliability)){
179+
assert($packet->orderChannel !== null && $packet->sequenceIndex !== null, 'These should have been set during decode');
179180
if($packet->sequenceIndex < $this->receiveSequencedHighestIndex[$packet->orderChannel] or $packet->orderIndex < $this->receiveOrderedIndex[$packet->orderChannel]){
180181
//too old sequenced packet, discard it
181182
return;
@@ -184,28 +185,30 @@ private function handleEncapsulatedPacket(EncapsulatedPacket $packet) : void{
184185
$this->receiveSequencedHighestIndex[$packet->orderChannel] = $packet->sequenceIndex + 1;
185186
$this->handleEncapsulatedPacketRoute($packet);
186187
}elseif(PacketReliability::isOrdered($packet->reliability)){
187-
if($packet->orderIndex === $this->receiveOrderedIndex[$packet->orderChannel]){
188+
$orderChannel = $packet->orderChannel;
189+
assert($orderChannel !== null, 'This should have been set during decode');
190+
if($packet->orderIndex === $this->receiveOrderedIndex[$orderChannel]){
188191
//this is the packet we expected to get next
189192
//Any ordered packet resets the sequence index to zero, so that sequenced packets older than this ordered
190193
//one get discarded. Sequenced packets also include (but don't increment) the order index, so a sequenced
191194
//packet with an order index less than this will get discarded
192-
$this->receiveSequencedHighestIndex[$packet->orderChannel] = 0;
193-
$this->receiveOrderedIndex[$packet->orderChannel] = $packet->orderIndex + 1;
195+
$this->receiveSequencedHighestIndex[$orderChannel] = 0;
196+
$this->receiveOrderedIndex[$orderChannel] = $packet->orderIndex + 1;
194197

195198
$this->handleEncapsulatedPacketRoute($packet);
196-
$i = $this->receiveOrderedIndex[$packet->orderChannel];
197-
for(; isset($this->receiveOrderedPackets[$packet->orderChannel][$i]); ++$i){
198-
$this->handleEncapsulatedPacketRoute($this->receiveOrderedPackets[$packet->orderChannel][$i]);
199-
unset($this->receiveOrderedPackets[$packet->orderChannel][$i]);
199+
$i = $this->receiveOrderedIndex[$orderChannel];
200+
for(; isset($this->receiveOrderedPackets[$orderChannel][$i]); ++$i){
201+
$this->handleEncapsulatedPacketRoute($this->receiveOrderedPackets[$orderChannel][$i]);
202+
unset($this->receiveOrderedPackets[$orderChannel][$i]);
200203
}
201204

202-
$this->receiveOrderedIndex[$packet->orderChannel] = $i;
203-
}elseif($packet->orderIndex > $this->receiveOrderedIndex[$packet->orderChannel]){
204-
if(count($this->receiveOrderedPackets[$packet->orderChannel]) >= self::$WINDOW_SIZE){
205+
$this->receiveOrderedIndex[$orderChannel] = $i;
206+
}elseif($packet->orderIndex > $this->receiveOrderedIndex[$orderChannel]){
207+
if(count($this->receiveOrderedPackets[$orderChannel]) >= self::$WINDOW_SIZE){
205208
//queue overflow for this channel - we should probably disconnect the peer at this point
206209
return;
207210
}
208-
$this->receiveOrderedPackets[$packet->orderChannel][$packet->orderIndex] = $packet;
211+
$this->receiveOrderedPackets[$orderChannel][$packet->orderIndex] = $packet;
209212
}else{
210213
//duplicate/already received packet
211214
}

src/generic/SendReliabilityLayer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,14 @@ public function addEncapsulatedToQueue(EncapsulatedPacket $packet, bool $immedia
169169
}
170170

171171
if(PacketReliability::isOrdered($packet->reliability)){
172+
if($packet->orderChannel === null){
173+
throw new \InvalidArgumentException("Order channel must be set reliability $packet->reliability");
174+
}
172175
$packet->orderIndex = $this->sendOrderedIndex[$packet->orderChannel]++;
173176
}elseif(PacketReliability::isSequenced($packet->reliability)){
177+
if($packet->orderChannel === null){
178+
throw new \InvalidArgumentException("Order channel must be set for reliability $packet->reliability");
179+
}
174180
$packet->orderIndex = $this->sendOrderedIndex[$packet->orderChannel]; //sequenced packets don't increment the ordered channel index
175181
$packet->sequenceIndex = $this->sendSequencedIndex[$packet->orderChannel]++;
176182
}

src/protocol/EncapsulatedPacket.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ public function toBinary(ByteBufferWriter $out) : void{
8686

8787
BE::writeUnsignedShort($out, strlen($this->buffer) << 3);
8888
if(PacketReliability::isReliable($this->reliability)){
89-
LE::writeUnsignedTriad($out, $this->messageIndex);
89+
LE::writeUnsignedTriad($out, $this->messageIndex ?? throw new \LogicException("Message index must be set for reliability $this->reliability"));
9090
}
9191
if(PacketReliability::isSequenced($this->reliability)){
92-
LE::writeUnsignedTriad($out, $this->sequenceIndex);
92+
LE::writeUnsignedTriad($out, $this->sequenceIndex ?? throw new \LogicException("Sequence index must be set for reliability $this->reliability"));
9393
}
9494
if(PacketReliability::isSequencedOrOrdered($this->reliability)){
95-
LE::writeUnsignedTriad($out, $this->orderIndex);
96-
Byte::writeUnsigned($out, $this->orderChannel);
95+
LE::writeUnsignedTriad($out, $this->orderIndex ?? throw new \LogicException("Order index must be set for reliability $this->reliability"));
96+
Byte::writeUnsigned($out, $this->orderChannel ?? throw new \LogicException("Order channel must be set for reliability $this->reliability"));
9797
}
9898
if($this->splitInfo !== null){
9999
BE::writeUnsignedInt($out, $this->splitInfo->getTotalPartCount());

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

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

tools/proxy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
$bindAddr = "0.0.0.0";
3333
$bindPort = 19132;
3434

35+
$argv ??= [];
3536
if(count($argv) === 3){
3637
$serverAddress = $argv[1];
3738
$serverPort = (int) $argv[2];

tools/scan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
require dirname(__DIR__) . '/vendor/autoload.php';
1313

14-
if(count($argv) === 3){
14+
if(isset($argv) && count($argv) === 3){
1515
$broadcastAddress = $argv[1];
1616
$port = (int) $argv[2];
1717
}else{

0 commit comments

Comments
 (0)