@@ -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 int $ reliability ;
34+ public PacketReliability $ reliability ;
3535 public ?int $ messageIndex = null ;
3636 public ?int $ sequenceIndex = null ;
3737 public ?int $ orderIndex = null ;
@@ -47,23 +47,28 @@ public static function fromBinary(BinaryStream $stream) : EncapsulatedPacket{
4747 $ packet = new EncapsulatedPacket ();
4848
4949 $ flags = $ stream ->getByte ();
50- $ packet ->reliability = $ reliability = ($ flags & self ::RELIABILITY_FLAGS ) >> self ::RELIABILITY_SHIFT ;
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 ;
5156 $ hasSplit = ($ flags & self ::SPLIT_FLAG ) !== 0 ;
5257
5358 $ length = (int ) ceil ($ stream ->getShort () / 8 );
5459 if ($ length === 0 ){
5560 throw new BinaryDataException ("Encapsulated payload length cannot be zero " );
5661 }
5762
58- if (PacketReliability:: isReliable ( $ reliability )){
63+ if ($ reliability-> isReliable ( )){
5964 $ packet ->messageIndex = $ stream ->getLTriad ();
6065 }
6166
62- if (PacketReliability:: isSequenced ( $ reliability )){
67+ if ($ reliability-> isSequenced ( )){
6368 $ packet ->sequenceIndex = $ stream ->getLTriad ();
6469 }
6570
66- if (PacketReliability:: isSequencedOrOrdered ( $ reliability )){
71+ if ($ reliability-> isSequencedOrOrdered ( )){
6772 $ packet ->orderIndex = $ stream ->getLTriad ();
6873 $ packet ->orderChannel = $ stream ->getByte ();
6974 }
@@ -81,11 +86,11 @@ public static function fromBinary(BinaryStream $stream) : EncapsulatedPacket{
8186
8287 public function toBinary () : string {
8388 return
84- chr (($ this ->reliability << self ::RELIABILITY_SHIFT ) | ($ this ->splitInfo !== null ? self ::SPLIT_FLAG : 0 )) .
89+ chr (($ this ->reliability -> value << self ::RELIABILITY_SHIFT ) | ($ this ->splitInfo !== null ? self ::SPLIT_FLAG : 0 )) .
8590 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 ) : "" ) .
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 ) : "" ) .
8994 ($ this ->splitInfo !== null ? Binary::writeInt ($ this ->splitInfo ->getTotalPartCount ()) . Binary::writeShort ($ this ->splitInfo ->getId ()) . Binary::writeInt ($ this ->splitInfo ->getPartIndex ()) : "" )
9095 . $ this ->buffer ;
9196 }
@@ -97,9 +102,9 @@ public function getHeaderLength() : int{
97102 return
98103 1 + //reliability
99104 2 + //length
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)
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)
103108 ($ this ->splitInfo !== null ? self ::SPLIT_INFO_LENGTH : 0 );
104109 }
105110
0 commit comments