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
|
|
|
Exception\LogicException |
20
|
|
|
}; |
21
|
|
|
use Innmind\Stream\Readable; |
22
|
|
|
use Innmind\Filesystem\Stream\StringStream; |
23
|
|
|
|
24
|
|
|
final class FrameReader |
25
|
|
|
{ |
26
|
52 |
|
public function __invoke(Readable $stream, Protocol $protocol): Frame |
27
|
|
|
{ |
28
|
52 |
|
$octet = UnsignedOctet::fromStream($stream); |
29
|
|
|
|
30
|
|
|
try { |
31
|
52 |
|
$type = Type::fromInt($octet->original()->value()); |
32
|
2 |
|
} catch (UnknownFrameType $e) { |
33
|
2 |
|
throw new NoFrameDetected(new StringStream( |
34
|
2 |
|
(string) $stream->read()->prepend((string) $octet) |
35
|
|
|
)); |
36
|
|
|
} |
37
|
|
|
|
38
|
51 |
|
$channel = new Channel( |
39
|
51 |
|
UnsignedShortInteger::fromStream($stream)->original()->value() |
40
|
|
|
); |
41
|
|
|
$payload = $stream |
42
|
51 |
|
->read(UnsignedLongInteger::fromStream($stream)->original()->value()) |
43
|
51 |
|
->toEncoding('ASCII'); |
44
|
|
|
|
45
|
|
|
if ( |
46
|
|
|
( |
47
|
51 |
|
$type === Type::method() || |
48
|
51 |
|
$type === Type::header() |
49
|
|
|
) && |
50
|
51 |
|
$payload->length() < 4 |
51
|
|
|
) { |
52
|
1 |
|
throw new PayloadTooShort; |
53
|
|
|
} |
54
|
|
|
|
55
|
50 |
|
$end = UnsignedOctet::fromStream($stream)->original()->value(); |
56
|
|
|
|
57
|
50 |
|
if ($end !== Frame::end()) { |
58
|
1 |
|
throw new ReceivedFrameNotDelimitedCorrectly; |
59
|
|
|
} |
60
|
|
|
|
61
|
49 |
|
$payload = new StringStream((string) $payload); |
62
|
|
|
|
63
|
|
|
switch ($type) { |
64
|
49 |
|
case Type::method(): |
65
|
46 |
|
$method = new Method( |
66
|
46 |
|
UnsignedShortInteger::fromStream($payload) |
67
|
46 |
|
->original() |
68
|
46 |
|
->value(), |
69
|
46 |
|
UnsignedShortInteger::fromStream($payload) |
70
|
46 |
|
->original() |
71
|
46 |
|
->value() |
72
|
|
|
); |
73
|
|
|
|
74
|
46 |
|
return Frame::method( |
75
|
46 |
|
$channel, |
76
|
46 |
|
$method, |
77
|
46 |
|
...$protocol->read($method, $payload) |
78
|
|
|
); |
79
|
|
|
|
80
|
19 |
|
case Type::header(): |
81
|
17 |
|
$class = UnsignedShortInteger::fromStream($payload) |
82
|
17 |
|
->original() |
83
|
17 |
|
->value(); |
84
|
17 |
|
$payload->read(2); // walk over the weight definition |
85
|
|
|
|
86
|
17 |
|
return Frame::header( |
87
|
17 |
|
$channel, |
88
|
17 |
|
$class, |
|
|
|
|
89
|
17 |
|
...$protocol->readHeader($payload) |
90
|
|
|
); |
91
|
|
|
|
92
|
18 |
|
case Type::body(): |
93
|
17 |
|
return Frame::body($channel, $payload->read()); |
94
|
|
|
|
95
|
1 |
|
case Type::heartbeat(): |
96
|
1 |
|
return Frame::heartbeat(); |
97
|
|
|
|
98
|
|
|
default: |
99
|
|
|
throw new LogicException; //if reached then there's an implementation error |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|