1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Connection; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
7
|
|
|
Transport\Protocol, |
8
|
|
|
Transport\Frame, |
9
|
|
|
Transport\Frame\Method, |
10
|
|
|
Transport\Frame\Type, |
11
|
|
|
Transport\Frame\Channel, |
12
|
|
|
Transport\Frame\Value\UnsignedOctet, |
13
|
|
|
Transport\Frame\Value\UnsignedShortInteger, |
14
|
|
|
Transport\Frame\Value\UnsignedLongInteger, |
15
|
|
|
Exception\ReceivedFrameNotDelimitedCorrectly, |
16
|
|
|
Exception\PayloadTooShort, |
17
|
|
|
Exception\UnknownFrameType, |
18
|
|
|
Exception\NoFrameDetected |
19
|
|
|
}; |
20
|
|
|
use Innmind\Stream\Readable; |
21
|
|
|
|
22
|
|
|
final class FrameReader |
23
|
|
|
{ |
24
|
8 |
|
public function __invoke(Readable $stream, Protocol $protocol): Frame |
25
|
|
|
{ |
26
|
8 |
|
$octet = $stream->read(1); |
27
|
|
|
|
28
|
|
|
try { |
29
|
8 |
|
$type = Type::fromInt( |
30
|
8 |
|
UnsignedOctet::fromString($octet)->original()->value() |
31
|
|
|
); |
32
|
2 |
|
} catch (UnknownFrameType $e) { |
33
|
2 |
|
throw new NoFrameDetected($octet->append((string) $stream->read())); |
34
|
|
|
} |
35
|
|
|
|
36
|
7 |
|
$channel = new Channel( |
37
|
7 |
|
UnsignedShortInteger::fromString($stream->read(2))->original()->value() |
38
|
|
|
); |
39
|
|
|
$payload = $stream |
40
|
7 |
|
->read(UnsignedLongInteger::fromString($stream->read(4))->original()->value()) |
41
|
7 |
|
->toEncoding('ASCII'); |
42
|
|
|
|
43
|
7 |
|
if ($payload->length() < 4) { |
44
|
1 |
|
throw new PayloadTooShort; |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
$end = $stream->read(1)->toEncoding('ASCII'); |
48
|
|
|
|
49
|
6 |
|
if ($end->length() !== 1) { |
50
|
2 |
|
throw new ReceivedFrameNotDelimitedCorrectly; |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
$end = UnsignedOctet::fromString($end)->original()->value(); |
54
|
|
|
|
55
|
4 |
|
if ($end !== 0xCE) { |
56
|
|
|
throw new ReceivedFrameNotDelimitedCorrectly; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
$method = $payload->substring(0, 4); |
60
|
4 |
|
$method = new Method( |
61
|
4 |
|
UnsignedShortInteger::fromString($method->substring(0, 2))->original()->value(), |
62
|
4 |
|
UnsignedShortInteger::fromString($method->substring(2, 4))->original()->value() |
63
|
|
|
); |
64
|
4 |
|
$arguments = $payload->substring(4); |
65
|
|
|
|
66
|
4 |
|
return new Frame( |
67
|
4 |
|
$type, |
68
|
4 |
|
$channel, |
69
|
4 |
|
$method, |
70
|
4 |
|
...$protocol->read($method, $arguments) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|