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