1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DaveRandom\LibLifxLan\Decoding; |
4
|
|
|
|
5
|
|
|
use DaveRandom\LibLifxLan\Decoding\Exceptions\DecodingException; |
6
|
|
|
use DaveRandom\LibLifxLan\Decoding\Exceptions\InsufficientDataException; |
7
|
|
|
use DaveRandom\LibLifxLan\Decoding\Exceptions\InvalidMessagePayloadLengthException; |
8
|
|
|
use DaveRandom\LibLifxLan\Header\Header; |
9
|
|
|
use DaveRandom\LibLifxLan\Packet; |
10
|
|
|
|
11
|
|
|
final class PacketDecoder |
12
|
|
|
{ |
13
|
|
|
private const HEADER_OFFSET = 0; |
14
|
|
|
private const MESSAGE_OFFSET = Header::WIRE_SIZE; |
15
|
|
|
|
16
|
|
|
private $headerDecoder; |
17
|
|
|
private $messageDecoder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $buffer |
21
|
|
|
* @param int $offset |
22
|
|
|
* @param int $length |
23
|
|
|
* @return int |
24
|
|
|
* @throws InsufficientDataException |
25
|
|
|
* @throws InvalidMessagePayloadLengthException |
26
|
|
|
*/ |
27
|
12 |
|
private function getValidDataLength(string $buffer, int $offset, ?int $length): int |
28
|
|
|
{ |
29
|
12 |
|
$dataLength = $length ?? (\strlen($buffer) - $offset); |
30
|
|
|
|
31
|
12 |
|
if ($dataLength < Header::WIRE_SIZE) { |
32
|
4 |
|
throw new InsufficientDataException( |
33
|
4 |
|
"Data length {$dataLength} less than minimum packet size " . Header::WIRE_SIZE |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
8 |
|
$statedLength = \unpack('vlength', $buffer, $offset)['length']; |
38
|
|
|
|
39
|
8 |
|
if ($statedLength !== $dataLength) { |
40
|
4 |
|
throw new InvalidMessagePayloadLengthException( |
41
|
4 |
|
"Packet length is stated to be {$statedLength} bytes, buffer is {$dataLength} bytes" |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
return $statedLength; |
46
|
|
|
} |
47
|
|
|
|
48
|
16 |
|
public function __construct(HeaderDecoder $headerDecoder = null, MessageDecoder $messageDecoder = null) |
49
|
|
|
{ |
50
|
16 |
|
$this->headerDecoder = $headerDecoder ?? new HeaderDecoder; |
51
|
16 |
|
$this->messageDecoder = $messageDecoder ?? new MessageDecoder; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function getHeaderDecoder(): HeaderDecoder |
55
|
|
|
{ |
56
|
2 |
|
return $this->headerDecoder; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
public function getMessageDecoder(): MessageDecoder |
60
|
|
|
{ |
61
|
2 |
|
return $this->messageDecoder; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $buffer |
66
|
|
|
* @param int $offset |
67
|
|
|
* @param int|null $length |
68
|
|
|
* @return Packet |
69
|
|
|
* @throws DecodingException |
70
|
|
|
* @throws InsufficientDataException |
71
|
|
|
* @throws InvalidMessagePayloadLengthException |
72
|
|
|
*/ |
73
|
12 |
|
public function decodePacket(string $buffer, int $offset = 0, int $length = null): Packet |
74
|
|
|
{ |
75
|
12 |
|
$dataLength = $this->getValidDataLength($buffer, $offset, $length); |
76
|
|
|
|
77
|
4 |
|
$header = $this->headerDecoder->decodeHeader($buffer, $offset + self::HEADER_OFFSET); |
78
|
4 |
|
$payload = $this->messageDecoder->decodeMessage( |
79
|
4 |
|
$header->getProtocolHeader()->getType(), |
80
|
4 |
|
$buffer, |
81
|
4 |
|
$offset + self::MESSAGE_OFFSET, |
82
|
4 |
|
$dataLength - Header::WIRE_SIZE |
83
|
|
|
); |
84
|
|
|
|
85
|
4 |
|
return new Packet($header, $payload); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|