|
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
|
|
|
}; |
|
18
|
|
|
use Innmind\Stream\Readable; |
|
19
|
|
|
|
|
20
|
|
|
final class FrameReader |
|
21
|
|
|
{ |
|
22
|
4 |
|
public function __invoke(Readable $stream, Protocol $protocol): Frame |
|
23
|
|
|
{ |
|
24
|
4 |
|
$type = Type::fromInt( |
|
25
|
4 |
|
UnsignedOctet::fromString($stream->read(1))->original()->value() |
|
26
|
|
|
); |
|
27
|
4 |
|
$channel = new Channel( |
|
28
|
4 |
|
UnsignedShortInteger::fromString($stream->read(2))->original()->value() |
|
29
|
|
|
); |
|
30
|
|
|
$payload = $stream |
|
31
|
4 |
|
->read(UnsignedLongInteger::fromString($stream->read(4))->original()->value()) |
|
32
|
4 |
|
->toEncoding('ASCII'); |
|
33
|
|
|
|
|
34
|
4 |
|
if ($payload->length() < 4) { |
|
35
|
1 |
|
throw new PayloadTooShort; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
$end = $stream->read(1)->toEncoding('ASCII'); |
|
39
|
|
|
|
|
40
|
3 |
|
if ($end->length() !== 1) { |
|
41
|
2 |
|
throw new ReceivedFrameNotDelimitedCorrectly; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
$end = UnsignedOctet::fromString($end)->original()->value(); |
|
45
|
|
|
|
|
46
|
1 |
|
if ($end !== 0xCE) { |
|
47
|
|
|
throw new ReceivedFrameNotDelimitedCorrectly; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
$method = $payload->substring(0, 4); |
|
51
|
1 |
|
$method = new Method( |
|
52
|
1 |
|
UnsignedShortInteger::fromString($method->substring(0, 2))->original()->value(), |
|
53
|
1 |
|
UnsignedShortInteger::fromString($method->substring(2, 4))->original()->value() |
|
54
|
|
|
); |
|
55
|
1 |
|
$arguments = $payload->substring(4); |
|
56
|
|
|
|
|
57
|
1 |
|
return new Frame( |
|
58
|
1 |
|
$type, |
|
59
|
1 |
|
$channel, |
|
60
|
1 |
|
$method, |
|
61
|
1 |
|
...$protocol->read($method, $arguments) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|