Skip to content

Commit 231b8d9

Browse files
committed
Optimize unpacking the packet length
1 parent 7cf5b4c commit 231b8d9

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Packer/PacketLength.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ public static function pack(int $length) : string
2828

2929
public static function unpack(string $data) : int
3030
{
31-
if (false === $data = @\unpack('C/N', $data)) {
31+
if (!isset($data[4]) || "\xce" !== $data[0]) {
3232
throw new \RuntimeException('Unable to unpack packet length.');
3333
}
3434

35-
return $data[1];
35+
return \ord($data[1]) << 24
36+
| \ord($data[2]) << 16
37+
| \ord($data[3]) << 8
38+
| \ord($data[4]);
3639
}
3740
}

0 commit comments

Comments
 (0)