Skip to content

Commit a49718c

Browse files
committed
Revert "Convert PacketReliability to enum"
This reverts commit 02f15b8. This is BC-breaking and should be placed on a separate development branch.
1 parent 02f15b8 commit a49718c

File tree

5 files changed

+48
-53
lines changed

5 files changed

+48
-53
lines changed

src/generic/ReceiveReliabilityLayer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,21 @@ private function handleEncapsulatedPacket(EncapsulatedPacket $packet) : void{
163163
return;
164164
}
165165

166-
if($packet->reliability->isSequencedOrOrdered() and ($packet->orderChannel < 0 or $packet->orderChannel >= PacketReliability::MAX_ORDER_CHANNELS)){
166+
if(PacketReliability::isSequencedOrOrdered($packet->reliability) and ($packet->orderChannel < 0 or $packet->orderChannel >= PacketReliability::MAX_ORDER_CHANNELS)){
167167
//TODO: this should result in peer banning
168168
$this->logger->debug("Invalid packet, bad order channel ($packet->orderChannel)");
169169
return;
170170
}
171171

172-
if($packet->reliability->isSequenced()){
172+
if(PacketReliability::isSequenced($packet->reliability)){
173173
if($packet->sequenceIndex < $this->receiveSequencedHighestIndex[$packet->orderChannel] or $packet->orderIndex < $this->receiveOrderedIndex[$packet->orderChannel]){
174174
//too old sequenced packet, discard it
175175
return;
176176
}
177177

178178
$this->receiveSequencedHighestIndex[$packet->orderChannel] = $packet->sequenceIndex + 1;
179179
$this->handleEncapsulatedPacketRoute($packet);
180-
}elseif($packet->reliability->isOrdered()){
180+
}elseif(PacketReliability::isOrdered($packet->reliability)){
181181
if($packet->orderIndex === $this->receiveOrderedIndex[$packet->orderChannel]){
182182
//this is the packet we expected to get next
183183
//Any ordered packet resets the sequence index to zero, so that sequenced packets older than this ordered

src/generic/SendReliabilityLayer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function sendDatagram(array $packets) : void{
8585

8686
$resendable = [];
8787
foreach($datagram->packets as $pk){
88-
if($pk->reliability->isReliable()){
88+
if(PacketReliability::isReliable($pk->reliability)){
8989
$resendable[] = $pk;
9090
}
9191
}
@@ -133,9 +133,9 @@ public function addEncapsulatedToQueue(EncapsulatedPacket $packet, bool $immedia
133133
$this->needACK[$packet->identifierACK] = [];
134134
}
135135

136-
if($packet->reliability->isOrdered()){
136+
if(PacketReliability::isOrdered($packet->reliability)){
137137
$packet->orderIndex = $this->sendOrderedIndex[$packet->orderChannel]++;
138-
}elseif($packet->reliability->isSequenced()){
138+
}elseif(PacketReliability::isSequenced($packet->reliability)){
139139
$packet->orderIndex = $this->sendOrderedIndex[$packet->orderChannel]; //sequenced packets don't increment the ordered channel index
140140
$packet->sequenceIndex = $this->sendSequencedIndex[$packet->orderChannel]++;
141141
}
@@ -153,7 +153,7 @@ public function addEncapsulatedToQueue(EncapsulatedPacket $packet, bool $immedia
153153
$pk->reliability = $packet->reliability;
154154
$pk->buffer = $buffer;
155155

156-
if($pk->reliability->isReliable()){
156+
if(PacketReliability::isReliable($pk->reliability)){
157157
$pk->messageIndex = $this->messageIndex++;
158158
}
159159

@@ -164,7 +164,7 @@ public function addEncapsulatedToQueue(EncapsulatedPacket $packet, bool $immedia
164164
$this->addToQueue($pk, true);
165165
}
166166
}else{
167-
if($packet->reliability->isReliable()){
167+
if(PacketReliability::isReliable($packet->reliability)){
168168
$packet->messageIndex = $this->messageIndex++;
169169
}
170170
$this->addToQueue($packet, $immediate);

src/generic/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function update(float $time) : void{
215215
}
216216
}
217217

218-
protected function queueConnectedPacket(ConnectedPacket $packet, PacketReliability $reliability, int $orderChannel, bool $immediate = false) : void{
218+
protected function queueConnectedPacket(ConnectedPacket $packet, int $reliability, int $orderChannel, bool $immediate = false) : void{
219219
$out = new PacketSerializer(); //TODO: reuse streams to reduce allocations
220220
$packet->encode($out);
221221

@@ -231,7 +231,7 @@ public function addEncapsulatedToQueue(EncapsulatedPacket $packet, bool $immedia
231231
$this->sendLayer->addEncapsulatedToQueue($packet, $immediate);
232232
}
233233

234-
protected function sendPing(PacketReliability $reliability = PacketReliability::UNRELIABLE) : void{
234+
protected function sendPing(int $reliability = PacketReliability::UNRELIABLE) : void{
235235
$this->queueConnectedPacket(ConnectedPing::create($this->getRakNetTimeMS()), $reliability, 0, true);
236236
}
237237

src/protocol/EncapsulatedPacket.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class EncapsulatedPacket{
3131

3232
public const SPLIT_INFO_LENGTH = 4 + 2 + 4; //split count (4) + split ID (2) + split index (4)
3333

34-
public PacketReliability $reliability;
34+
public int $reliability;
3535
public ?int $messageIndex = null;
3636
public ?int $sequenceIndex = null;
3737
public ?int $orderIndex = null;
@@ -47,28 +47,23 @@ public static function fromBinary(BinaryStream $stream) : EncapsulatedPacket{
4747
$packet = new EncapsulatedPacket();
4848

4949
$flags = $stream->getByte();
50-
$reliability = PacketReliability::tryFrom(($flags & self::RELIABILITY_FLAGS) >> self::RELIABILITY_SHIFT);
51-
if($reliability === null){
52-
//TODO: we should reject the ACK_RECEIPT types here - they aren't supposed to be sent over the wire
53-
throw new BinaryDataException("Invalid encapsulated packet reliability");
54-
}
55-
$packet->reliability = $reliability;
50+
$packet->reliability = $reliability = ($flags & self::RELIABILITY_FLAGS) >> self::RELIABILITY_SHIFT;
5651
$hasSplit = ($flags & self::SPLIT_FLAG) !== 0;
5752

5853
$length = (int) ceil($stream->getShort() / 8);
5954
if($length === 0){
6055
throw new BinaryDataException("Encapsulated payload length cannot be zero");
6156
}
6257

63-
if($reliability->isReliable()){
58+
if(PacketReliability::isReliable($reliability)){
6459
$packet->messageIndex = $stream->getLTriad();
6560
}
6661

67-
if($reliability->isSequenced()){
62+
if(PacketReliability::isSequenced($reliability)){
6863
$packet->sequenceIndex = $stream->getLTriad();
6964
}
7065

71-
if($reliability->isSequencedOrOrdered()){
66+
if(PacketReliability::isSequencedOrOrdered($reliability)){
7267
$packet->orderIndex = $stream->getLTriad();
7368
$packet->orderChannel = $stream->getByte();
7469
}
@@ -86,11 +81,11 @@ public static function fromBinary(BinaryStream $stream) : EncapsulatedPacket{
8681

8782
public function toBinary() : string{
8883
return
89-
chr(($this->reliability->value << self::RELIABILITY_SHIFT) | ($this->splitInfo !== null ? self::SPLIT_FLAG : 0)) .
84+
chr(($this->reliability << self::RELIABILITY_SHIFT) | ($this->splitInfo !== null ? self::SPLIT_FLAG : 0)) .
9085
Binary::writeShort(strlen($this->buffer) << 3) .
91-
($this->reliability->isReliable() ? Binary::writeLTriad($this->messageIndex) : "") .
92-
($this->reliability->isSequenced() ? Binary::writeLTriad($this->sequenceIndex) : "") .
93-
($this->reliability->isSequencedOrOrdered() ? Binary::writeLTriad($this->orderIndex) . chr($this->orderChannel) : "") .
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) : "") .
9489
($this->splitInfo !== null ? Binary::writeInt($this->splitInfo->getTotalPartCount()) . Binary::writeShort($this->splitInfo->getId()) . Binary::writeInt($this->splitInfo->getPartIndex()) : "")
9590
. $this->buffer;
9691
}
@@ -102,9 +97,9 @@ public function getHeaderLength() : int{
10297
return
10398
1 + //reliability
10499
2 + //length
105-
($this->reliability->isReliable() ? 3 : 0) + //message index
106-
($this->reliability->isSequenced() ? 3 : 0) + //sequence index
107-
($this->reliability->isSequencedOrOrdered() ? 3 + 1 : 0) + //order index (3) + order channel (1)
100+
(PacketReliability::isReliable($this->reliability) ? 3 : 0) + //message index
101+
(PacketReliability::isSequenced($this->reliability) ? 3 : 0) + //sequence index
102+
(PacketReliability::isSequencedOrOrdered($this->reliability) ? 3 + 1 : 0) + //order index (3) + order channel (1)
108103
($this->splitInfo !== null ? self::SPLIT_INFO_LENGTH : 0);
109104
}
110105

src/protocol/PacketReliability.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,57 @@
1616

1717
namespace raklib\protocol;
1818

19-
enum PacketReliability : int{
19+
abstract class PacketReliability{
2020

2121
/*
2222
* From https://github.com/OculusVR/RakNet/blob/master/Source/PacketPriority.h
2323
*
2424
* Default: 0b010 (2) or 0b011 (3)
2525
*/
2626

27-
case UNRELIABLE = 0;
28-
case UNRELIABLE_SEQUENCED = 1;
29-
case RELIABLE = 2;
30-
case RELIABLE_ORDERED = 3;
31-
case RELIABLE_SEQUENCED = 4;
27+
public const UNRELIABLE = 0;
28+
public const UNRELIABLE_SEQUENCED = 1;
29+
public const RELIABLE = 2;
30+
public const RELIABLE_ORDERED = 3;
31+
public const RELIABLE_SEQUENCED = 4;
3232

3333
/* The following reliabilities are used in RakNet internals, but never sent on the wire. */
34-
case UNRELIABLE_WITH_ACK_RECEIPT = 5;
35-
case RELIABLE_WITH_ACK_RECEIPT = 6;
36-
case RELIABLE_ORDERED_WITH_ACK_RECEIPT = 7;
34+
public const UNRELIABLE_WITH_ACK_RECEIPT = 5;
35+
public const RELIABLE_WITH_ACK_RECEIPT = 6;
36+
public const RELIABLE_ORDERED_WITH_ACK_RECEIPT = 7;
3737

3838
public const MAX_ORDER_CHANNELS = 32;
3939

40-
public function isReliable() : bool{
40+
public static function isReliable(int $reliability) : bool{
4141
return (
42-
$this === self::RELIABLE or
43-
$this === self::RELIABLE_ORDERED or
44-
$this === self::RELIABLE_SEQUENCED or
45-
$this === self::RELIABLE_WITH_ACK_RECEIPT or
46-
$this === self::RELIABLE_ORDERED_WITH_ACK_RECEIPT
42+
$reliability === self::RELIABLE or
43+
$reliability === self::RELIABLE_ORDERED or
44+
$reliability === self::RELIABLE_SEQUENCED or
45+
$reliability === self::RELIABLE_WITH_ACK_RECEIPT or
46+
$reliability === self::RELIABLE_ORDERED_WITH_ACK_RECEIPT
4747
);
4848
}
4949

50-
public function isSequenced() : bool{
50+
public static function isSequenced(int $reliability) : bool{
5151
return (
52-
$this === self::UNRELIABLE_SEQUENCED or
53-
$this === self::RELIABLE_SEQUENCED
52+
$reliability === self::UNRELIABLE_SEQUENCED or
53+
$reliability === self::RELIABLE_SEQUENCED
5454
);
5555
}
5656

57-
public function isOrdered() : bool{
57+
public static function isOrdered(int $reliability) : bool{
5858
return (
59-
$this === self::RELIABLE_ORDERED or
60-
$this === self::RELIABLE_ORDERED_WITH_ACK_RECEIPT
59+
$reliability === self::RELIABLE_ORDERED or
60+
$reliability === self::RELIABLE_ORDERED_WITH_ACK_RECEIPT
6161
);
6262
}
6363

64-
public function isSequencedOrOrdered() : bool{
64+
public static function isSequencedOrOrdered(int $reliability) : bool{
6565
return (
66-
$this === self::UNRELIABLE_SEQUENCED or
67-
$this === self::RELIABLE_ORDERED or
68-
$this === self::RELIABLE_SEQUENCED or
69-
$this === self::RELIABLE_ORDERED_WITH_ACK_RECEIPT
66+
$reliability === self::UNRELIABLE_SEQUENCED or
67+
$reliability === self::RELIABLE_ORDERED or
68+
$reliability === self::RELIABLE_SEQUENCED or
69+
$reliability === self::RELIABLE_ORDERED_WITH_ACK_RECEIPT
7070
);
7171
}
7272
}

0 commit comments

Comments
 (0)