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