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